如何在Arduino程序中解决"invalid operands of types"?



这是对两个代码的操作。

首先,我通过sms(arduino,gsm模块(发送从gps模块中找到的纬度和经度值,然后通过另一个代码,我使用http协议(arduino + gps/gsm/gprs shield(将其发送到服务器中的php文件。现在,当我合并两个代码时,它显示如下错误:

Arduino: 1.8.10 (Windows 10), Board: "Arduino/Genuino Uno"
C:UsersrafezOneDriveDocumentsArduinoHajji_TrackerHajji_Tracker.ino: In function 'void getGPSLocation()':
Hajji_Tracker:58:29: error: invalid operands of types 'const char [12]' and 'char [16]' to binary 'operator+'
Serial.println("Latitude  :"+latitude);
~~~~~~~~~~~~~^~~~~~~~~
Hajji_Tracker:59:30: error: invalid operands of types 'const char [13]' and 'char [16]' to binary 'operator+'
Serial.println("Longitude  :"+longitude);
~~~~~~~~~~~~~~^~~~~~~~~~
C:UsersrafezOneDriveDocumentsArduinoHajji_TrackerHajji_Tracker.ino: In function 'void sendTabData(String, int, boolean)':
Hajji_Tracker:91:18: error: incompatible types in assignment of 'String' to 'char [16]'
latitude = data[3];
^
Hajji_Tracker:92:18: error: incompatible types in assignment of 'String' to 'char [16]'
longitude =data[4];
^

C:UsersrafezOneDriveDocumentsArduinoHajji_TrackerHajji_Tracker.ino:138:32: warning: invalid conversion from 'String*' to 'uint8_t {aka unsigned char}' [-fpermissive]
serialSIM808.write(latitude);   // Add id to the url
^
In file included from C:UsersrafezOneDriveDocumentsArduinoHajji_TrackerHajji_Tracker.ino:1:0:
C:Program Files (x86)ArduinohardwarearduinoavrlibrariesSoftwareSerialsrc/SoftwareSerial.h:102:18: note:   initializing argument 1 of 'virtual size_t SoftwareSerial::write(uint8_t)'
virtual size_t write(uint8_t byte);
^~~~~
C:UsersrafezOneDriveDocumentsArduinoHajji_TrackerHajji_Tracker.ino:144:33: warning: invalid conversion from 'String*' to 'uint8_t {aka unsigned char}' [-fpermissive]
serialSIM808.write(longitude);   // Add value to url
^
In file included from C:UsersrafezOneDriveDocumentsArduinoHajji_TrackerHajji_Tracker.ino:1:0:
C:Program Files (x86)ArduinohardwarearduinoavrlibrariesSoftwareSerialsrc/SoftwareSerial.h:102:18: note:   initializing argument 1 of 'virtual size_t SoftwareSerial::write(uint8_t)'
virtual size_t write(uint8_t byte);
^~~~~
C:UsersrafezOneDriveDocumentsArduinoHajji_TrackerHajji_Tracker.ino: In function 'void loop()':
Hajji_Tracker:169:37: error: cannot convert 'char*' to 'String*' for argument '1' to 'void gsm_send_data(String*, String*)'
gsm_send_data(latitude,longitude);
^
Multiple libraries were found for "SoftwareSerial.h"
Used: C:Program
exit status 1
invalid operands of types 'const char [12]' and 'char [16]' to binary 'operator+'

我尝试了很多事情,但无法摆脱这些问题。如何编译我的代码?

这是我的代码:

#include <SoftwareSerial.h>
SoftwareSerial serialSIM808(11, 10); //Arduino(RX=11), Arduino(TX=10) //you can replace these with any other pins
//Arduino(RX) to SIM808(TX)
//Arduino(TX) to SIM808(RX)
//Arduino(Gnd) to SIM808(Gnd)
String data[5];
#define DEBUG true
String state,timegps;
char latitude[16];// = "24";
char longitude[16];// = "24";
int numLatitude = 1,numLongitude = 100;

void gsm_disConnect_gprs(){
serialSIM808.write("AT+CGATT=0rn"); // Attach to GPRS
delay(2000);
Serial.println("GPRS off");
}
void setup() {
//Begin serial comunication with Arduino and Arduino IDE (Serial Monitor)
Serial.begin(9600);
while(!Serial);
//Being serial communication witj Arduino and SIM808
serialSIM808.begin(9600);
delay(1000);
Serial.println("Setup Complete!");
delay(200);
sendData("AT+CGNSPWR=1",1000,DEBUG);//Turn on GPS(GNSS - Global Navigation Satellite System)
delay(200);
sendData("AT+CGNSSEQ=RMC",1000,DEBUG);//configure GPS sequence mode to RMC
delay(200);
sendData("AT+CGPSSTATUS?",1000,DEBUG);//check if GPS status is either 2D or 3D fix location. You can use this AT command to check te GPS status manually using the serial monitor.

getGPSLocation();
serialSIM808.write("AT+CREG?rn");
delay(150);
gsm_connect_gprs();
}
void getGPSLocation()
{
//--------------------- send SMS containing google map location---------------------
sendTabData("AT+CGNSINF",1000,DEBUG);//Get GPS info(location
if (state !=0) {
Serial.println("State  :"+state);
Serial.println("Time  :"+timegps);
Serial.println("Latitude  :"+latitude);
Serial.println("Longitude  :"+longitude);
} else {
Serial.println("GPS Initializing… Items to check: Power supply 12v 2A; Antenna must be facing the sky and/or near the window; Power switch must be turned on.");
}
//-----------------------------------------------------------------------------------
}

void sendTabData(String command , const int timeout , boolean debug){
serialSIM808.println(command);
long int time = millis();
int i = 0;
while((time+timeout) > millis()){
while(serialSIM808.available()){
char c = serialSIM808.read();
if (c != ',') {
data[i] +=c;
delay(100);
} else {
i++;
}
if (i == 5) {
delay(100);
goto exitL;
}
}
}exitL:
if (debug) {
state = data[1];
timegps = data[2];
latitude = data[3];
longitude =data[4];
memset(data,0,sizeof(data));
}
}

String sendData (String command , const int timeout ,boolean debug){
String response = "";
serialSIM808.println(command);
long int time = millis();
int i = 0;
while ( (time+timeout ) > millis()){
while (serialSIM808.available()){
char c = serialSIM808.read();
response +=c;
}
}
if (debug) {
Serial.print(response);
}
return response;
}

void gsm_connect_gprs(){
serialSIM808.write("AT+CGATT=1rn"); // Attach to GPRS
delay(2000);
serialSIM808.write("AT+SAPBR=1,1rn"); // Open a GPRS context
delay(2000);
//serialSIM808.write("AT+SAPBER=2,1rn");  // To query the GPRS context
//delay(2000);
Serial.println("GPRS on");
}

void gsm_send_data(String * latitude,String * longitude)
{
Serial.println("Sending data.");    
serialSIM808.write("AT+HTTPINITrn");  // Initialize HTTP
//Serial.print("AT+HTTPINITrn");
delay(1000);
serialSIM808.write("AT+HTTPPARA="URL","http://499b.000webhostapp.com/?latitude=3&longitude=16"rn"); // Send PARA command
//serialSIM808.write("AT+HTTPPARA="URL","http://shehanshaman.000webhostapp.com/?id=");
//Serial.print("AT+HTTPPARA="URL","http://shehanshaman.000webhostapp.com/?id=");
delay(50);
serialSIM808.write(latitude);   // Add id to the url
//Serial.print(latitude);
delay(50);
serialSIM808.write("&longitude="); 
//Serial.print("&longitude=");
delay(50);
serialSIM808.write(longitude);   // Add value to url
//Serial.print(longitude);
delay(50);
serialSIM808.write(""rn");   // close url
//Serial.print(""rn");
delay(2000);
serialSIM808.write("AT+HTTPPARA="CID",1rn");    // End the PARA
//Serial.print("AT+HTTPPARA="CID",1rn");
delay(2000);
serialSIM808.write("AT+HTTPACTION=0rn");
//Serial.print("AT+HTTPACTION=0rn");
delay(3000);    
serialSIM808.write("AT+HTTPTERMrn");
//Serial.print("AT+HTTPTERMrn");
//Serial.println();
delay(3000);    
Serial.print("data sent complete : ");
}
void loop() {
//Read SIM808 output (if available) and print it in Arduino IDE Serial Monitor
if(numLatitude<5){
itoa(numLatitude, latitude, 10);
itoa(numLongitude,longitude, 10);
gsm_send_data(latitude,longitude);
Serial.print(numLatitude);
Serial.print(" >> ");
Serial.print(numLongitude);
Serial.println();
delay(2000);
numLatitude++;
numLongitude+=45;
if(numLatitude==5) gsm_disConnect_gprs();
}
}

请注意字符串数组和字符数组之间的区别。

未为字符串和字符数组定义 + 运算符。

请改用两个字符串对象。

相关内容

最新更新