0

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.

2 Answers2

0

for start try:

if (serialSIM800.available()) {
  String content = serialSIM800.readString();
  Serial.print(content);
}

in setup() add serialSIM800.setTimeut(50);

Juraj
  • 3,490
  • 4
  • 18
  • 25
  • tnx I just used the `serialSIM800.readString()` instead of the `serialSIM800.read()` – omid ebrahimi Nov 08 '18 at 19:58
  • I doubt it really helped. What with `while(serialSIM800.available()==0);` blocking the loop()? – Juraj Nov 08 '18 at 20:02
  • I use this code: `while(serialSIM800.available()==0){ delay(2000); return;}` If the serial is not available for two seconds, it will be return. – omid ebrahimi Nov 09 '18 at 12:48
  • `if (serialSIM800.available()) {` ensures the sketch loops unblocked until data is not available. no need to block the loop – Juraj Nov 09 '18 at 13:13
0

Finally, I changed the code as follows:

 String dump(bool printData) {
      byte while_cunt = 0;
      while (serialSIM800.available() == 0){
        while_cunt++;
        delay(15); // It can change for a proper delay
        if(while_cunt >=250){  // If the data from serial was not received past 3.75 seconds
          Serial.println("return");
          return "";   // Exit from dump function
        }
      }
      String content = "";
      while (serialSIM800.available() > 0) {
        content += serialSIM800.readString();
      }

      if (printData) {
        Serial.println(content);
      }
      return content;
    }

It can return and print serial data And it works fast Because every 15 milliseconds checks that the data has been received and if it has not been received for a certain period of time(In this case 3.75 seconds), it will be out of process and will not crash. Examples for this function:

     serialSIM800.println("AT");
     dump(true); 
// print Received response from sim800

    serialSIM800.println("AT");
    String str =  dump(true); 
    // print Received response from sim800 and Saved in variable named 'str'

    serialSIM800.println("AT");
     String str =  dump(false); 
//Save response  in variable named 'str' without print