How to send the state of an LED via SMS

Hi,

I would like to find out how do I send the state of an LED in arduino via an SMS. So if the LED is ON I want to send and SMS saying the LED is ON and when Its OFF I want to send an SMS saying the LED is OFF. This is my code I have:

Any type of help will be appreciated.

[code][code]#include <SoftwareSerial.h>

SoftwareSerial SIM900(2, 3); // RX, TX

char incoming_char=0; //Will hold the incoming character from the Serial Port.
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
int led_status = 0;     // variable to store the led status

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  
  Serial.begin(19200); // set the baud rate
  SIM900.begin(19200); // for GSM shield
  delay(20000);  // give time to log on to network.
  SIM900.print("AT+CMGF=1\r");  // set SMS mode to text
  delay(100);
  SIM900.println("AT+CNMI=2,2,0,0,0\r"); 
  SIM900.print("AT+CLIP=1\r"); // turn on caller ID notification
  // blurt out contents of new SMS upon receipt to the GSM shield's serial out
  delay(100); 
  digitalWrite(led, LOW);  // Set led to LOW
 // Serial.println("AT+CMGD=1,4");  //Delete all SMS in box
}

void sendSMS(char led_status){               //SEND SMS

  SIM900.print("AT+CMGF=1\r");                     // AT command to send SMS message
  delay(1000);
  SIM900.println("AT + CMGS = \"+2772XXXXXXX\"");  // recipient's mobile number, in international format
  delay(1000);
  SIM900.println( " LED STATUS: ON ");                         // message to send
  delay(1000);
  SIM900.println((char)26);                        // End AT command with a ^Z, ASCII code 26
  delay(1000); 
  SIM900.println();
  delay(5000);     // give module time to send SMS  
}

// the loop routine runs over and over again forever:
void loop() {
 //If #a1b1c1d1 comes as sms, all led's should light up.
 if(SIM900.available() >0)
 {
 incoming_char=SIM900.read(); 
 if (incoming_char=='#')
   {
   delay(10);
   incoming_char=SIM900.read(); 

 //first led
   if (incoming_char=='a')
     {
   delay(10);
   incoming_char=SIM900.read();

 if (incoming_char=='0')
   {
   digitalWrite(led, LOW);
   } 
 else if (incoming_char=='1')
   {
   digitalWrite(led, HIGH);
   }
 else if (incoming_char=='S')
   {
   digitalRead(led);
   led_status=digitalRead(led);
   Serial.print(led_status);  // prints status on serial terminal
   sendSMS(led_status);
   }
 delay(10);
      }
    }
 }
}

[/code][/code]

Please read the post How to Use This Forum and mark your code up using the '#' key.

Hi Dan,

I have added the code tag using #

First of all, a couple of things. Either put the opening brace '{' on a line of its own, or don't. Don't mix the the two styles. And the Auto-format feature is your friend. Use it - a lot.

Anyway, apart from that your code works perfectly on my setup. I'm using an Uno R3 and a Seeedstudio SIM900 shield.

#a0 turns the led on pin 13 off
#a1 turns the led on pin 13 on
#aS reports the status of the led pin to serial monitor. Probably just sent you an SMS too as I didn't change the destination number!

So you might want to anonymise your number if it is yours, and I should have asked earlier - what shield and which Arduino are you using? Link to shield please.

Hi Dan,

I;m also using the UNO R3 board. For the GSM module I'm using :

http://www.otto.co.za/products/industrial-products/module-development-kits/sim900-development-kit-for-sim900-or-sim900d-modules/

The code works as it sends the SMS, however even if the LED if OFF it still says LED ON. I was thinking of modifying the code a bit to this:

void sendSMS(char led_status){               //SEND SMS

  SIM900.print("AT+CMGF=1\r");                     // AT command to send SMS message
  delay(1000);
  SIM900.println("AT + CMGS = \"+277XXXXXXXX\"");  // recipient's mobile number, in international format
  delay(1000);
  SIM900.println( " LED STATUS: "+ led_status);                         // message to send
  delay(1000);
  SIM900.println((char)26);                        // End AT command with a ^Z, ASCII code 26
  delay(1000); 
  SIM900.println();
  delay(5000);     // give module time to send SMS

I will test this later and let you know of the results.

Hi Dan,

I have tried the code again but did not work as desired, I'm still not able to send the status of the LED via SMS

I'm at work now so I'm unable to test your changes.

You could try changing the routine parameter:

void sendSMS(byte led_status)

and then

if (led_status==0)
{
	SIM900.println( " LED STATUS: OFF");
}
else
{
	SIM900.println( " LED STATUS: ON");
}

Send it to Serial Monitor first to see if it works.

Hi Dan,

This work fine. thanks for the help much appreciated.

Hi Dan,

I modified my code yesterday to be able to turn ON/OFF via a relay. I’m able to turn ON/OFF and get status of the LED via SMS.

I have an issue, when I upload the code onto the Arduino UNO Rev3, the LED comes ON for a few seconds and then goes OFF. I don’t like this behaviour. I have gone through the code to see where I’m going wrong.

RELAY: http://www.communica.co.za/Catalog/Details/P1337860176

#include <SoftwareSerial.h>

SoftwareSerial SIM900(2, 3); // RX, TX

char incoming_char=0; //Will hold the incoming character from the Serial Port.
 
// Pin 8 has an LED connected on most Arduino boards.
int led = 8;
int led13 = 13;
int led_status = 0;     // variable to store the led status

/*-----( Declare Constants )-----*/
#define RELAY_ON 0
#define RELAY_OFF 1

/*-----( Declare variables )-----*/
#define Relay_1  8  // Arduino Digital I/O pin number
#define Relay_2  7
#define Relay_3  6
#define Relay_4  5

  // the setup routine runs once when you press reset:
void setup() {                
  
  //-------( Initialize Pins so relays are inactive at reset)----
  
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF); 
  
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(led13, OUTPUT);
  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT);  
  pinMode(Relay_3, OUTPUT);  
  pinMode(Relay_4, OUTPUT);  
  
  Serial.begin(19200); // set the baud rate
  SIM900.begin(19200); // for GSM shield
  delay(20000);  // give time to log on to network.
  Serial.println("setup Section");
  SIM900.print("AT+CMGF=1\r");  // set SMS mode to text
  delay(100);
  SIM900.println("AT+CNMI=2,2,0,0,0\r"); 
  SIM900.print("AT+CLIP=1\r"); // turn on caller ID notification
  // blurt out contents of new SMS upon receipt to the GSM shield's serial out
  delay(100); 
  digitalWrite(led, LOW);  // Set led to LOW
  digitalWrite(led13, LOW);  // Set led to LOW
 // Serial.println("AT+CMGD=1,4");  //Delete all SMS in box
}

void sendSMS(byte led_status){               //SEND SMS

  SIM900.print("AT+CMGF=1\r");                     // AT command to send SMS message
  delay(1000);
  SIM900.println("AT + CMGS = \"+277XXXXXXX\"");  // recipient's mobile number, in international format
  delay(1000);
if (led_status==0)
    {
      SIM900.println( " LIGHT STATUS: OFF");
      }
else
    {
      SIM900.println( " LIGHT STATUS: ON");
    }
  delay(1000);
  SIM900.println((char)26);                        // End AT command with a ^Z, ASCII code 26
  delay(1000); 
  SIM900.println();
  delay(5000);     // give module time to send SMS  
}

// the loop routine runs over and over again forever:
void loop() {
 
 if(SIM900.available() >0)
 {
 incoming_char=SIM900.read(); 
 if (incoming_char=='#')
   {
   delay(10);
   incoming_char=SIM900.read(); 

 //first led
   if (incoming_char=='a')
     {
   delay(10);
   incoming_char=SIM900.read();

 if (incoming_char=='0')
   {
   digitalWrite(led, LOW);
   digitalWrite(Relay_1, LOW);// set the Relay OFF
   delay(1000);              // wait for a second
   } 
 else if (incoming_char=='1')
   {
   digitalWrite(led, HIGH);
   digitalWrite(Relay_1, HIGH);// set the Relay ON
   delay(1000);              // wait for a second
   }
 else if (incoming_char=='S')
   {
   digitalRead(led);
   led_status=digitalRead(led);
   Serial.print(led_status);  // prints status on serial terminal
   sendSMS(led_status);
   }
 delay(10);
      }
    }
 }
}

You could try initialising the pins before writing to them?

And if we are going for consistency, you don't appear to initialise pins 'led' or 'led13'.

Hi Dan,

In the void Setup () I have this defined:

  digitalWrite(led, LOW);  // Set led to LOW
  digitalWrite(led13, LOW);  // Set led to LOW

Is this not OK?

Sorry, you do have them set, I missed it. It's still rather early!

You could try declare the pins as INPUT or OUTPUT first, then do the digital writes.

  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(led13, OUTPUT);
  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT);  
  pinMode(Relay_3, OUTPUT);  
  pinMode(Relay_4, OUTPUT);  

  //-------( Initialize Pins so relays are inactive at reset)----
  
  digitalWrite(led, LOW);  // Set led to LOW
  digitalWrite(led13, LOW);  // Set led to LOW
  
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF);

thanks, i will try this later and get back to you.

Hi Dan,

I now want to build a level of security into controlling this LED via SMS. I want to have a password. Example: currently I'm controlling/monitoring the LED using these commands:

#a0 - to turn OFF light
#a1 - to turn ON light
#aS - to get the light STATUS

In my text message i now want a password TEXT = passwd + command i.e the password i want to use for example is ?20!4

?20!4#a0 - to turn light OFF
?20!4#a1 - to turn ligh ON
?20!4#aS - to get STATUS

I thought of modifying my void loop to this:

void loop() {
 
 if(SIM900.available() >0)
 {
   
 incoming_char=SIM900.read(); 
 
  if (incoming_char=='?')
   {
   delay(10);
   incoming_char=SIM900.read(); 
   
    if (incoming_char=='2')
   {
   delay(10);
   incoming_char=SIM900.read(); 
   
   
    if (incoming_char=='0')
   {
   delay(10);
   incoming_char=SIM900.read(); 
   
   
    if (incoming_char=='!')
   {
   delay(10);
   incoming_char=SIM900.read(); 
   
   
    if (incoming_char=='4')
   {
   delay(10);
   incoming_char=SIM900.read(); 
   
 if (incoming_char=='#')
   {
   delay(10);
   incoming_char=SIM900.read(); 

 if (incoming_char=='a')
     {
   delay(10);
   incoming_char=SIM900.read();

 if (incoming_char=='0')
   {
   digitalWrite(led, LOW);
   digitalWrite(Relay_1, LOW);// set the Relay OFF
   delay(1000);              // wait for a second
   } 
 else if (incoming_char=='1')
   {
   digitalWrite(led, HIGH);
   digitalWrite(Relay_1, HIGH);// set the Relay ON
   delay(1000);              // wait for a second
   }
 else if (incoming_char=='S')
   {
   digitalRead(led);
   led_status=digitalRead(led);
   Serial.print(led_status);  // prints status on serial terminal
   sendSMS(led_status);
   }
 delay(10);
                   }
                }
             }
          }
        }
      }
    }
  }
}

Is there a shorter way i can use to have password checked in the SMS being sent with out using the IF statement because it makes the code very long :frowning:

Stick them in a char array (Not a String!) and then do a string compare?

Just in case: strcmp - C++ Reference

Hi Dan,

I have built some security into my code. I have not used the char array as mentioned previuosly, but I will. When i run this code even if i send the correct code to the arduino i.e ?20!4#a1 it keeps executing the sendSMSFAULT function repeatedly. Also why is it runnign that part of the code because the correct code was sent to the arduino to turn on light. Please have a look at the attached code for please:

The sendSMSFAULT should only run if the incoming character does not begin with ? mark

#include <SoftwareSerial.h>

SoftwareSerial SIM900(2, 3); // RX, TX

char incoming_char=0; //Will hold the incoming character from the Serial Port.If the command becomes bigger than 8 bits, try use char16_t
 
// Pin 8 has an LED connected on most Arduino boards.
int led = 8;
int led13 = 13;
int led_status = 0;     // variable to store the led status

/*-----( Declare Constants )-----*/
#define RELAY_ON 0
#define RELAY_OFF 1

/*-----( Declare variables )-----*/
#define Relay_1  8  // Arduino Digital I/O pin number
#define Relay_2  7
#define Relay_3  6
#define Relay_4  5

  // the setup routine runs once when you press reset:
void setup() {                

  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(led13, OUTPUT);
  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT);  
  pinMode(Relay_3, OUTPUT);  
  pinMode(Relay_4, OUTPUT);  
  
  digitalWrite(led, LOW);  // Set led to LOW
  digitalWrite(led13, LOW);  // Set led to LOW
  
  //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF); 
  
  Serial.begin(19200); // set the baud rate
  SIM900.begin(19200); // for GSM shield
  delay(20000);  // give time to log on to network.
  Serial.println("setup Section");
  SIM900.print("AT+CMGF=1\r");  // set SMS mode to text
  delay(100);
  SIM900.println("AT+CNMI=2,2,0,0,0\r"); 
  SIM900.print("AT+CLIP=1\r"); // turn on caller ID notification
  // blurt out contents of new SMS upon receipt to the GSM shield's serial out
  delay(100); 
  //digitalWrite(led, LOW);  // Set led to LOW
  //digitalWrite(led13, LOW);  // Set led to LOW
 // Serial.println("AT+CMGD=1,4");  //Delete all SMS in box
}

void sendSMS(byte led_status){               //SEND SMS

  SIM900.print("AT+CMGF=1\r");                     // AT command to send SMS message
  delay(1000);
  SIM900.println("AT + CMGS = \"+277XXXXXXXX\"");  // recipient's mobile number, in international format
  delay(1000);
if (led_status==0)
    {
      SIM900.println( " LIGHT STATUS: OFF");
      }
else
    {
      SIM900.println( " LIGHT STATUS: ON");
    }
  delay(1000);
  SIM900.println((char)26);                        // End AT command with a ^Z, ASCII code 26
  delay(1000); 
  SIM900.println();
  delay(5000);     // give module time to send SMS  
}

void sendSMSFAULT(){               //SEND SMS

  SIM900.print("AT+CMGF=1\r");                     // AT command to send SMS message
  delay(1000);
  SIM900.println("AT + CMGS = \"+277XXXXXXX\"");  // recipient's mobile number, in international format
  delay(1000);
  SIM900.println( " SORRY! YOU ARE NOT ALLOWED TO USE THE SYSTEM");
  delay(1000);
  SIM900.println((char)26);                        // End AT command with a ^Z, ASCII code 26
  delay(1000); 
  SIM900.println();
  delay(5000);     // give module time to send SMS  
}

// the loop routine runs over and over again forever:
void loop() {
 
 if(SIM900.available() >0)
 {
 incoming_char=SIM900.read(); 
 if (incoming_char!='?')
   {
   delay(10);
   Serial.println("Discarded SMS");
   sendSMSFAULT();
   Serial.println("AT+CMGD=1,4");
   incoming_char=SIM900.read(); 
  if (incoming_char=='?')
   {
   delay(10);
   incoming_char=SIM900.read();   
 if (incoming_char=='2')
   {
   delay(10);
   incoming_char=SIM900.read(); 
 if (incoming_char=='0')
   {
   delay(10);
   incoming_char=SIM900.read(); 
 if (incoming_char=='!')
   {
   delay(10);
   incoming_char=SIM900.read();  
 if (incoming_char=='4')
   {
   delay(10);
   incoming_char=SIM900.read();   
 if (incoming_char=='#')
   {
   delay(10);
   incoming_char=SIM900.read(); 
 if (incoming_char=='a')
     {
   delay(10);
   incoming_char=SIM900.read();
 if (incoming_char=='0')
   {
   digitalWrite(led, LOW);
   digitalWrite(Relay_1, LOW);// set the Relay OFF
   delay(1000);              // wait for a second
   } 
 else if (incoming_char=='1')
   {
   digitalWrite(led, HIGH);
   digitalWrite(Relay_1, HIGH);// set the Relay ON
   delay(1000);              // wait for a second
   }
 else if (incoming_char=='S')
   {
   digitalRead(led);
   led_status=digitalRead(led);
   Serial.print(led_status);  // prints status on serial terminal
   sendSMS(led_status);
   }
 delay(10);
                   }
                }
             }
            }
          }
        }
      }
    }
  }
}

I also included this in the code as you previously advised in the void Setup, however even after the changes, when i upload the code onto the arduino, my LED turns ON and stays ON…:frowning:

 // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(led13, OUTPUT);
  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT);  
  pinMode(Relay_3, OUTPUT);  
  pinMode(Relay_4, OUTPUT);  
  
  digitalWrite(led, LOW);  // Set led to LOW
  digitalWrite(led13, LOW);  // Set led to LOW
  
  //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF);

Hi Dan,

May you please give me an example how I can used the char array to analyze the incoming char and to do a compare as mentioned earlier.

"Stick them in a char array (Not a String!) and then do a string compare?"

In reply #3 I said:

Auto-format feature is your friend. Use it - a lot.

If you try it on the code you posted on 4th October you should be able to see where the problem arises. You have all your conditional testing in the section beginning:

 if (incoming_char!='?')

Hi Dan,

I have modified my void loop, however now my code keeps executing the sendSMSFAULT() function ALWAYS, even without sending commands to my modem. Please have a look at where I’m going wrong:

#include <SoftwareSerial.h>

SoftwareSerial SIM900(2, 3); // RX, TX

char incoming_char=0; //Will hold the incoming character from the Serial Port.If the command becomes bigger than 8 bits, try use char16_t

// Pin 8 has an LED connected on most Arduino boards.
int led = 8;
int led13 = 13;
int led_status = 0;     // variable to store the led status

/*-----( Declare Constants )-----*/
#define RELAY_ON 0
#define RELAY_OFF 1

/*-----( Declare variables )-----*/
#define Relay_1  8  // Arduino Digital I/O pin number
#define Relay_2  7
#define Relay_3  6
#define Relay_4  5

// the setup routine runs once when you press reset:
void setup() {                

  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(led13, OUTPUT);
  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT);  
  pinMode(Relay_3, OUTPUT);  
  pinMode(Relay_4, OUTPUT);  

  digitalWrite(led, LOW);  // Set led to LOW
  digitalWrite(led13, LOW);  // Set led to LOW

    //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF); 

  Serial.begin(19200); // set the baud rate
  SIM900.begin(19200); // for GSM shield
  delay(20000);  // give time to log on to network.
  Serial.println("setup Section");
  SIM900.print("AT+CMGF=1\r");  // set SMS mode to text
  delay(100);
  SIM900.println("AT+CNMI=2,2,0,0,0\r"); 
  SIM900.print("AT+CLIP=1\r"); // turn on caller ID notification
  // blurt out contents of new SMS upon receipt to the GSM shield's serial out
  delay(100); 
  //digitalWrite(led, LOW);  // Set led to LOW
  //digitalWrite(led13, LOW);  // Set led to LOW
  // Serial.println("AT+CMGD=1,4");  //Delete all SMS in box
}

void sendSMS(byte led_status){               //SEND SMS

  SIM900.print("AT+CMGF=1\r");                     // AT command to send SMS message
  delay(1000);
  SIM900.println("AT + CMGS = \"+27725XXXX\"");  // recipient's mobile number, in international format
  delay(1000);
  if (led_status==0)
  {
    SIM900.println( " LIGHT STATUS: OFF");
  }
  else
  {
    SIM900.println( " LIGHT STATUS: ON");
  }
  delay(1000);
  SIM900.println((char)26);                        // End AT command with a ^Z, ASCII code 26
  delay(1000); 
  SIM900.println();
  delay(5000);     // give module time to send SMS  
}

void sendSMSFAULT(){               //SEND SMS

  SIM900.print("AT+CMGF=1\r");                     // AT command to send SMS message
  delay(1000);
  SIM900.println("AT + CMGS = \"+27XXXXX\"");  // recipient's mobile number, in international format
  delay(1000);
  SIM900.println( " SORRY! YOU ARE NOT ALLOWED TO USE THE SYSTEM");
  delay(1000);
  SIM900.println((char)26);                        // End AT command with a ^Z, ASCII code 26
  delay(1000); 
  SIM900.println();
  delay(5000);     // give module time to send SMS  
}

// the loop routine runs over and over again forever:
void loop() {

  if(SIM900.available() >0)
  {
    incoming_char=SIM900.read(); 
    if (incoming_char!='?')
    {
      delay(10);
      Serial.println("Discard SMS");
      sendSMSFAULT();
      Serial.println("AT+CMGD=1,4");
      {
        delay(10);
        incoming_char=SIM900.read();   
        if (incoming_char=='?')
        {
          delay(10);
          incoming_char=SIM900.read();   
          if (incoming_char=='2')
          {
            delay(10);
            incoming_char=SIM900.read(); 
            if (incoming_char=='0')
            {
              delay(10);
              incoming_char=SIM900.read(); 
              if (incoming_char=='!')
              {
                delay(10);
                incoming_char=SIM900.read();  
                if (incoming_char=='4')
                {
                  delay(10);
                  incoming_char=SIM900.read();   
                  if (incoming_char=='#')
                  {
                    delay(10);
                    incoming_char=SIM900.read(); 
                    if (incoming_char=='a')
                    {
                      delay(10);
                      incoming_char=SIM900.read();
                      if (incoming_char=='0')
                      {
                        digitalWrite(led, LOW);
                        digitalWrite(Relay_1, LOW);// set the Relay OFF
                        delay(1000);              // wait for a second
                      } 
                      else if (incoming_char=='1')
                      {
                        digitalWrite(led, HIGH);
                        digitalWrite(Relay_1, HIGH);// set the Relay ON
                        delay(1000);              // wait for a second
                      }
                      else if (incoming_char=='S')
                      {
                        digitalRead(led);
                        led_status=digitalRead(led);
                        Serial.print(led_status);  // prints status on serial terminal
                        sendSMS(led_status);
                      }
                      delay(10);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}