5

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);
}
3
  • 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
    Commented Jun 11, 2014 at 16:40
  • first i want to know where did u add "smsData[smsIndex++] = c; smsData[smsIndex] = '\0';" ?
    – user2788
    Commented Jun 12, 2014 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
    Commented Jun 13, 2014 at 6:59

2 Answers 2

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.

1
  • I succeeded with adding "smsData[smsIndex++] = c; smsData[smsIndex] = '\0';". Thanks for your answer.
    – Zhu
    Commented Jun 10, 2014 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.