GSM Shield receive an SMS and reply

Hello!
I am currently using Arduino Uno and a compatible GSM Shield.
I am trying to receive an SMS with some characters("#ok" in my code), analyze these characters, and if they match with what Arduino is expecting(#ok), Arduino must control the state of a digital port and send an SMS to a defined number.

Please someone can help me? I am getting nowhere... :frowning:

Here is my code.

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);


int checkPin = 10;
char inchar;
bool check1=LOW;
bool input;

void setup()
{
 pinMode(checkPin,INPUT);
 SIM900.begin(19200); 
 delay(7000);
 SIM900.print("AT+CMGF=1\r");                                                        
 delay(100);
}

bool receiveSMS(){
 SIM900.print("AT+CNMI=2,2,0,0,0\r");
 delay(100);
 if(SIM900.available()>0){
   inchar=SIM900.read();
   if(inchar=='#'){
     delay(10);
     inchar=SIM900.read();
     if(inchar=='o'){
       delay(10);
       inchar=SIM900.read();
       if(inchar=='k'){
         return true;
         }
       }
     SIM900.println("AT+CMGD=1,4");
     }else{
       return false;
       }
   } 
}


void sendSMS()
{
 SIM900.println("AT + CMGS = \"+393272275551\"");                                   
 delay(100);
 check1 = check();
 if(check1==true){
     SIM900.println("Button pressed!");       
     delay(100);
     SIM900.println((char)26);                      
     delay(100); 
     SIM900.println();
     delay(5000);
     }else{
           SIM900.println("Button not pressed!");        
           delay(100);
           SIM900.println((char)26);                     
           delay(100); 
           SIM900.println();
           delay(5000);
     }
}


bool check(){
 
 if(digitalRead(checkPin)==HIGH){
   return true;
   }else{
     return false;}
 
}

void loop()
{
 input = receiveSMS();
 if(input == true){
 sendSMS();
 }
 do {} while (1);
}

Please modify your post to format your code using code tags.

See the topic 'How to use this forum'.

dannable:
Please modify your post to format your code using code tags.

See the topic 'How to use this forum'.

Done. Thank you!

You don't need to execute the command

SIM900.print("AT+CNMI=2,2,0,0,0\r");

more than once, so move it to set up.

So, running through your code by hand. We run setup() to configure everything, fine. Then we run loop(). This calls the function receiveSMS(). Let's assume that there's nothing there at the moment - SIM900.available()==0 - so it drops through and returns to loop(). Then we have an infinite loop....

If there is something available, perhaps information returned from the previous commands, and the first character isn't a '#', then drop through and return to loop() - as above.

Perhaps what you want to do is if there is something to read, read it, and if '#' then process. If not, discard and wait for the next character to appear?

dannable:
If there is something available, perhaps information returned from the previous commands, and the first character isn't a '#', then drop through and return to loop() - as above.

Perhaps what you want to do is if there is something to read, read it, and if '#' then process. If not, discard and wait for the next character to appear?

Basically, I think you understood right. I send an SMS from another phone with the text: #ok.
It reads the stream from the shield(I guess, because I don't figured out perfectly how it works), if the stream contains '#', keep reading. If the next character is 'o', keep reading and the same thing for the 'k'. Once it recognizes that the last character was 'k', it means it received #ok and return true.
If input == true, the loop() calls sendSMS(), which, internally does a control on the pin state(LOW or HIGH) and communicates the state via SMS to the given number.

Yes, once you have a '#', then check the next is 'o' and then the next is 'k'. Then set the flag. Otherwise, keep reading! But don't forget to allow for, say, '##ok' coming through

You could always echo the character read to the screen to see what is happening?

dannable:
You could always echo the character read to the screen to see what is happening?

I'm sorry but I think I didn't understand your question.
I think the problem is in receiving and interpreting the sms, because the sending part works, already tested.

When you:

 if(SIM900.available()>0){
   inchar=SIM900.read();

print the value of inchar to screen to see what it has read? It helps with debugging.

Ok.
No, I haven't done it until now. I'll try a little bit later, because I have not the possibility to use a second SIM card now.
I will update you as soon as possible.

Hi ,
You will possibly need two changes in your program

SIM900.print("AT+CNMI=2,2,0,0,0\r");

move this command to the void setup() loop

and to find the "#ok" in the receivng sms you can use "indexof" command

try this
String inchar;
if(SIM900.available()>0)
{
inchar=SIM900.read();
if(inchar.indexOf("#ok") >=0) // this command will check if your messgae received has got #ok in it .

Hope this post is helpful :slight_smile:
All the best

check this link also .. this wil help you receive sms and once you received you just use that indexof command..