Go Down

Topic: How to send the state of an LED via SMS (Read 21097 times) previous topic - next topic

thandana

Sep 23, 2014, 06:07 pm Last Edit: Sep 26, 2014, 09:05 am by thandana Reason: 1
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: [Select]
[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]

dannable

Please read the post How to Use This Forum and mark your code up using the '#' key.
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

thandana

Hi Dan,

I have added the code tag using #

dannable

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!
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

dannable

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.
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

thandana

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:

Code: [Select]
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.

thandana

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

dannable

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

You could try changing the routine parameter:
Code: [Select]

void sendSMS(byte led_status)


and then

Code: [Select]
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.
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

thandana

Hi Dan,

This work fine. thanks for the help much appreciated.

thandana

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

Code: [Select]
#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);
      }
    }
}
}
   

   
   
   
   
 


dannable

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'.
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

thandana

Hi Dan,

In the void Setup () I have this defined:

Code: [Select]
  digitalWrite(led, LOW);  // Set led to LOW
  digitalWrite(led13, LOW);  // Set led to LOW


Is this not OK?

dannable

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.

Code: [Select]

  // 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);
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

thandana

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

thandana

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:
Code: [Select]

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 :-(

Go Up