SIM808: Same SMS received, although different SMS was sent from phone to GSM

Hello guys!

I have the following problem.

See the code below:

#include <SoftwareSerial.h>
SoftwareSerial gsm(2, 3);


int8_t answer;
int8_t answer2;

char frame[200];
char SMS[200];
int x;
unsigned long previous3;
String mystring;

void setup()  
{
  Serial.begin(9600);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  gsm.begin(9600);

    Serial.println("Connecting to the network...");
 
    
  while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) || 
            sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );

Serial.println("Connection established...");   

sendATcommand("AT+CMGF=1", "OK", 1000);    // sets the SMS mode to text    
sendATcommand("AT+CNMI=2,1,0,0,0", "OK", 2000);


sendATcommand("AT+CMGD=1", "OK", 2000);
sendATcommand("AT+CMGD=2", "OK", 2000);
 
}


void loop(){


read_SMS();  //reads SMS

if(millis() - previous3 > 10000){
  sendATcommand("AT+CMGDA=\"DEL ALL\"" , "OK", 2000);
  previous3 = millis ();
}
}


int8_t read_SMS(){
  answer2 = sendATcommand("AT+CMGR=1", "+CMGR:", 2000);
    if (answer2 == 1)
    {
        answer2 = 0;
        while(gsm.available() == 0);
        // this loop reads the data of the SMS
        do{
            // if there are data in the UART input buffer, reads it and checks for the asnwer
            if(gsm.available() > 0){    
                SMS[x] = gsm.read();
                x++;
                // check if the desired answer (OK) is in the response of the module
                if (strstr(SMS, "OK") != NULL)    
                {
                    answer2 = 1;
                }
            }
        }while(answer2 == 0);    // Waits for the asnwer with time out
        
        SMS[x] = '\0';
        Serial.println("Received message: ");
        Serial.println(SMS); 
        

    else
    {
        Serial.print("error ");
        Serial.println(answer, DEC);
    }    
}



  int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){

    uint8_t x=0,  answer=0;
    char response[100];
    unsigned long previous;

    memset(response, '\0', 100);    // Initialize the string
    
    delay(100);
    
    while( gsm.available() > 0) gsm.read();    // Clean the input buffer
    
    gsm.println(ATcommand);    // Send the AT command 


    x = 0;
    previous = millis();

    // this loop waits for the answer
    do{
        if(gsm.available() != 0){    
            // if there are data in the UART input buffer, reads it and checks for the asnwer
            response[x] = gsm.read();
//            positions[x]=response[x];
            x++;
            // check if the desired answer  is in the response of the module
            if (strstr(response, expected_answer) != NULL)    
            {
                answer = 1;
            }
        }
         // Waits for the asnwer with time out
    }while((answer == 0) && ((millis() - previous) < timeout));   
    
    return answer;
}

In main loop there is only function read_SMS(); which continuously checks if new MSG was received.

When I send my first SMS after Arduino was reset, it reads the first msg just fine, then after 10 seconds it deletes it from memory. After that I send my 2nd SMS and here is where problem occurs. Actually 2nd msg was received and stored, but the SMS array stayed the same, so I still read the same 1st msg.

Example: my first msg from my cell phone to GSM module:

Received message:
“REC UNREAD”,"+3**********","",“16/06/22,11:31:31+12”
Hello

my second msg was “Bye” but it still displayed same thing above. It keeps going for all messages afterwards (i.e. third, fourth etc.) till I reset my Arduino.

Is there any special way I should clear out SMS parameter on each cycle? Thank you in advance!

buenas noches soi mui nuevo en esto del arduino y qisiera pedirles el fabor de guiarme en lo siguiente. e comprado un SIM808 shield y lo qiero usar para poner 4 LED en estado on o off por medio de mensages de wassap y q me devuelva un mensage cada q se le de una orden si alguen tiene la libreria completa y me puede ayudar muchas gracias buena noche...

buenas noches soi muí nuevo en esto del arruino y quisiera pedirles el favor de guiarme en lo siguiente. e comprado un SIM808 shield y lo quiero usar para poner 4 LED en estado on o off por medio de mensajes de wassap y q me devuelva un mensaje cada q se le de una orden si alaguen tiene la libreria completa y me puede ayudar muchas gracias buena noche

fabiojimenez; no puedes usar wassap pero puedes usar mensajes de texto o instrucciones por internet en youtube he visto varios videos asi soy nuevo y no puedo ayudarte mas. https://www.youtube.com/watch?v=2pOUQB_Cvpw