Go Down

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

thandana

Hi Dan,

I tried it on your code and it worked. I sent about 10 SMS and a match was found. I will test it later on my code to see if it works. I will keep you posted.

thandana

Hi Dan,

It seems to be working on my code too. I sent multiple SMS's and it kept finding a match

I now want to be able to turn ON/OFF the light using time activation. As an example I would love to turn ON the lights at 06:00AM and turn them OFF at 21:00PM automatically. Any ideas on how to do this.

I will buy a RTC to attached to my arduino as I believe I will need. Below is my current code:

Code: [Select]
#include <SoftwareSerial.h>

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

int pin8 = 8;
int led13 = 13;
char buffer[9]; // eight char plus null terminator
char secret_code[] = "20!4#a1";
char secret_code1[] = "20!4#a0";
char secret_code2[] = "20!4#aS";
char incoming_char=0;
byte buffer_pos=0;
/*-----( Declare Constants )-----*/
#define RELAY_ON 1
#define RELAY_OFF 0

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

void setup()
{               
  pinMode(pin8, OUTPUT);
  pinMode(led13, OUTPUT);
  digitalWrite(led13, LOW);
  digitalWrite(pin8, LOW);  // Set led to LOW

  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT); 
  pinMode(Relay_3, OUTPUT); 
  pinMode(Relay_4, OUTPUT);

  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.
  SIM900.print("ATE0\r");
  SIM900.print("AT+CMGF=1\r");  // set SMS mode to text
  delay(100);
  SIM900.println("AT+CNMI=2,2,0,0,0\r");
  delay(100);
  Serial.println("Finished Setup Section");
}

void sendSMS(byte led_status)               
{
  SIM900.print("AT+CMGF=1\r");                     // AT command to send SMS message
  delay(1000);
  SIM900.println("AT + CMGS = \"+27XXXXXXXX\"");  // 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();
  Serial.println("Message sent");
  delay(5000);     // give module time to send SMS
}

void loop()
{
  if (SIM900.available()>0)
  {
    Serial.println("Reading incoming data");
    incoming_char=SIM900.read();
    if (incoming_char=='?')
    {
      Serial.println("Got a ?");
      delay(100);
      buffer_pos=0;
      while (SIM900.available())
      {
        delay(100);
        incoming_char=SIM900.read();
        buffer[buffer_pos]=incoming_char;
        buffer_pos++;

        if (buffer_pos==7) // Already incremented
        {
          //Print it out
          buffer[buffer_pos]='\0';

          Serial.write(buffer);
          Serial.println();
          if (strcmp(buffer,secret_code)==0)
          {
            Serial.println("It's a match!");
            digitalWrite(pin8, HIGH);
            int led_status=digitalRead(pin8);
            Serial.print ("LED status = ");
            Serial.println(led_status);  // prints status on serial terminal
            sendSMS(led_status);

            delay(1000);              // wait for a second
          }
          buffer_pos=0;
          if (strcmp(buffer,secret_code1)==0)
          {
            Serial.println("It's a match!");
            digitalWrite(pin8, LOW);
            int led_status=digitalRead(pin8);
            Serial.print ("LED status = ");
            Serial.println(led_status);  // prints status on serial terminal
            sendSMS(led_status);

            delay(1000);              // wait for a second
          }
          buffer_pos=0;
          if (strcmp(buffer,secret_code2)==0)
          {
            Serial.println("It's a match!");
            int led_status=digitalRead(pin8);
            Serial.print ("LED status = ");
            Serial.println(led_status);  // prints status on serial terminal
            sendSMS(led_status);

            delay(1000);              // wait for a second
          }
          buffer_pos=0;
        }
      }
    }
  }
}


dannable

If this is for an assignment that you need to hand in then I think you need to start doing some of the work?

However, I would suggest that if you are going to use an RTC then get one based on the DS3231 (or similar). Hint - this chip has an alarm.
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

dannable

Incidentally, the SIM900 has an RTC built in.
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

thandana

Hi Dan,

Yes I will do the work and was hoping if I get into some difficulty I could post it here again for help. I was planning to use the DS1307 RTC with arduino, i will also look into the SIM900 RTC and see if I can interface it with arduino.

I will keep you posted on my progress

dannable

You can always post here, probably in the programming section rather than this one though!

Have a search for 1307 before you commit just to see what other people say about it.
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

thandana

Hi Dan,

I've been having some difficulty with my code this morning. It seems like its not running through the void loop (), any reason why this is the case? I have tried resetting my arduino and SIM900 GSM shield but still doesn't seem to be going through the loop(). I have not changed the code from last time as that was working. May you please test this code for me and check if it is working your end.

I have tried other codes, and they run through the loop()

Code: [Select]

#include <SoftwareSerial.h>

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

int pin8 = 8;
int led13 = 13;
char buffer[9]; // eight char plus null terminator
char secret_code[] = "20!4#a1";
char secret_code1[] = "20!4#a0";
char secret_code2[] = "20!4#aS";
char incoming_char=0;
byte buffer_pos=0;
/*-----( Declare Constants )-----*/
#define RELAY_ON 1
#define RELAY_OFF 0

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

void setup()
{               
  pinMode(pin8, OUTPUT);
  pinMode(led13, OUTPUT);
  digitalWrite(led13, LOW);
  digitalWrite(pin8, LOW);  // Set led to LOW

  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT); 
  pinMode(Relay_3, OUTPUT); 
  pinMode(Relay_4, OUTPUT);

  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.
  SIM900.print("ATE0\r");
  SIM900.print("AT+CMGF=1\r");  // set SMS mode to text
  delay(100);
  SIM900.println("AT+CNMI=2,2,0,0,0\r");
  delay(100);
  Serial.println("Finished Setup Section");
}

void sendSMS(byte led_status)               
{
  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);
  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();
  Serial.println("Message sent");
  delay(5000);     // give module time to send SMS
}

void loop()
{
  if (SIM900.available()>0)
  {
    Serial.println("Reading incoming data");
    incoming_char=SIM900.read();
    if (incoming_char=='?')
    {
      Serial.println("Got a ?");
      delay(100);
      buffer_pos=0;
      while (SIM900.available())
      {
        delay(100);
        incoming_char=SIM900.read();
        buffer[buffer_pos]=incoming_char;
        buffer_pos++;

        if (buffer_pos==7) // Already incremented
        {
          //Print it out
          buffer[buffer_pos]='\0';

          Serial.write(buffer);
          Serial.println();
          if (strcmp(buffer,secret_code)==0)
          {
            Serial.println("It's a match!");
            digitalWrite(pin8, HIGH);
            int led_status=digitalRead(pin8);
            Serial.print ("LED status = ");
            Serial.println(led_status);  // prints status on serial terminal
            sendSMS(led_status);

            delay(1000);              // wait for a second
          }
          buffer_pos=0;
          if (strcmp(buffer,secret_code1)==0)
          {
            Serial.println("It's a match!");
            digitalWrite(pin8, LOW);
            int led_status=digitalRead(pin8);
            Serial.print ("LED status = ");
            Serial.println(led_status);  // prints status on serial terminal
            sendSMS(led_status);

            delay(1000);              // wait for a second
          }
          buffer_pos=0;
          if (strcmp(buffer,secret_code2)==0)
          {
            Serial.println("It's a match!");
            int led_status=digitalRead(pin8);
            Serial.print ("LED status = ");
            Serial.println(led_status);  // prints status on serial terminal
            sendSMS(led_status);

            delay(1000);              // wait for a second
          }
          buffer_pos=0;
        }
      }
    }
  }
}



rehnuma

Hi Dan,
I have tried this code with my arduino Uno and sim900

#include <SoftwareSerial.h>

SoftwareSerial SIM900(7, 19); // 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 led9 = 9;
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  6  // Arduino Digital I/O pin number
#define Relay_2  7
#define Relay_3  8
#define Relay_4  9

  // the setup routine runs once when you press reset:
void setup() {               
 
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(led9, 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(led9, 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);
 
  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(led9, 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 = \"+8801775XXXXXXX\"");  // 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);
      }
    }
}
}
   

After compiling and verifying the led 8,9 turns off. And in Serial monitor only "setup section" is written. what should I do to turn on/off led via sms? one more thing i want to know in recipients number should i give the gsm shield number or other number?

dannable

Code: [Select]
SoftwareSerial SIM900(7, 19); // RX, TX
int led = 8;
int led9 = 9;

Code: [Select]
#define Relay_2  7
#define Relay_3  8
#define Relay_4  9


This isn't going to end well...

In future please use the code button to format your code </>
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

AlexTadeo

Hi,

what can i do if i don't want to receive new sms message notifications when I'm reading the serial port???

Go Up