Arduino Forum

Products => Arduino GSM Shield => Topic started by: MOHD521 on Nov 27, 2015, 08:31 pm

Title: Unable to read any SMS's other than the first SMS using SIM908 shield
Post by: MOHD521 on Nov 27, 2015, 08:31 pm
I'm using two SIM908 shield to communicate with each other. One of the shields will be connected to a sensor and it should send values contentiously to the second SIM908 (receiver). I'm facing a problem on the receiver side, I'm only able to receive the first SMS and when sending a new SMS it displays the first one.

The code that I'm using is from (cooking-hacks.com) where I bought the shield. In the original code the instructions were in void setup function so I moved them to void loop function to keep checking for new SMS and print any received values. I also made other minor changes. Here is the code

Code: [Select]
int8_t answer;
int x;
int onModulePin= 2;
char aux_string[30];
char SMS[200];
float fSMS;

char data[20];

void setup(){

pinMode(onModulePin, OUTPUT);
Serial.begin(115200); 

Serial.println("Starting...");
power_on();





}


void loop(){

Serial.println("Setting SMS mode...");
sendATcommand("AT+CMGF=1", "OK", 1000);    // sets the SMS mode to text
sendATcommand("AT+CPMS=\"SM\",\"SM\",\"SM\"", "OK", 1000);    // selects the memory

answer = sendATcommand("AT+CMGR=1", "+CMGR:", 2000);    // reads the first SMS (since all other messages are deleted from previous delete statment)
if (answer == 1)
{
    answer = 0;
    while(Serial.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(Serial.available() > 0){   
            SMS[x] = Serial.read();
            x++;
            // check if the desired answer (OK) is in the response of the module
            if (strstr(SMS, "OK") != NULL)   
            {
                answer = 1;
            }
        }
    }while(answer == 0);    // Waits for the asnwer with time out

    SMS[x] = '\0';


/*  strtok(SMS, ",");
strtok(NULL, ",");
strtok(NULL, ",");
strtok(NULL, "\n");
strcpy(data,strtok(NULL, ""));*/

    Serial.println(SMS);   

    //Serial.println(data);

/*    fSMS= atof (data);
    Serial.println(fSMS, 5);*/

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


Serial.println("AT+CMGDA= \"DEL ALL\"");//delete all messages from SIM card


delay(5000);
 }

void power_on(){

uint8_t answer=0;

// checks if the module is started
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
    // power on pulse
    digitalWrite(onModulePin,HIGH);
    delay(3000);
    digitalWrite(onModulePin,LOW);

    // waits for an answer from the module
    while(answer == 0){     // Send AT every two seconds and wait for the answer
        answer = sendATcommand("AT", "OK", 2000);   
    }
}

}

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);    // Initialice the string

delay(100);

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

Serial.println(ATcommand);    // Send the AT command


    x = 0;
previous = millis();

// this loop waits for the answer
do{
    // if there are data in the UART input buffer, reads it and checks for the asnwer
    if(Serial.available() != 0){   
        response[x] = Serial.read();
        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;
}
Title: Re: Unable to read any SMS's other than the first SMS using SIM908 shield
Post by: dannable on Nov 27, 2015, 09:11 pm
I may be wrong, but you appear to be trying to talk to both the SIM908 AND the serial monitor through D0 & D1?

See http://forum.arduino.cc/index.php?topic=299558.0 (http://forum.arduino.cc/index.php?topic=299558.0)

Isn't a SIM908 overkill to just receive SMS?
Title: Re: Unable to read any SMS's other than the first SMS using SIM908 shield
Post by: MOHD521 on Nov 27, 2015, 10:02 pm
I apologies , I should have been more clear regarding my project. I will be using the first SIM shield (sender) which will be placed in a vehicle to send its coordinate to the second SIM shield (receiver). I was able to send the coordinates contentiously to my mobile phone. However, I'm not able to contentiously receiver this data on the receiver side. I need to use the coordinates to calculate the distance between the two shields.

Now regarding the serial communication, yes you're right regarding my use of D0 & D1. I didn't know that this may cause an issue ( I'm fairly new at using arduino). Is it possible to solve this issue by using an Arduino Mega where D0 & D1 will communicate with the shield and use other serial communication pins such as pin 14 and 15 to communication with the serial monitor?
Title: Re: Unable to read any SMS's other than the first SMS using SIM908 shield
Post by: dannable on Nov 28, 2015, 02:03 pm
Yes, that is one option. But you would have to use Serial to communicate with the Serial Monitor, and Serial1 to communicate with the shield?