Sim808 problems with AT commands HELP!

Hi, i’m doing a project with Arduino uno and Sim808 v3.2.4.
I look for GPS cordinates of my arduino and see if it is in the range cordinates that i’ve choose.
If not he send me an SMS with all data.
I also want, if i send to arduino an SMS that he send me back an SMS
with all the info (if i want to check if everithing is ok).

All function (get GPS data, send SMS, recive SMS and send SMS back) are working fine separatly…
but if i use them together it doesent work… he just ignore the sms that i’ve send and continue
to get GPS data. Why??

I also have problem in sending multiple SMS…he just send me the first SMS…I see that if i call the function
“get_GPS()” betwen first and second SMS the problem is solved but i wish to fix it in the right way.

Please help me…if you need further information just ask!
Thankyou very much!

#include <SoftwareSerial.h>

byte GNSSrunstatus;;
byte Fixstatus;
char UTCdatetime[18];
char latitude[11];
char logitude[11];
char altitude[8];
char speedOTG[6];
char course[6];
byte fixmode;
char HDOP[4];
char PDOP[4];
char VDOP[4];
char satellitesinview[2];
char GNSSsatellitesused[2];
char GLONASSsatellitesused[2];
char cn0max[2];
char HPA[6];
char VPA[6];
float latGPS;
float longGPS;
const float latA = 45.813000;
const float longA = 10.952000;
const float latB = 45.816000;
const float longB = 10.957500;
boolean furto;
String textMessage;

SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Serial begin ok");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("AT");
  mySerial.println("AT+CGNSPWR=1");
  delay(10000);
}

void loop() { // run over and over
  delay(20000);
  get_GPS();
  antifurto();
  if (furto) {
  smsfurto1();
  smsfurto2();
  }
  ricezione();
  delay(20000);
}



// get GPS cordinates 
int8_t get_GPS() {

  int8_t counter, answer;
  long previous;

  // First get the NMEA string
  // Clean the input buffer
  while ( mySerial.available() > 0) mySerial.read();
  // request Basic string
  mySerial.println("AT+CGNSINF"); //sendATcommand("AT+CGNSINF", "AT+CGNSINF\r\n\r\n", 2000);

  counter = 0;
  answer = 0;
  memset(frame, '\0', sizeof(frame));    // Initialize the string
  previous = millis();
  // this loop waits for the NMEA string
  do {

    if (mySerial.available() != 0) {
      frame[counter] = mySerial.read();
      counter++;
      // check if the desired answer is in the response of the module
      if (strstr(frame, "OK") != NULL)
      {
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < 2000));
  frame[counter - 3] = '\0';

  // Parses the string
  strtok_single(frame, ": ");
  GNSSrunstatus = atoi(strtok_single(NULL, ","));;// Gets GNSSrunstatus
  Fixstatus = atoi(strtok_single(NULL, ",")); // Gets Fix status
  strcpy(UTCdatetime, strtok_single(NULL, ",")); // Gets UTC date and time
  strcpy(latitude, strtok_single(NULL, ",")); // Gets latitude
  strcpy(logitude, strtok_single(NULL, ",")); // Gets longitude
  strcpy(altitude, strtok_single(NULL, ",")); // Gets MSL altitude
  strcpy(speedOTG, strtok_single(NULL, ",")); // Gets speed over ground
  strcpy(course, strtok_single(NULL, ",")); // Gets course over ground
  fixmode = atoi(strtok_single(NULL, ",")); // Gets Fix Mode
  strtok_single(NULL, ",");
  strcpy(HDOP, strtok_single(NULL, ",")); // Gets HDOP
  strcpy(PDOP, strtok_single(NULL, ",")); // Gets PDOP
  strcpy(VDOP, strtok_single(NULL, ",")); // Gets VDOP
  strtok_single(NULL, ",");
  strcpy(satellitesinview, strtok_single(NULL, ",")); // Gets GNSS Satellites in View
  strcpy(GNSSsatellitesused, strtok_single(NULL, ",")); // Gets GNSS Satellites used
  strcpy(GLONASSsatellitesused, strtok_single(NULL, ",")); // Gets GLONASS Satellites used
  strtok_single(NULL, ",");
  strcpy(cn0max, strtok_single(NULL, ",")); // Gets C/N0 max
  strcpy(HPA, strtok_single(NULL, ",")); // Gets HPA
  strcpy(VPA, strtok_single(NULL, "\r")); // Gets VPA
  return answer; 

  //convert string in float to compare it with my range cordinates
  longGPS = atof (logitude);
  latGPS = atof (latitude);
        
}


/* strtok_fixed - fixed variation of strtok_single */
static char *strtok_single(char *str, char const *delims)
{
    static char  *src = NULL;
    char  *p,  *ret = 0;
    if (str != NULL)
        src = str;
    if (src == NULL || *src == '\0')    // Fix 1
        return NULL;
    ret = src;                          // Fix 2
    if ((p = strpbrk(src, delims)) != NULL)
    {
        *p  = 0;
        src = ++p;
    }
    else
        src += strlen(src);
    return ret;
}



//look if it founds in the selected range of cordinates
void antifurto()  {
  if (latA < latGPS && latB > latGPS && longA < longGPS && longB > longGPS)  { furto =false;
  Serial.println("\ tutto ok");
  }
  else { furto =true;
  Serial.println("\ attenzione!");
  };
  }


//send SMS if is not in the selected range to the first phone number
void smsfurto1() { 
  //Set Exact Baud rate of the GSM/GPRS Module.
  mySerial.begin(9600); 
  mySerial.print("\r");
  delay(1000);                 
  mySerial.print("AT+CMGF=1\r");   
  delay(1000);
  /*Replace XXXXXXXXXX to 10 digit mobile number &  ZZ to 2 digit country code*/
  mySerial.print("AT+CMGS=\"+xxxxxxxxx\"\r");     
  delay(1000);
  //The text of the message to be sent.
  mySerial.print("ATTENZIONE FURTO! la tua arnia si trova qui: latitudine: ");
  mySerial.print(latitude);
  mySerial.print(", longitudine: ");
  mySerial.print(logitude);
  mySerial.print(", velocita: ");
  mySerial.print(speedOTG);
  mySerial.print(" kmh");   
  delay(1000);
  mySerial.write(0x1A);
  delay(1000);
  Serial.println("sms furto");
}


//send SMS if is not in the selected range to the second phone number
void smsfurto2() { 
  //Set Exact Baud rate of the GSM/GPRS Module.
  mySerial.begin(9600); 
  mySerial.print("\r");
  delay(1000);                 
  mySerial.print("AT+CMGF=1\r");   
  delay(1000);
  /*Replace XXXXXXXXXX to 10 digit mobile number &  ZZ to 2 digit country code*/
  mySerial.print("AT+CMGS=\"+yyyyyyyy\"\r");     
  delay(1000);
  //The text of the message to be sent.
  mySerial.print("ATTENZIONE FURTO! la tua arnia si trova qui: latitudine: ");
  mySerial.print(latitude);
  mySerial.print(", longitudine: ");
  mySerial.print(logitude);
  mySerial.print(", velocita: ");
  mySerial.print(speedOTG);
  mySerial.print(" kmh");   
  delay(1000);
  mySerial.write(0x1A);
  delay(1000);
  Serial.println("sms furto");
}



//look if arduino have recive an SMS
void ricezione(){
  Serial.println("controllo ricezione sms");
  if(mySerial.available()>0){
    textMessage = mySerial.readString();
    Serial.print(textMessage);   
    delay(10);
  }
  if(textMessage.indexOf("STATO")>=0){
    // send SMS with GPS info 
    Serial.println("Invio info stato arnia");
    smsstato(); 
    textMessage = "";   
  }
}

// send SMS with GPS info 
void smsstato(){
  mySerial.print("AT+CMGF=1\r");   
  delay(1000);
  /*Replace XXXXXXXXXX to 10 digit mobile number &  ZZ to 2 digit country code*/
  mySerial.print("AT+CMGS=\"+39348xxxxx\"\r");     
  delay(1000);
  The text of the message to be sent.
  mySerial.print("La tua arnia si trova qui: latitudine: ");
  mySerial.print(latitude);
  mySerial.print(", longitudine: ");
  mySerial.print(logitude);   
  delay(1000);
  mySerial.write(0x1A);
  delay(1000);
  Serial.println("sms stato"); 
}

It looks like ricezione() is looking for characters sent by the SIM808 but the first thing that get_GPS() does is throw away any characters sent by the SIM808.

  // Clean the input buffer
  while ( mySerial.available() > 0) mySerial.read();

Thank you very much :).. So if i save "textMessage" string in the function "get_GPS()" before cleaning the buffer (instead saving it in "ricezione()") i will fix it? Or you suggest me something else?

Thank you very much again :slight_smile:

I would put ricezione() before get_GPS(). That way, if a text message has arrived in the 40 seconds of delay (20 at the top of the loop and 20 at the bottom) it will be received rather than thrown away. Your reply will just have the GPS position from forty seconds ago.

Didn't work :frowning: That's the answer on serial monitor.. ( i've also add some line to print GPS data to see
if it's working).

controllo ricezione sms


OK

+CMTI: "SM",1
mia float latitudine
45.817405
mia float latitudine
10.957757
UTCdatetime
20190907165747.000
latitude
45.817410
logitude
10.957758

controllo ricezione sms


OK
mia float latitudine
45.817398
mia float latitudine
10.957756
UTCdatetime
20190907165830.000
latitude
45.817400
logitude
10.957755

After " Controllo ricezione SMS" he always print "OK" as if he receives some AT command..except when i send the SMS than he printed controllo ricezione sms "OK" and then "+CMTI: "SM",1"..
i've tried again sending another SMS and he have printed "+CMTI: "SM",2.. but still doesn't work :frowning:

Post the separate sketches where each operation works separately.

This is the sketch i’ve used for reciving SMS.

// Include Software Serial library to communicate with GSM
#include <SoftwareSerial.h>

// Configure software serial port
SoftwareSerial mySerial(10, 11);

// Variable to store text message
String textMessage;


void setup() {

  // Initializing serial commmunication
  Serial.begin(9600); 
  mySerial.begin(9600);

  // Give time to your GSM shield log on to network
  delay(20000);
  Serial.print("mySerial ready...");

  // AT command to set mySerial to SMS mode
  mySerial.print("AT+CMGF=1\r"); 
  delay(100);
  // Set module to send SMS data to serial out upon receipt 
  mySerial.print("AT+CNMI=2,2,0,0,0\r");
  delay(100);
}

void loop(){
  ricezione();
  delay(2000);
}  

void ricezione(){
  if(mySerial.available()>0){
    textMessage = mySerial.readString();
    Serial.print(textMessage);    
    delay(10);
  } 
  if(textMessage.indexOf("STATO")>=0){
    // Turn on relay and save current state
    Serial.println("Invio info stato arnia");
    smsstato();  
    textMessage = "";   
  }
}

void smsstato(){
  mySerial.print("AT+CMGF=1\r");    
  delay(1000);
  /*Replace XXXXXXXXXX to 10 digit mobile number &  ZZ to 2 digit country code*/
  mySerial.print("AT+CMGS=\"+393483952xxx\"\r");     
  delay(1000);
  //The text of the message to be sent.
  mySerial.print("prova invio sms: ");  
  delay(1000);
  mySerial.write(0x1A);
  delay(1000);
  Serial.println("sms furto");
  
}

And this is the sketch i’ve used for GPS

#include <SoftwareSerial.h>

char frame[100];
byte GNSSrunstatus;;
byte Fixstatus;
char UTCdatetime[18];
char latitude[11];
char logitude[11];
char altitude[8];
char speedOTG[6];
char course[6];
byte fixmode;
char HDOP[4];
char PDOP[4];
char VDOP[4];
char satellitesinview[2];
char GNSSsatellitesused[2];
char GLONASSsatellitesused[2];
char cn0max[2];
char HPA[6];
char VPA[6];
float latGPS;
float longGPS;
const float latA = 45.813000;
const float longA = 10.952000;
const float latB = 45.816000;
const float longB = 10.957500;
boolean furto;

SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Serial begin ok");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("AT");
  mySerial.println("AT+CGNSPWR=1");
  delay(10000);
}

void loop() { // run over and over
  delay(10000);
  get_GPS();
  antifurto();
  if (furto) {
  smsfurto();
  }
  delay(10000);
}


int8_t get_GPS() {

  int8_t counter, answer;
  long previous;

  // First get the NMEA string
  // Clean the input buffer
  while ( mySerial.available() > 0) mySerial.read();
  // request Basic string
  mySerial.println("AT+CGNSINF"); //sendATcommand("AT+CGNSINF", "AT+CGNSINF\r\n\r\n", 2000);

  counter = 0;
  answer = 0;
  memset(frame, '\0', sizeof(frame));    // Initialize the string
  previous = millis();
  // this loop waits for the NMEA string
  do {

    if (mySerial.available() != 0) {
      frame[counter] = mySerial.read();
      counter++;
      // check if the desired answer is in the response of the module
      if (strstr(frame, "OK") != NULL)
      {
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < 2000));
  frame[counter - 3] = '\0';

  // Parses the string
  strtok_single(frame, ": ");
  GNSSrunstatus = atoi(strtok_single(NULL, ","));;// Gets GNSSrunstatus
  Fixstatus = atoi(strtok_single(NULL, ",")); // Gets Fix status
  strcpy(UTCdatetime, strtok_single(NULL, ",")); // Gets UTC date and time
  strcpy(latitude, strtok_single(NULL, ",")); // Gets latitude
  strcpy(logitude, strtok_single(NULL, ",")); // Gets longitude
  strcpy(altitude, strtok_single(NULL, ",")); // Gets MSL altitude
  strcpy(speedOTG, strtok_single(NULL, ",")); // Gets speed over ground
  strcpy(course, strtok_single(NULL, ",")); // Gets course over ground
  fixmode = atoi(strtok_single(NULL, ",")); // Gets Fix Mode
  strtok_single(NULL, ",");
  strcpy(HDOP, strtok_single(NULL, ",")); // Gets HDOP
  strcpy(PDOP, strtok_single(NULL, ",")); // Gets PDOP
  strcpy(VDOP, strtok_single(NULL, ",")); // Gets VDOP
  strtok_single(NULL, ",");
  strcpy(satellitesinview, strtok_single(NULL, ",")); // Gets GNSS Satellites in View
  strcpy(GNSSsatellitesused, strtok_single(NULL, ",")); // Gets GNSS Satellites used
  strcpy(GLONASSsatellitesused, strtok_single(NULL, ",")); // Gets GLONASS Satellites used
  strtok_single(NULL, ",");
  strcpy(cn0max, strtok_single(NULL, ",")); // Gets C/N0 max
  strcpy(HPA, strtok_single(NULL, ",")); // Gets HPA
  strcpy(VPA, strtok_single(NULL, "\r")); // Gets VPA

  //convert GPS data in float to compare with my cordinates range
  longGPS = atof (logitude); 
  latGPS = atof (latitude);
       
  Serial.println("mia float latitudine");
  Serial.println(latGPS,6);
  Serial.println("mia float latitudine");
  Serial.println(longGPS,6);
  Serial.println("UTCdatetime");
  Serial.println(UTCdatetime);
  Serial.println("latitude"); 
  Serial.println(latitude);
  Serial.println("logitude");
  Serial.println(logitude);
   return answer;  
}


/* strtok_fixed - fixed variation of strtok_single */
static char *strtok_single(char *str, char const *delims)
{
    static char  *src = NULL;
    char  *p,  *ret = 0;
    if (str != NULL)
        src = str;
    if (src == NULL || *src == '\0')    // Fix 1
        return NULL;
    ret = src;                          // Fix 2
    if ((p = strpbrk(src, delims)) != NULL)
    {
        *p  = 0;
        src = ++p;
    }
    else
        src += strlen(src);
    return ret;
}


//check if GPS is in the selected range
void antifurto()  { 
  if (latA < latGPS && latB > latGPS && longA < longGPS && longB > longGPS)  { furto =false;
  Serial.println("\ tutto ok");
  }
  else { furto =true;
  Serial.println("\ attenzione!");
  };
  }



//send SMS allarm
void smsfurto() {  
  //Set Exact Baud rate of the GSM/GPRS Module.
  mySerial.begin(9600);  
  mySerial.print("\r");
  delay(1000);                  
  mySerial.print("AT+CMGF=1\r");    
  delay(1000);
  /*Replace XXXXXXXXXX to 10 digit mobile number &  ZZ to 2 digit country code*/
  mySerial.print("AT+CMGS=\"+393388719xxx\"\r");     
  delay(1000);
  //The text of the message to be sent.
  mySerial.print("ATTENZIONE FURTO! la tua arnia si trova qui: latitudine: ");
  mySerial.print(latitude);
  mySerial.print(", longitudine: ");
  mySerial.print(logitude);
  mySerial.print(", velocita: ");
  mySerial.print(speedOTG);
  mySerial.print(" kmh");    
  delay(1000);
  mySerial.write(0x1A);
  delay(1000);
  Serial.println("sms furto"); 
}

I ve just found an error!! When i put the sketch together i didn’t copy the AT commands for set “mySerial to SMS mode”… and to Set module to send SMS data to serial out upon receipt…

Here is the new sketch…

#include <SoftwareSerial.h>

char frame[100];
byte GNSSrunstatus;;
byte Fixstatus;
char UTCdatetime[18];
char latitude[11];
char logitude[11];
char altitude[8];
char speedOTG[6];
char course[6];
byte fixmode;
char HDOP[4];
char PDOP[4];
char VDOP[4];
char satellitesinview[2];
char GNSSsatellitesused[2];
char GLONASSsatellitesused[2];
char cn0max[2];
char HPA[6];
char VPA[6];
float latGPS;
float longGPS;
const float latA = 45.813000;
const float longA = 10.952000;
const float latB = 45.816000;
const float longB = 10.957500;
boolean furto;
// Variable to store text message
String textMessage;


SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Serial begin ok");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("AT");
  mySerial.println("AT+CGNSPWR=1");
  delay(10000);
}

void loop() { // run over and over
  ricezione();
  delay(10000);
  get_GPS();
  //antifurto();
  //if (furto) {
  //smsfurto();
  //}
  delay(10000);
}


int8_t get_GPS() {

  int8_t counter, answer;
  long previous;

  // First get the NMEA string
  // Clean the input buffer
  while ( mySerial.available() > 0) mySerial.read();
  // request Basic string
  mySerial.println("AT+CGNSINF"); //sendATcommand("AT+CGNSINF", "AT+CGNSINF\r\n\r\n", 2000);

  counter = 0;
  answer = 0;
  memset(frame, '\0', sizeof(frame));    // Initialize the string
  previous = millis();
  // this loop waits for the NMEA string
  do {

    if (mySerial.available() != 0) {
      frame[counter] = mySerial.read();
      counter++;
      // check if the desired answer is in the response of the module
      if (strstr(frame, "OK") != NULL)
      {
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < 2000));
  frame[counter - 3] = '\0';

  // Parses the string
  strtok_single(frame, ": ");
  GNSSrunstatus = atoi(strtok_single(NULL, ","));;// Gets GNSSrunstatus
  Fixstatus = atoi(strtok_single(NULL, ",")); // Gets Fix status
  strcpy(UTCdatetime, strtok_single(NULL, ",")); // Gets UTC date and time
  strcpy(latitude, strtok_single(NULL, ",")); // Gets latitude
  strcpy(logitude, strtok_single(NULL, ",")); // Gets longitude
  strcpy(altitude, strtok_single(NULL, ",")); // Gets MSL altitude
  strcpy(speedOTG, strtok_single(NULL, ",")); // Gets speed over ground
  strcpy(course, strtok_single(NULL, ",")); // Gets course over ground
  fixmode = atoi(strtok_single(NULL, ",")); // Gets Fix Mode
  strtok_single(NULL, ",");
  strcpy(HDOP, strtok_single(NULL, ",")); // Gets HDOP
  strcpy(PDOP, strtok_single(NULL, ",")); // Gets PDOP
  strcpy(VDOP, strtok_single(NULL, ",")); // Gets VDOP
  strtok_single(NULL, ",");
  strcpy(satellitesinview, strtok_single(NULL, ",")); // Gets GNSS Satellites in View
  strcpy(GNSSsatellitesused, strtok_single(NULL, ",")); // Gets GNSS Satellites used
  strcpy(GLONASSsatellitesused, strtok_single(NULL, ",")); // Gets GLONASS Satellites used
  strtok_single(NULL, ",");
  strcpy(cn0max, strtok_single(NULL, ",")); // Gets C/N0 max
  strcpy(HPA, strtok_single(NULL, ",")); // Gets HPA
  strcpy(VPA, strtok_single(NULL, "\r")); // Gets VPA

  //convert GPS string to float to compare with my GPS range cordinates
  longGPS = atof (logitude); 
  latGPS = atof (latitude);
       
  Serial.println("mia float latitudine");
  Serial.println(latGPS,6);
  Serial.println("mia float latitudine");
  Serial.println(longGPS,6);
  Serial.println("UTCdatetime");
  Serial.println(UTCdatetime);
  Serial.println("latitude"); 
  Serial.println(latitude);
  Serial.println("logitude");
  Serial.println(logitude);
   return answer;  
}


/* strtok_fixed - fixed variation of strtok_single */
static char *strtok_single(char *str, char const *delims)
{
    static char  *src = NULL;
    char  *p,  *ret = 0;
    if (str != NULL)
        src = str;
    if (src == NULL || *src == '\0')    // Fix 1
        return NULL;
    ret = src;                          // Fix 2
    if ((p = strpbrk(src, delims)) != NULL)
    {
        *p  = 0;
        src = ++p;
    }
    else
        src += strlen(src);
    return ret;
}


//check if GPS is in the slected range
void antifurto()  { 
  if (latA < latGPS && latB > latGPS && longA < longGPS && longB > longGPS)  { furto =false;
  Serial.println("\ tutto ok");
  }
  else { furto =true;
  Serial.println("\ attenzione!");
  };
  }



//invio SMS furto
void smsfurto() {  
  //Set Exact Baud rate of the GSM/GPRS Module.
  mySerial.begin(9600);  
  mySerial.print("\r");
  delay(1000);                  
  mySerial.print("AT+CMGF=1\r");    
  delay(1000);
  /*Replace XXXXXXXXXX to 10 digit mobile number &  ZZ to 2 digit country code*/
  mySerial.print("AT+CMGS=\"+393388719xxx\"\r");     
  delay(1000);
  //The text of the message to be sent.
  mySerial.print("ATTENZIONE FURTO! la tua arnia si trova qui: latitudine: ");
  mySerial.print(latitude);
  mySerial.print(", longitudine: ");
  mySerial.print(logitude);
  mySerial.print(", velocita: ");
  mySerial.print(speedOTG);
  mySerial.print(" kmh");    
  delay(1000);
  mySerial.write(0x1A);
  delay(1000);
  Serial.println("sms furto"); 
}


void ricezione(){
  Serial.println ("controllo ricezione SMS");
  // AT command to set mySerial to SMS mode
  mySerial.print("AT+CMGF=1\r"); 
  delay(100);
  // Set module to send SMS data to serial out upon receipt 
  mySerial.print("AT+CNMI=2,2,0,0,0\r");
  delay(100);
  if(mySerial.available()>0){
    textMessage = mySerial.readString();
    Serial.print(textMessage);   
    delay(10);
  }
  if(textMessage.indexOf("STATO")>=0){
    // Turn on relay and save current state
    Serial.println("Invio info stato arnia");
    smsstato(); 
    textMessage = "";   
  }
}

void smsstato(){
  mySerial.print("AT+CMGF=1\r");    
  delay(1000);
  /*Replace XXXXXXXXXX to 10 digit mobile number &  ZZ to 2 digit country code*/
  mySerial.print("AT+CMGS=\"+393483952xxx\"\r");     
  delay(1000);
  //The text of the message to be sent.
  mySerial.print("prova invio sms: ");  
  delay(1000);
  mySerial.write(0x1A);
  delay(1000);
  Serial.println("sms stato");
  
}

It still doesn’t work (it works just if tourn off gps fuction)… i see that if i send SMS “STATO”
aftet revive() have run and before get_GPS() it happend nothing…But if i send it
after get_GPS() have run and before ricezione() runs it recive the SMS…but don’t send nothing.

i also don’t understand why after “controllo ricezione SMS” he print some numbers
Here is serial monitor response (i ve comments when i’ve sended SMS)

10:58:43.267 -> controllo ricezione SMS
10:58:44.493 -> ,8,,,37,,
10:58:44.493 -> 
10:58:44.493 -> OK
10:58:44.493 -> AT+CMGF=1

10:58:44.526 -> OK
10:58:44.526 -> AT+CNMI=2,2,0,0,0

10:58:44.526 -> OK

//SMS SENDED BEFORE get_GPS()

10:58:54.633 -> mia float latitudine
10:58:54.666 -> 45.817440
10:58:54.666 -> mia float latitudine
10:58:54.699 -> 10.957732
10:58:54.699 -> UTCdatetime
10:58:54.699 -> 20190908085853.000
10:58:54.732 -> latitude
10:58:54.732 -> 45.817443
10:58:54.765 -> logitude
10:58:54.765 -> 10.957732
10:59:04.703 -> controllo ricezione SMS
10:59:05.928 -> 11,8,,,33,,
10:59:05.961 -> 
10:59:05.961 -> OK
10:59:05.961 -> AT+CMGF=1

10:59:05.961 -> OK
10:59:05.961 -> AT+CNMI=2,2,0,0,0

10:59:05.994 -> OK
10:59:16.071 -> mia float latitudine
10:59:16.104 -> 45.817470
10:59:16.104 -> mia float latitudine
10:59:16.137 -> 10.957710
10:59:16.137 -> UTCdatetime
10:59:16.170 -> 20190908085915.000
10:59:16.170 -> latitude
10:59:16.203 -> 45.817473
10:59:16.203 -> logitude
10:59:16.203 -> 10.957710

//SMS SENDED AFTER get_GPS()

10:59:26.177 -> controllo ricezione SMS
10:59:27.403 -> 11,8,,,33,,
10:59:27.403 -> 
10:59:27.403 -> OK
10:59:27.403 -> 
10:59:27.403 -> +CMT: "+393332896XXX","","19/09/08,10:59:1mia float latitudine
10:59:37.576 -> 45.817455
10:59:37.576 -> mia float latitudine
10:59:37.610 -> 10.957712
10:59:37.610 -> UTCdatetime
10:59:37.610 -> 20190908085936.000
10:59:37.643 -> latitude
10:59:37.643 -> 45.817455
10:59:37.676 -> logitude
10:59:37.676 -> 10.957712

I think i need not to read any new message but to check if there is some unread message and then check if contain "STATO"..maybe?

ilteo85:
I think i need not to read any new message but to check if there is some unread message and then check if contain "STATO"..maybe?

I think that would work. Can you turn off the feature that sends you a message whenever an SMS arrives? Then in ricezione() you can ask the SIM808 if a new message is available. If a new message has arrived, read it and check for "STATO".

We are ALMOST there! I ve decided to read just the first message saved than check for STATO and then delete the message…so next one will be the first message save again :)… I chose this option becouse we will be just 2 person that will send SMS to arduino…and the chance that we will send both SMS before de first message have been deleted is almost zero…And i will keep the space for new SMS on sim card always free (otherwise i think it will be memory full after some message)

I ve said ALMOST becouse it works great when i disable all the commands in “smsstato()” except for “Serial.println(“sms stato”)” for test it… but when i enable those commands it just works send first SMS and doesent delete the SMS… I think there is something wrong in my send SMS commands also becouse when i’ve tried to send multiple SMS it doesent works… i ve read about this line to sends ctrl+z end of message.

mySerial.write(0x1A); 
// sends ctrl+z end of message

It’s already in the code i used but still not working… do i miss something?

Thank you very much…here is the code!

#include <SoftwareSerial.h>

char frame[100];
byte GNSSrunstatus;;
byte Fixstatus;
char UTCdatetime[18];
char latitude[11];
char logitude[11];
char altitude[8];
char speedOTG[6];
char course[6];
byte fixmode;
char HDOP[4];
char PDOP[4];
char VDOP[4];
char satellitesinview[2];
char GNSSsatellitesused[2];
char GLONASSsatellitesused[2];
char cn0max[2];
char HPA[6];
char VPA[6];
float latGPS;
float longGPS;
const float latA = 45.813000;
const float longA = 10.952000;
const float latB = 45.816000;
const float longB = 10.957500;
boolean furto;
// Variable to store text message
String textMessage;


SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Serial begin ok");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("AT");
  mySerial.println("AT+CGNSPWR=1");
  delay(10000);
}

void loop() { // run over and over
  ricezione();
  delay(10000);
  get_GPS();
  //antifurto();
  //if (furto) {
  //smsfurto();
  //}
  delay(10000);
}


int8_t get_GPS() {

  int8_t counter, answer;
  long previous;

  // First get the NMEA string
  // Clean the input buffer
  while ( mySerial.available() > 0) mySerial.read();
  // request Basic string
  mySerial.println("AT+CGNSINF"); //sendATcommand("AT+CGNSINF", "AT+CGNSINF\r\n\r\n", 2000);

  counter = 0;
  answer = 0;
  memset(frame, '\0', sizeof(frame));    // Initialize the string
  previous = millis();
  // this loop waits for the NMEA string
  do {

    if (mySerial.available() != 0) {
      frame[counter] = mySerial.read();
      counter++;
      // check if the desired answer is in the response of the module
      if (strstr(frame, "OK") != NULL)
      {
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < 2000));
  frame[counter - 3] = '\0';

  // Parses the string
  strtok_single(frame, ": ");
  GNSSrunstatus = atoi(strtok_single(NULL, ","));;// Gets GNSSrunstatus
  Fixstatus = atoi(strtok_single(NULL, ",")); // Gets Fix status
  strcpy(UTCdatetime, strtok_single(NULL, ",")); // Gets UTC date and time
  strcpy(latitude, strtok_single(NULL, ",")); // Gets latitude
  strcpy(logitude, strtok_single(NULL, ",")); // Gets longitude
  strcpy(altitude, strtok_single(NULL, ",")); // Gets MSL altitude
  strcpy(speedOTG, strtok_single(NULL, ",")); // Gets speed over ground
  strcpy(course, strtok_single(NULL, ",")); // Gets course over ground
  fixmode = atoi(strtok_single(NULL, ",")); // Gets Fix Mode
  strtok_single(NULL, ",");
  strcpy(HDOP, strtok_single(NULL, ",")); // Gets HDOP
  strcpy(PDOP, strtok_single(NULL, ",")); // Gets PDOP
  strcpy(VDOP, strtok_single(NULL, ",")); // Gets VDOP
  strtok_single(NULL, ",");
  strcpy(satellitesinview, strtok_single(NULL, ",")); // Gets GNSS Satellites in View
  strcpy(GNSSsatellitesused, strtok_single(NULL, ",")); // Gets GNSS Satellites used
  strcpy(GLONASSsatellitesused, strtok_single(NULL, ",")); // Gets GLONASS Satellites used
  strtok_single(NULL, ",");
  strcpy(cn0max, strtok_single(NULL, ",")); // Gets C/N0 max
  strcpy(HPA, strtok_single(NULL, ",")); // Gets HPA
  strcpy(VPA, strtok_single(NULL, "\r")); // Gets VPA

  //convert GPS string to float to compare with my GPS range cordinates
  longGPS = atof (logitude);
  latGPS = atof (latitude);
       
  Serial.println("mia float latitudine");
  Serial.println(latGPS,6);
  Serial.println("mia float latitudine");
  Serial.println(longGPS,6);
  Serial.println("UTCdatetime");
  Serial.println(UTCdatetime);
  Serial.println("latitude");
  Serial.println(latitude);
  Serial.println("logitude");
  Serial.println(logitude);
   return answer; 
}


/* strtok_fixed - fixed variation of strtok_single */
static char *strtok_single(char *str, char const *delims)
{
    static char  *src = NULL;
    char  *p,  *ret = 0;
    if (str != NULL)
        src = str;
    if (src == NULL || *src == '\0')    // Fix 1
        return NULL;
    ret = src;                          // Fix 2
    if ((p = strpbrk(src, delims)) != NULL)
    {
        *p  = 0;
        src = ++p;
    }
    else
        src += strlen(src);
    return ret;
}


//check if GPS is in the slected range
void antifurto()  {
  if (latA < latGPS && latB > latGPS && longA < longGPS && longB > longGPS)  { furto =false;
  Serial.println("\ tutto ok");
  }
  else { furto =true;
  Serial.println("\ attenzione!");
  };
  }



//invio SMS furto
void smsfurto() { 
  //Set Exact Baud rate of the GSM/GPRS Module.
  mySerial.begin(9600); 
  mySerial.print("\r");
  delay(1000);                 
  mySerial.print("AT+CMGF=1\r");   
  delay(1000);
  /*Replace XXXXXXXXXX to 10 digit mobile number &  ZZ to 2 digit country code*/
  mySerial.print("AT+CMGS=\"+393388719xxx\"\r");     
  delay(1000);
  //The text of the message to be sent.
  mySerial.print("ATTENZIONE FURTO! la tua arnia si trova qui: latitudine: ");
  mySerial.print(latitude);
  mySerial.print(", longitudine: ");
  mySerial.print(logitude);
  mySerial.print(", velocita: ");
  mySerial.print(speedOTG);
  mySerial.print(" kmh");   
  delay(1000);
  mySerial.write(0x1A);
  delay(1000);
  Serial.println("sms furto");
}


void ricezione(){
  Serial.println ("controllo ricezione SMS");
  // AT command to set mySerial to SMS mode
  mySerial.print("AT+CMGF=1\r");
  delay(100);
  // Read the first SMS saved in the sim
  mySerial.print("AT+CMGR=1\r");
  delay(10);  
  if(mySerial.available()>0){
    textMessage = mySerial.readString();
    Serial.print(textMessage);   
    delay(10);
  }
  // check if the SMS is "STATO"
  if(textMessage.indexOf("STATO")>=0){    
    Serial.println("Invio info stato arnia");
    smsstato();
    textMessage = "";   
  }
  // delete the first SMS 
  mySerial.print("AT+CMGD=1\r");
  delay(100);
}

void smsstato(){
  mySerial.print("AT+CMGF=1\r");   
  delay(1000);
  /*Replace XXXXXXXXXX to 10 digit mobile number &  ZZ to 2 digit country code*/
  mySerial.print("AT+CMGS=\"+39348395xxxx\"\r");     
  delay(1000);
  //The text of the message to be sent.
  mySerial.print("prova invio sms: "); 
  delay(1000);
  mySerial.write(0x1A);
  delay(1000);
  Serial.println("sms stato"); 
}