Control LED with SMS (SIM900 GSM shield)

Hello Guys,

I am absolutely newbie in Arduino field. Currently I am building a simple security system with SIM900 shield. I got stuck with controlling a LED with SMS messages. I managed to control the led by serial commands ( 'b' = on and 'k'=off). But I cannot manage to solve to change the led status by sending text messages to the GSM shield. (Of course SIM card is added. I have already been able to call remote numbers). Could you please help me what should be changed in the code? If I send a word like "arming" in SMS then I would like the led to turn on. I do not use a unique gsm library. Thank you for your help in advance.

Here is the code:

//#include <SoftwareSerial.h>
//SoftwareSerial SIM900(7, 8); // configure software serial port

char inchar; //Will hold the incoming character from the Serial Port.
 
int led = 13; 

void setup()
{
 
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
//Initialize GSM module serial port for communication.
Serial.begin(9600);
delay(3000); // give time for GSM module to register on network etc.

Serial.println("AT+CMGF=1\r"); // set SMS mode to text
delay(200);
Serial.println("AT+CSMS=1\r"); 
delay(200);

Serial.println("AT+CNMI=2,2,0,0,0"); // set module to send SMS data to serial out upon receipt 

delay(200);
Serial.println("AT+CMGD=1,4"); // delete all SMS
}

void loop() 
{

if(Serial.available() >0)
{  
delay(10);
inchar=Serial.read(); 
if (inchar=='k')   
{     
      digitalWrite(led, LOW);
            } 
else if (inchar=='b')
            {
      digitalWrite(led, HIGH);
            }
      delay(10);
            
            }      
             
}

study Serial Input Basics

(and you can also make use of the standard C functions to deal with c-strings stdlib.h and string.h)

ever heard about indenting code?

Hi J-M-L,

Thanks for your info. I'll go through on that study and will see if it can help. I haven't heardt about indenting codes yet.

szalaco:
I haven't heardt about indenting codes yet.

it means make sure brackets and lines are aligned properly :slight_smile:

You get that by pressing ctrl-T (or cmd-T on a Mac) in the IDE - you'll see that will change your life and make your code way more readable

it means make sure brackets and lines are aligned properly :slight_smile:

Yes I see, thanks for the hint :slight_smile:

I worked forward on my project without half success. So far I've managed to send SMS to the GSM shield and the serial monitor print that sent letter ('b') and the led turn on. But if I send the second SMS with the turning off letter ('k') then nothing happens :frowning:
Any idea pls? Please help me correcting the code. Hard to understand all steps without deep experience.
Thx in advance.
Code is on below.

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8); // configure software serial port

char inchar; //Will hold the incoming character from the Serial Port.

boolean newData = false;

int led = 13;

void setup()
{

  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
  Serial.begin(9600);
  delay(1000);
  SIM900.begin(9600); //Initialize GSM module serial port for communication.
  delay(3000); // give time for GSM module to register on network etc.

  SIM900.println("AT+CMGF=1\r"); // set SMS mode to text
  delay(200);
  SIM900.println("AT+CSMS=1\r");
  delay(200);

  SIM900.println("AT+CNMI=2,2,0,0,0"); // set module to send SMS data to serial out upon receipt

  delay(200);
  SIM900.println("AT+CMGD=1,4"); // delete all SMS
  delay(2000);
}

void loop()
{

  if (SIM900.available() > 0)
  {
    delay(100);
    inchar = SIM900.read();
    Serial.print(inchar);
    if (inchar == 'k')
    {
      delay(100);
      digitalWrite(led, LOW);
      delay(1000);
      SIM900.println("AT+CMGF=1\r"); // set SMS mode to text
      delay(300);
      SIM900.println("AT+CSMS=1\r");
      delay(300);

      SIM900.println("AT+CNMI=2,2,0,0,0"); // set module to send SMS data to serial out upon receipt

      delay(300);
      SIM900.println("AT+CMGD=1,4"); // delete all SMS
      delay(2000);
    }
    else if (inchar == 'b')
    {
      delay(100);
      digitalWrite(led, HIGH);
      delay(1000);
      SIM900.println("AT+CMGF=1\r"); // set SMS mode to text
      delay(300);
      SIM900.println("AT+CSMS=1\r");
      delay(300);

      SIM900.println("AT+CNMI=2,2,0,0,0"); // set module to send SMS data to serial out upon receipt

      delay(300);
      SIM900.println("AT+CMGD=1,4"); // delete all SMS
      delay(2000);
    }

  }

}

Your challenge is that your code is full of totally un-necessary delays and you send AT commands to your module to set it up but never read the answer from those AT commands so when you hit the loop you'll likely receive data that might not be SMS related...

have you studied the Serial Input Basics examples listed above? this is the right way to handle a serial port.

Sorry I was out for a few days. Thank you for your advices. I tried to understand the Serial Input Basics but I do not see what exactly I missed.

In the below code there is a read command (inchar = SIM900.read();). I thought that is enough for reading out the character I sent in SMS.

if (SIM900.available() > 0)
  {
    delay(100);
    inchar = SIM900.read();
    Serial.print(inchar);

Where else should I use reading commands? In the setup part?

My motion sensor security system works but I would like to turn it on/off remotely with a simple character in SMS. That is why I would need this part to be completed. Until it is not solved I cannot place the security system out. If I can control a led with an SMS then I can control a PIR sensor remotely easily.

Anyway thanks for the help so far.

write a small code that reads through all the stuff received on the SIM900 when you send a SMS

--> you'll see that before showing you your SMS content there are tons of other characters - something like +CMT (or something equivalent) and after that you have the mobile number of the sender and the date and then at the end is really the body of the SMS message

so basically if you send an SMS with just "k", what you'll receive is a flow of ASCII bytes like

+CMT: "+xxphoneNumberxxxx","","date,hour"\r\n[color=red]k[/color]\r\n

so you need to parse that the right way, detect the +CMT to ensure it's a command of interest, skip the rest of the line (could be just \n or \r and not both - you have to figure that out), get to the new line and here is your command --> hence understanding Serial Input Basics can help

Okay thanks again. Keeping fight with this forward :slight_smile:

I thought that is enough for reading out the character I sent in SMS.

The output from the AT command to read an SMS includes far more than the one character you might have sent as the message body.