Go Down

Topic: GSM Shield receive an SMS and reply (Read 4528 times) previous topic - next topic

Cerro

Sep 04, 2015, 10:37 am Last Edit: Sep 04, 2015, 12:44 pm by Cerro
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);
}

dannable

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

See the topic 'How to use this forum'.
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

Cerro

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

See the topic 'How to use this forum'.
Done. Thank you!

dannable

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?

Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

Cerro

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.

dannable

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?
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

Cerro

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.

dannable

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.
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

Cerro

#8
Sep 04, 2015, 02:03 pm Last Edit: Sep 04, 2015, 02:04 pm by Cerro
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.

nikhiljoji

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..


IsabelSchmidt

You shoud take a proper course on it to understand it properly. Going to the following website edu birdy review might help you in better understand it.

SharrySteve1

You shoud take a proper course on it to understand it properly. Going to the following website edu birdy review might help you in better understand it.

:) Done

Vellut

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 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

Go Up