4

I am using Arduino GSM Shield receiving SMS from an Android app. And the content of this SMS will control a LED. If the content of this SMS is not "off", the LED will be on and the content will be printed in the serial monitor. But if it is "off", the LED will be off immediately. Besides, the LED will keep being on until the "off" message coming. For now, I used the code from the example of the software. But I cannot use the content of this SMS to control the status of LED. With the code below, the LED could not be turned on and the content could not be displayed on the monitor. I think it was because the sketch failed to get the whole content of this SMS. Could anybody tell me how to solve this problem? Thanks.

#include <GSM.h>
GSM gsmAccess;
GSM_SMS sms;
char senderNumber[20];  
int led=13;
void setup() 
{
  Serial.begin(9600);
  pinMode(led,OUTPUT);
  digitalWrite(led,LOW);
  while (!Serial) {
    ;
  } 
  Serial.println("SMS Messages Receiver");
  boolean notConnected = true;
  while(notConnected)
    {
      if(gsmAccess.begin("6442")==GSM_READY)
        notConnected = false;
      else
        {
          Serial.println("Not connected");
          delay(1000);
        }
    }  
  Serial.println("GSM initialized");
  Serial.println("Waiting for messages");
}

void loop() 
{
  char c;
  int val=0;
  val=digitalRead(led);
  if (val==HIGH){
    digitalWrite(led,HIGH);
  }
  if (sms.available()){
    Serial.println("Message received from:");
    sms.remoteNumber(senderNumber, 20);
    Serial.println(senderNumber);

    if(sms.peek()=='#')
      {
        Serial.println("Discarded SMS");
        sms.flush();
      }
    while(c=sms.read())   
      if(c='off'){
        digitalWrite(led,LOW);
      }else{
        digitalWrite(led,HIGH);
        Serial.print(c);
      }
    Serial.println("\nEND OF MESSAGE");
    sms.flush();
    Serial.println("MESSAGE DELETED");
  }
  delay(1000);
}

migrated from electronics.stackexchange.com Jun 10 '14 at 15:39

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • 1
    If the below answer actually helped you in fixing your problem, please mark it as answered/correct. Also, it would help others if you could edit your question to reflect the changes you have made to get it working. – Madivad Jun 11 '14 at 16:40
  • first i want to know where did u add "smsData[smsIndex++] = c; smsData[smsIndex] = '\0';" ? – user2788 Jun 12 '14 at 16:22
  • @user2788 I added it below while, and then use strcmp(smsData,"off")==0 to check whether the content of this sms is "off". – Zhu Jun 13 '14 at 6:59
4

First of all, you have a typo: You need the double-equals operator in

if (c == 'off') {

But more importantly, you can't use a char variable to hold or compare more than one character at a time. I don't know the details of the Arduino GSM library, but if sms.read() returns a string, then you'll need a string variable or an array of char to hold the result.

  • I succeeded with adding "smsData[smsIndex++] = c; smsData[smsIndex] = '\0';". Thanks for your answer. – Zhu Jun 10 '14 at 11:27
1

sms.read() function returns only a single byte of the SMS string. You can store the whole data in a character array and process it later like following:

char c, message[140];
int i=0;
while (c = sms.read()) {
    message[i++] = c;
}
if (strstr (message, "off")) {    //If SMS contains off anywhere
    digitalWrite(led,LOW);
} else {
    digitalWrite(led,HIGH);
    Serial.print(message);
}

And I believe the following portion in your code is not required as it is doing nothing:

val=digitalRead(led);
if (val==HIGH){
    digitalWrite(led,HIGH);
}

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