Arduino Forum

Products => Arduino GSM Shield => Topic started by: Cerro on Sep 04, 2015, 10:37 am

Title: GSM Shield receive an SMS and reply
Post by: Cerro on Sep 04, 2015, 10:37 am
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... :(

Here is my code.

Code: [Select]

#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);
}
Title: Re: GSM Shield receive an SMS and reply
Post by: dannable on Sep 04, 2015, 12:06 pm
Please modify your post to format your code using code tags.

See the topic 'How to use this forum'.
Title: Re: GSM Shield receive an SMS and reply
Post by: Cerro on Sep 04, 2015, 12:50 pm
Please modify your post to format your code using code tags.

See the topic 'How to use this forum'.
Done. Thank you!
Title: Re: GSM Shield receive an SMS and reply
Post by: dannable on Sep 04, 2015, 01:15 pm
You don't need to execute the command

Code: [Select]
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?

Title: Re: GSM Shield receive an SMS and reply
Post by: Cerro on Sep 04, 2015, 01:32 pm
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.
Title: Re: GSM Shield receive an SMS and reply
Post by: dannable on Sep 04, 2015, 01:37 pm
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?
Title: Re: GSM Shield receive an SMS and reply
Post by: Cerro on Sep 04, 2015, 01:44 pm
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.
Title: Re: GSM Shield receive an SMS and reply
Post by: dannable on Sep 04, 2015, 01:59 pm
When you:

Code: [Select]
if(SIM900.available()>0){
   inchar=SIM900.read();


print the value of inchar to screen to see what it has read? It helps with debugging.
Title: Re: GSM Shield receive an SMS and reply
Post by: Cerro on Sep 04, 2015, 02:03 pm
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.
Title: Re: GSM Shield receive an SMS and reply
Post by: nikhiljoji on Sep 17, 2015, 10:19 am
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 :)
All the best

http://arduinoisfun.blogspot.in/2015/04/receive-message-with-arduino-with.html

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

Title: Re: GSM Shield receive an SMS and reply
Post by: IsabelSchmidt on May 20, 2019, 05:55 pm
You shoud take a proper course on it to understand it properly. Going to the following website edu birdy review (https://edubirdie.org/edubirdie-review-the-most-honest-and-reliable/) might help you in better understand it.
Title: Re: GSM Shield receive an SMS and reply
Post by: SharrySteve1 on Jul 19, 2019, 01:02 am
You shoud take a proper course on it to understand it properly. Going to the following website (http://www.essayarsenal.co.uk/write-my-essay-for-money.aspx) edu birdy review (https://edubirdie.org/edubirdie-review-the-most-honest-and-reliable/) might help you in better understand it.

:) Done
Title: Re: GSM Shield receive an SMS and reply
Post by: Vellut on Sep 01, 2019, 12:02 pm
If you enjoy betting on games with the same players of a given sport as the ones you are playing, try out our Sports Betting Book and choose your favourite predictions  (https://betjedi.net/)sports to bet on. For a comprehensive guide to all the main betting options, pick up a copy of Betfair Sportsbook which provides a complete betting list for more than two million sports and over 50,000 sports book categories.We are happy to be used by those who wish not to get bogged down with the ever-changing regulations governing the betting trade. I