Hi everyone, currently I build a security system for my home using a GSM shield with SIM900.
My objective is to control my system with SMS.
I made various searches on the use of SIM900 and I succed to send SMS but my problem is for receiving SMS. In fact I can received the first SMS properly but when I send an other SMS to the shield I read only the first SMS. And it doesn't matter the number of SMS I send to the SIM900 I only read the first SMS.
I have tried to delete all messages after each SMS but the problem is still the same...
My program is :
int x;
int8_t answer;
int onModulePin= 9;
char aux_string[30];
char SMS[200];
String test = "";
int count=0;
String nop = "NO";
String mdp = "PASSWORD";
void setup() {
pinMode(onModulePin, OUTPUT);
Serial.begin(115200);
power_on();
delay(3000);
sendATcommand(aux_string, "OK", 2000);
delay(3000);
while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) ||
sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 ); //Tant que l'on n'est pas connecté au réseau
sendATcommand("AT+CNMI=1,1,", "+CMTI:\"SM\",2", 1000);
sendATcommand("AT+CMGF=1", "OK", 1000);//Mode SMS texte
sendATcommand("AT+CMGDA=\"DEL ALL\"", "OK", 6000); //Supprime tous les messages de la carte
sendATcommand("AT+CPMS=\"SM\",\"SM\",\"SM\"", "OK", 1000); //Selectionne la mémoire
send_SMS("DEMARRAGE");
}
void loop() {
test = read_SMS();
if(test.equals(mdp)){
Serial.println("CODE BON");
send_SMS("ALARME ACTIVEE");
sendATcommand("AT+CMGDA=\"DEL ALL\"", "OK", 6000); //Supprime tous les messages de la carte
if(test.equals(nop)){ }
else{
Serial.println("WRONG PASSWORD");
sendATcommand("AT+CMGDA=\"DEL ALL\"", "OK", 6000); //Supprime tous les messages de la carte
read_SMS() = "";
}
delay(500);
}
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);
}
}
}
String read_SMS(){
answer = sendATcommand("AT+CMGR=1,0", "+CMGR:", 200); // reads the first SMS
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';
Serial.println(SMS);
}
else
{
return(nop);
}
}
int8_t send_SMS(char* sms_text){
sprintf(aux_string,"AT+CMGS=\"%s\"", "+33XXXXXXXXX");
answer = sendATcommand(aux_string, ">", 2000); // send the SMS number
if (answer == 1)
{
Serial.println(sms_text);
Serial.write(0x1A);
answer = sendATcommand("", "OK", 20000);
if (answer == 1)
{
Serial.print("Sent ");
}
else
{
Serial.print("error ");
}
}
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); // 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;
}
And when I send a first SMS an an other after I obtain that :
Sent AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
"REC UNREAD","+33XXXXXXXXX","","17/02/19,17:32:54+04" // The first SMS which I sent, everything goes well
I send a first SMS
OK
WRONG PASSWORD
AT+CMGDA="DEL ALL"
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
AT+CMGR=1,0
"REC UNREAD","+33XXXXXXXXX","","17/02/19,17:32:54+04" // I have sent an other SMS with a different text...
I send a first SMS
OK
So if someone have an idea...
See you soon !!