Pages: [1]   Go Down
Author Topic: Induino R3 sms based led turn off not working  (Read 1010 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried the following program in Induino R3 to turn off an LED at pin 12. by sending an sms "offa". I used a gprs shield.
I got the following output in serial monitor. and the LED didn't turned OFF.
Can you please say what may be the problem?
code and output are attached
Code:
#include <SoftwareSerial.h>
// EN: String buffer for the GPRS shield message
String msg = String("");
// EN: Set to 1 when the next GPRS shield message will contains the SMS message
int SmsContentFlag = 0;
//control pins of relay.
int a=12;
// EN: Code PIN of the SIM card (if applied)
//String SIM_PIN_CODE = String( "XXXX" );
void setup()
{
  Serial.begin(19200);                 // the GPRS baud rate
  // Initialize  PINs
  pinMode( 12, OUTPUT );
  digitalWrite( 12, HIGH );
 Serial.println( "AT+CMGF=1" );
 delay(200);
}
void loop()
{
    char SerialInByte;
    if(Serial.available())
    {
        SerialInByte = (unsigned char)Serial.read();
       delay(5);
        // -------------------------------------------------------------------
        // EN: Program also listen to the GPRS shield message.
        // -------------------------------------------------------------------
       // EN: If the message ends with <CR> then process the message
        if( SerialInByte == 13 ){
          // EN: Store the char into the message buffer
          ProcessGprsMsg();
         }
         if( SerialInByte == 10 ){
            // EN: Skip Line feed
         }
         else {
           // EN: store the current character in the message string buffer
           msg += String(SerialInByte);
         }
     }
}
// EN: Make action based on the content of the SMS.
//     Notice than SMS content is the result of the processing of several GPRS shield messages.
void ProcessSms( String sms ){
  if( sms.indexOf("offa") >= 0 ){
    digitalWrite( a, LOW );
  }
}
// EN: Request Text Mode for SMS messaging
void GprsTextModeSMS(){
  Serial.println( "AT+CMGF=1" );
}
void GprsReadSmsStore( String SmsStorePos ){
  Serial.print( "AT+CMGR=" );
  Serial.println( SmsStorePos );
}
// EN: Clear the GPRS shield message buffer
void ClearGprsMsg(){
  msg = "";
}
// EN: interpret the GPRS shield message and act appropiately
void ProcessGprsMsg() {
  if( msg.indexOf( "Call Ready" ) >= 0 ){
     Serial.println( "*** GPRS Shield registered on Mobile Network ***" );
     GprsTextModeSMS();
  }
  // EN: unsolicited message received when getting a SMS message
  if( msg.indexOf( "+CMTI" ) >= 0 ){
    Serial.println( "*** SMS Received ***" );
     // EN: Look for the coma in the full message (+CMTI: "SM",6)
     //     In the sample, the SMS is stored at position 6
     int iPos = msg.indexOf( "," );
     String SmsStorePos = msg.substring( iPos+1 );
    Serial.print( "SMS stored at " );
   Serial.println( SmsStorePos );
     // EN: Ask to read the SMS store
     GprsReadSmsStore( SmsStorePos );
  }
  // EN: SMS store readed through UART (result of GprsReadSmsStore request)
  if( msg.indexOf( "+CMGR:" ) >= 0 ){
    // EN: Next message will contains the BODY of SMS
    SmsContentFlag = 1;
    // EN: Following lines are essentiel to not clear the flag!
    ClearGprsMsg();
    return;
  }
  // EN: +CMGR message just before indicate that the following GRPS Shield message
  //     (this message) will contains the SMS body
  if( SmsContentFlag == 1 ){
    Serial.println( "*** SMS MESSAGE CONTENT ***" );
    Serial.println( msg );
    Serial.println( "*** END OF SMS MESSAGE ***" );
    ProcessSms( msg );
  }
  ClearGprsMsg();
  // EN: Always clear the flag
  SmsContentFlag = 0;
}
Code:
AT+CMGF=1
*** SMS Received ***
SMS stored at 7
AT+CMGR=7
Logged

Forum Moderator
ਪੰਜਾਬ
Offline Offline
Edison Member
*****
Karma: 12
Posts: 1601
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

first of all define the int a = 12; as a constant, that is like:

Code:
const int a = 12;
(do this for all the pin and constant value declarations)

secondly, your IF condition on the function processSMS isn't working!
check whether you are reading the message in the correct way and use serial prints in the message prcosser to see that whether its even entering the IF condition or not and if yes what result it leaves behined.
Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

Pages: [1]   Go Up
Jump to: