SIM808: GPS refresh and change phone number

Hello,

i'm trying to build a car tracking system using SIM808 with DFRobot library:

  1. Arduino starts with state=0 :nothing to do except wait for incoming call),

  2. when a phone call arrives, it passes to state=1, FIRST PROBLEM: how can i read (and display) the phone number of the incoming call? I'd like to pass from one state to another only if certain numbers call

3)if the car moves during state=1 arduino/sim808 should send every ten second the gps position, but this doesn't happen, why? If I get Arduino pass to state 0 and then again to state 1 it gives to me the right gps location, so the gps module refreshes the position.

This is the code

#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>
#define MESSAGE_LENGTH 160

char gprsBuffer[64]; //variabile per attivare l'ascolto delle chiamate in entrata
char *s = NULL;

char message[MESSAGE_LENGTH];//variabili per attivare l'ascolto degli sms in entrata
int messageIndex = 0;
char MESSAGE[300];
char lat[12];//variabili per la posizione
char lon[12];
float la, lo, la_old = 0.00, lo_old = 0.00, la_dec, lo_dec;
int la_int, lo_int;
 

char *phone;
char mio[] = "+39xxxxxxxxxxx";
char datetime[24];
bool state = 0; //variabile di stato

unsigned long previousMillis = 0;        
// constants won't change:
long interval = 10000;           

#define PIN_TX    3
#define PIN_RX    2
SoftwareSerial mySerial(PIN_TX,PIN_RX);
DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,

void coord(){
  sim808.getGPS();
  
    while(!sim808.getGPS())
    {
     //Serial.println("pippo");
    }
  
    la = sim808.GPSdata.lat;
    lo = sim808.GPSdata.lon;
}

void sms_send(){
        dtostrf(la_dec, 0, 4, lat); //put float value of la into char array of lat. 6 = number of digits before decimal sign. 2 = number of digits after the decimal sign.
        dtostrf(lo_dec, 0, 4, lon); //put float value of lo into char array of lon
        
     
        sprintf(MESSAGE, "Latitude : %s\nLongitude : %s\nMy Module Is Working.\nhttp://maps.google.com/maps?q=%s,%s\n", lat, lon, lat, lon);
        Serial.println(MESSAGE);       
    
         //phone = mio;
         Serial.println("Sim808 init success");
         Serial.println("Start to send message ...");
         Serial.println(MESSAGE);
         Serial.println(phone);
         sim808.sendSMS(phone,MESSAGE);
         //memcpy(phone[16], mio, sizeof(phone[16]));
         phone = mio;
}

void ring(){
//******** Wait serial data *************
   if(sim808.readable()){
      sim808_read_buffer(gprsBuffer,32,DEFAULT_TIMEOUT);
      //Serial.print(gprsBuffer);

   //************** Detect the current state of the telephone or SMS ************************
      if(NULL != strstr(gprsBuffer,"RING")) {
          sim808.answer();          
          state = !state;
          }
    sim808_clean_buffer(gprsBuffer,32);
   }
}

void sms_listen(){
   
   
      
      Serial.print("messageIndex: ");
      Serial.println(messageIndex);
     
      sim808.readSMS(messageIndex, message, MESSAGE_LENGTH, phone, datetime);
                 
      //***********In order not to full SIM Memory, is better to delete it**********
      sim808.deleteSMS(messageIndex);
      Serial.print("From number: ");
      Serial.println(phone); 
      Serial.print("Datetime: ");
      Serial.println(datetime);       
      Serial.print("Recieved Message: ");
      Serial.println(message);

      coord();

      sms_send();
      
    
}


void setup() {
   mySerial.begin(9600);
  Serial.begin(9600);
  phone = mio;

  //******** Initialize sim808 module *************
  while(!sim808.init())
  {
      Serial.print("Sim808 init error\r\n");
      delay(1000);
  }
  delay(3000);

  mySerial.print("AT+CLIP=1\r"); // turn on caller ID notification

  if( sim808.attachGPS())
      Serial.println("Open the GPS power success");
  else
      Serial.println("Open the GPS power failure");
     
  Serial.println("Init Success, please send SMS message to me!");

}

void loop() {
  if (state == 0){
    //Serial.println("ciao");
    la = 0.00;
    lo = 0.00;
    //sms_listen();
    unsigned long now = millis();
    while (millis() < now + interval) { //conta fino a interval
    ring();
     //*********** Detecting unread SMS ************************
       /*messageIndex = sim808.isSMSunread();
    
       //*********** At least, there is one UNREAD SMS ***********
      if (messageIndex > 0){
     sms_listen();
     }
    }
  }

  else if (state == 1){

    //ring();
     
    la_old = la;
    lo_old = lo;
  
    /*Serial.print("la_old = ");
    Serial.println(la_old);
    Serial.print("lo_old = ");
    Serial.println(lo_old);
    Serial.println(phone);    */
  
    coord();
  
    /*Serial.print("la = ");
    Serial.println(la);
    Serial.print("lo = ");
    Serial.println(lo);*/

    la_int = la;
    la_dec = la_int + ((la - la_int) * 100)/60;
    lo_int = lo;
    lo_dec = lo_int + ((lo - lo_int) * 100)/60;

    if (((la - la_old) > 0.01 || (la_old - la) > 0.01) && ((lo - lo_old) > 0.01 || (lo_old - lo) > 0.01)){

        sms_send();
    }

    //sms_listen();

    unsigned long now = millis(); //riprende il tempo
    
    while (millis() < now + interval) { //conta fino a interval
      ring();
      //*********** Detecting unread SMS ************************
       /*messageIndex = sim808.isSMSunread();
    
       //*********** At least, there is one UNREAD SMS ***********
      if (messageIndex > 0)
        {
        sms_listen();
      }*/
     }
    }
}

P.S.: i'd like also to let the system listen incoming text messages, if ones arrives it should give to me the GPS location, but if i insert sms_listen() function the system stops working (doesn't pass to one state to another), and if i load the program with that lines arduino IDE gives to me a waning about memory (80% full), could be this the problem?

I understood that GPS refresh inside state 1 was too quick and so the difference between current and previous location where never greater than 0.01, now i fix a location inside state 0 (la_park and lo_park) and then i compare it during state 1 (when car should be parked).
This is the new code

#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>
#define MESSAGE_LENGTH 160

char gprsBuffer[64]; //variabile per attivare l'ascolto delle chiamate in entrata
char *s = NULL;

char message[MESSAGE_LENGTH];//variabili per attivare l'ascolto degli sms in entrata
int messageIndex = 0;
char MESSAGE[300];
char lat[12];//variabili per la posizione
char lon[12];
float la, lo, la_park = 0.00, lo_park = 0.00, la_dec, lo_dec;
int la_int, lo_int;
 

char *phone;
char mio[] = "+393317943514";
char datetime[24];
bool state = 0; //variabile di stato
bool hasRun = 0, blinking = 0;

// Generally, you should use "unsigned long" for variables that hold time
unsigned long previousMillis = 0;        // will store last time LED was updated
// constants won't change:
long interval = 10000;           // interval at which to blink (milliseconds)

#define PIN_TX    3
#define PIN_RX    2
SoftwareSerial mySerial(PIN_TX,PIN_RX);
DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,

void coord(){
  sim808.getGPS();
  
    while(!sim808.getGPS())
    {
     //Serial.println("pippo");
    }
  
    la = sim808.GPSdata.lat;
    lo = sim808.GPSdata.lon;
}

void sms_send(){
        la_int = la;
        la_dec = la_int + ((la - la_int) * 100)/60;
        lo_int = lo;
        lo_dec = lo_int + ((lo - lo_int) * 100)/60;
        
        dtostrf(la_dec, 0, 4, lat); //put float value of la into char array of lat. 6 = number of digits before decimal sign. 2 = number of digits after the decimal sign.
        dtostrf(lo_dec, 0, 4, lon); //put float value of lo into char array of lon
        
     
        sprintf(MESSAGE, "Latitude : %s\nLongitude : %s\nMy Module Is Working.\nhttp://maps.google.com/maps?q=%s,%s\n", lat, lon, lat, lon);
        Serial.println(MESSAGE);       
    
         //phone = mio;
         Serial.println("Sim808 init success");
         Serial.println("Start to send message ...");
         Serial.println(MESSAGE);
         Serial.println(phone);
         sim808.sendSMS(phone,MESSAGE);
         //memcpy(phone[16], mio, sizeof(phone[16]));
         phone = mio;
}

void ring(){
//******** Wait serial data *************
   if(sim808.readable()){
      sim808_read_buffer(gprsBuffer,32,DEFAULT_TIMEOUT);
      //Serial.print(gprsBuffer);

   //************** Detect the current state of the telephone or SMS ************************
      if(NULL != strstr(gprsBuffer,"RING")) {
          sim808.answer();
          //mio = mySerial.read();
          //Serial.println(mySerial.read());
          if (state == 1){
            hasRun = 0;
            Serial.println(hasRun);}
          state = !state;                  
       }
    sim808_clean_buffer(gprsBuffer,32);
   }
   
}

void sms_listen(){
   
   
      
      Serial.print("messageIndex: ");
      Serial.println(messageIndex);
     
      sim808.readSMS(messageIndex, message, MESSAGE_LENGTH, phone, datetime);
                 
      //***********In order not to full SIM Memory, is better to delete it**********
      sim808.deleteSMS(messageIndex);
      Serial.print("From number: ");
      Serial.println(phone); 
      Serial.print("Datetime: ");
      Serial.println(datetime);       
      Serial.print("Recieved Message: ");
      Serial.println(message);

      coord();

      sms_send();
      
    
}


void setup() {
   mySerial.begin(9600);
  Serial.begin(9600);
  phone = mio;
  pinMode(13, OUTPUT);

  //******** Initialize sim808 module *************
  while(!sim808.init())
  {
      Serial.print("Sim808 init error\r\n");
      delay(1000);
  }
  delay(3000);

  mySerial.print("AT+CLIP=1\r"); // turn on caller ID notification

  if( sim808.attachGPS())
      Serial.println("Open the GPS power success");
  else
      Serial.println("Open the GPS power failure");
     
  Serial.println("Init Success, please send SMS message to me!");

}

void loop() {
  
  if (state == 0){
    //Serial.println("ciao");
    
    if(hasRun == 0){ 
    coord();
    
    la_park = la;
    lo_park = lo;
    Serial.print("la_park= ");
    Serial.println(la_park);
    Serial.print("lo_park= ");
    Serial.println(lo_park);
    Serial.println(hasRun);
    
    sms_send();
    
    hasRun = !hasRun;
    Serial.println(hasRun);}

    digitalWrite(13, LOW);
    
    //sms_listen();
    unsigned long now = millis();
    while (millis() < now + interval) { //conta fino a interval
    ring();
    //sms_listen();
    }
  }

  else if (state == 1){

    blinking = !blinking;
    digitalWrite(13, blinking);
    //la_old = la;
    //lo_old = lo;
  
    /*Serial.print("la_old = ");
    Serial.println(la_old);
    Serial.print("lo_old = ");
    Serial.println(lo_old);
    Serial.println(phone);    */
  
    coord();
  
    Serial.print("la - la_park = ");
    Serial.println(la - la_park);
    Serial.print("lo - lo_park = ");
    Serial.println(lo - lo_park);

    if (((la - la_park) > 0.01 || (la_park - la) > 0.01) && ((lo - lo_park) > 0.01 || (lo_park - lo) > 0.01)){

        sms_send();
    }

    //sms_listen();

    unsigned long now = millis(); //riprende il tempo
    
    while (millis() < now + interval) { //conta fino a interval
      ring();
      //Serial.println(hasRun);
      //*********** Detecting unread SMS ************************
       //messageIndex = sim808.isSMSunread();
    
       //*********** At least, there is one UNREAD SMS ***********
      if (messageIndex > 0)
        {
        sms_listen();
      }
     }
    }
}

it remains the FIRST PROBLEM: how can i read (and display) the phone number of the incoming call? I'd like to pass from one state to another only if certain numbers call, and how to get incoming sms response.