3

I want to read the SMS using Arduino and GSM module, I want to perform conditional programming based on the received SMS.

What I found really interesting is SMS format is different if I send message from different phones.

Output of my code is

Case 1:(From a smartphone)

Mobile Number is: +91954xxxxxxx Message Text: 5+22" Hello Arduino

Case 2:(From a normal phone)

Mobile Number is: +91984yyyyyyy Message Text: Hello Arduino


Does it really depend on the sender's mobile phone? If yes, how to move further to make my code independent on the sender mobile.


My code is

//===final code

char RcvdMsg[200] = "";
int RcvdCheck = 0;
int RcvdConf = 0;
int index = 0;
int RcvdEnd = 0;
char MsgMob[15];
char MsgTxt[50];
int MsgLength = 0;

void Config() // This function is configuring our SIM900 module i.e. sending the initial AT commands
{
   delay(1000);
   Serial.print("ATE0\r");
   Response();
   Serial.print("AT\r");
   Response();
   Serial.print("AT+CMGF=1\r");
   Response();
   Serial.print("AT+CNMI=1,2,0,0,0\r");
   Response();
}


void setup()
{
   Serial.begin(9600);
   Config();
}

void loop()
{
   RecSMS();
}


void Response() // Get the Response of each AT Command
{
   int count = 0;
   Serial.println();
   while(1)
   {
      if(Serial.available())
      {
         char data =Serial.read();
         if(data == 'K'){Serial.println("OK");break;}
         if(data == 'R'){Serial.println("GSM Not Working");break;}
      }
      count++;
      delay(10);
      if(count == 1000){Serial.println("GSM not Found");break;}

   }
}

void RecSMS() // Receiving the SMS and extracting the Sender Mobile number & Message Text
{
   if(Serial.available())
   {
      char data = Serial.read();
      if(data == '+'){RcvdCheck = 1;}
      if((data == 'C') && (RcvdCheck == 1)){RcvdCheck = 2;}
      if((data == 'M') && (RcvdCheck == 2)){RcvdCheck = 3;}
      if((data == 'T') && (RcvdCheck == 3)){RcvdCheck = 4;}
      if(RcvdCheck == 4){RcvdConf = 1; RcvdCheck = 0;}

      if(RcvdConf == 1)
      {
         if(data == '\n'){RcvdEnd++;}
         if(RcvdEnd == 3){RcvdEnd = 0;}
         RcvdMsg[index] = data;

         index++;
         if(RcvdEnd == 2){RcvdConf = 0;MsgLength = index-2;index = 0;}
         if(RcvdConf == 0)
         {
            Serial.print("Mobile Number is: ");
            for(int x = 4;x < 17;x++)
            {
               MsgMob[x-4] = RcvdMsg[x];
               Serial.print(MsgMob[x-4]);
            }
            Serial.println();
            Serial.print("Message Text: ");
            for(int x = 46; x < MsgLength; x++)
            {
               MsgTxt[x-46] = RcvdMsg[x];
               Serial.print(MsgTxt[x-46]);
            }

            Serial.println();

            //****Resetting all the variables*****//

            //RcvdMsg[200]={0};
            RcvdCheck = 0;
            RcvdConf = 0;
            index = 0;
            RcvdEnd = 0;
            MsgMob[15];
            MsgTxt[50];
            MsgLength = 0;

            Serial.flush();


         }
      }
   }
}
1

If you're not trying to capture the mobile number or whatever SMS text was sent, but only react to specific SMS messages, then don't try to parse the whole message, but design the protocol between any arbitrary phone and your Arduino to be detectable independent of everything else, e.g.:

#L 5 on        ; set an LED state<br>
#M 3 137       ; set a motor speed<br>

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.