Sorry for my weak English. I'm trying to receive the json data from Sim800 on my Arduino. To read the data on the serial port I used the following code:
while(serialSIM800.available()==0); //Wait until the data is received
String content = "";
while(serialSIM800.available()>0){ // When data is received
content = content + char(char (serialSIM800.read()));
}
Serial.print(content);
But incomplete data is received. As follows:
{"id":"1212","temp":"24","hum","4
For the better result I used the following code:
byte x;
char data[128];
void sim800Reply() {
x=0;
do{
while(serialSIM800.available()==0);
data[x]=serialSIM800.read();
Serial.print(data[x]);
x++;
} while(!(data[x-1]=='K'&&data[x-2]=='O'));
}
Data is completely received. As follows:
{"id":"1212","temp":"24","hum","45","date":"11.2018","status":"200"}
OK
But I think this code is not good and there are problems.for example If the serialSIM800 is not available like when sim800 are not connected, The following code causes crash while(serialSIM800.available()==0);
Because this is always true OR If there is an error and OK
Was not received, The following code causes crash while(!(data[x-1]=='K'&&data[x-2]=='O'));
Because this is always true.The maximum data length is 120 bytes, What should I do to Receive the Json data from Arduino serial? Thank you all.
for start try:
in setup() add
serialSIM800.setTimeut(50);