SIM800, works but needs coding to check if really online

Hey,

So i've got SIM800 working fine with AT-commands.

At the moment my code is working with delays. I've basically just tested how many seconds does it take for the GSM to get online and send the data.

I would like some help on how to IF's for the procedure. For example, to check if the module is online and only then proceed. Perhaps if it fails multiple times -> do something.

void sendData() {
  Serial.println("Starting data transfer");
  int u;
  for (u = 0; u < 20; u++) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");   // print empty line

  delay(3000);
  Serial.println("Sending data");
  delay(100);
  serialSIM800.println("AT+SAPBR=3,1,\"APN\",\"internet\"");
  delay(1000);
  serialSIM800.println("AT+SAPBR=1,1");
  delay(1000);
  serialSIM800.println("AT+HTTPINIT");
  delay(500);
  serialSIM800.println("AT+HTTPPARA=\"CID\",1");
  delay(500);
  serialSIM800.println("AT+HTTPPARA=\"UA\",\"Paatti_0_8\"");    // Change User Agent for web server

  delay(500);
  Serial.println("Starting payload");
  char valA[10], valB[10], valC[10], valD[10], valE[10], valF[10];

  String identifier = String("Testip");
  String location   = String("lokaatio");

  //dtostrf(identifier, 20,20, valA); // identifier A
  //dtostrf(location, 3, 3, valB);    // location   B
  dtostrf(vin, 3, 2, valC);           // voltage    C
  dtostrf(acsCurrent, 4, 3, valD);    // current    D
  dtostrf(t, 3, 2, valE);             // temp       E
  dtostrf(h, 3, 2, valF);             // humi       F


  char urlData[200];

  /* Order of URL
        identifier
        location
        voltage
        current
        temp
        humi
        gps_lat
        gps_long
  */

 
  // Construct Payload for URL
  strcpy(urlData, "\"AT+HTTPPARA=\"URL\",\"http://url.domain.fi/simlogger/db_simlogger_insert.php?");
  strcat(urlData, "pw=");
  strcat(urlData, "13fev");      // Secret password
  strcat(urlData, "&identifier=");
  strcat(urlData, "ident");         // change to variable
  strcat(urlData, "&location=");
  strcat(urlData, "loc");           // change to variable
  strcat(urlData, "&voltage=");
  strcat(urlData, valC);
  strcat(urlData, "&current=");
  strcat(urlData, valD);
  strcat(urlData, "&temp=");
  strcat(urlData, valE);
  strcat(urlData, "&humi=");
  strcat(urlData, valF);
  strcat(urlData, "&gps_lat=");
  strcat(urlData, "10.000000");          // change to variable
  strcat(urlData, "&gps_long=");
  strcat(urlData, "20.000000");          // change to variable
  strcat(urlData, "\"");

  Serial.print("Payload: ");      //
  Serial.print(urlData);          // Print to serial console to see what is inside the payload
  Serial.println(" EOL");         //

  delay(3000);    // Important (datalenght)
  Serial.println("Payload ready, send to modem");
  serialSIM800.println(urlData);  // Print to modem
  delay(3000);    // Important (datalenght)

  serialSIM800.println("AT+HTTPACTION=0");
  Serial.println("Data sent!");
  delay(4000);


}

So, if I have understood correctly, you want to check if the AT commands are successful instead of just closing your eyes and delaying.

You need to read the serial response from the SIM800 after each command and check that it is what you want, eg. "OK", "ERROR", etc. Note that you may need to handle a timeout situation where you don't receive a response in a reasonable amount of time.

Yes exaclty. Could you provide me with some sort of example if statement?

Well, you need to read the data from serial first. You may be able to take inspiration from Robin2's excellent thread about reading from serial.

When you have read the response, then you can use strstr() to search for whatever token you are interested in, like:

if (strstr(mySerialInputBuffer, "OK")) {
  // found OK so do something
}

You could also take a look at someone else's work.