How to send the state of an LED via SMS

Thanks, I further look into this.

Hi Dan,

I'm very confused as what to look for now :~ =( I havn't been able to figure out why this code wouldn't run the 2nd time. Below was the output i saw in the serial terminal

Finished Setup Section
Reading incoming data: 13 

Reading incoming data: 10 

Reading incoming data: 62 >
Reading incoming data: 32  
Reading incoming data: 13 

Reading incoming data: 10 

Reading incoming data: 62 >
Reading incoming data: 32

Any other advise?

Send a text/SMS and watch what happens to buffer_pos. You may need to send more than one to fully appreciate what is happening.

Once you have worked out what the problem is then there are a few different ways to resolve it. That choice is up to you.

Hi Dan,

I'm really struggling to find out whats wrong here :frowning:

No point asking for a screen shot with the forum being in the state it is.

What is the value of buffer_pos immediately before it receives the second message? What should it be? And what is the effect of that?

Hi Dan,

I have been playing around with the code you have given me. Please see attached the screenshot of the serial terminal.

Although sending the correct code using my code, the incoming data is read incorrectly in my code:-(

Everytime I'm expecting it to print something like this on the serial terminal:

Got a ?
20!4#a1
It's a match
LED status = 1
message sent

Is this correct?

lights.pdf (291 KB)

Why aren't you running the example I gave you in reply #28(?) which prints out the value of buffer_pos? And if you pay attention to the values it is printing it will reveal where the problem lies.

HI Dan,

I did use the example in 28 to try and see what the problem is. Everytime I sent an SMS i could see the incomming data on the serial terminal and also when the modem attempts to send SMS status. I tried to use what I saw there and analye the other code to see where is was failing and still couldn't.

I used the below code:

#include <SoftwareSerial.h>

SoftwareSerial SIM900(7,8); // RX, TX

int led13 = 13;
char buffer[9]; // eight char plus null terminator
char secret_code[] = "20!4#a0"; 
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
byte buffer_pos=0;

void setup() {                

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

    Serial.begin(19200); // set the baud rate
  SIM900.begin(19200); // for GSM shield
  delay(5000);  // give time to log on to network.
  SIM900.print("ATE1\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){               //SEND SMS

  //SIM900.print("AT+CMGF=1\r");                     // AT command to send SMS message
  //delay(1000);
  SIM900.println("AT + CMGS = \"+447751xxxxxx\"");  // 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.print("Reading incoming data: ");
    incoming_char=SIM900.read();
    Serial.print(int(incoming_char));
    Serial.print(" ");
    Serial.println(incoming_char);
    if (incoming_char=='?')
    {
      Serial.println("Got a ?");
      delay(100);
      while (SIM900.available())
      {
        delay(100);
        incoming_char=SIM900.read();

        Serial.print("Reading incoming data in loop: Buffer_pos = ");
        Serial.print(buffer_pos);
        Serial.print(" ");
        Serial.print(int(incoming_char));
        Serial.print(" ");
        Serial.println(incoming_char);


        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(led13, HIGH);
            int led_status=digitalRead(led13);
            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;
        }
      }
    }
  }
}

I will send you the output of the code in #28 a bit later. Maybe you can guide me to interpret those outputs.

Hi Dan,

I'm really struggling to solve this one. May you please assist me as I'm now blocked from continuing, please.

Regards,

Take a screen capture of Serial Monitor when you have sent one SMS and post it here. Keep it a sensible size but legible.

Hi Dan,

Apologies for such a late response. Please find attached the serial output when i send the SMS for the first time.

serial_terminal.pdf (805 KB)

Ok, let's get rid of some of the 'noise'. Change the line in setup() to

  SIM900.print("ATE0\r");

(this turns the echo off) and repeat the capture screen shot exercise?

Hi Dan,

Please find attached screenshot.

I used the below code:

#include <SoftwareSerial.h>

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

int led13 = 13;
char buffer[9]; // eight char plus null terminator
char secret_code[] = "20!4#a1"; 
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
byte buffer_pos=0;

void setup() {                

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

    Serial.begin(19200); // set the baud rate
  SIM900.begin(19200); // for GSM shield
  delay(5000);  // 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){               //SEND SMS

  //SIM900.print("AT+CMGF=1\r");                     // AT command to send SMS message
  //delay(1000);
  SIM900.println("AT + CMGS = \"+27XXXXXX\"");  // 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.print("Reading incoming data: ");
    incoming_char=SIM900.read();
    Serial.print(int(incoming_char));
    Serial.print(" ");
    Serial.println(incoming_char);
    if (incoming_char=='?')
    {
      Serial.println("Got a ?");
      delay(100);
      while (SIM900.available())
      {
        delay(100);
        incoming_char=SIM900.read();

        Serial.print("Reading incoming data in loop: Buffer_pos = ");
        Serial.print(buffer_pos);
        Serial.print(" ");
        Serial.print(int(incoming_char));
        Serial.print(" ");
        Serial.println(incoming_char);


        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(led13, HIGH);
            int led_status=digitalRead(led13);
            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;
        }
      }
    }
  }
}

serial_terminal1.pdf (562 KB)

Now look at the last line?

buffer_pos is already at 1. The moment you receive another question mark it will start writing the data that follows it to position 2. So you will never get another match.

This is probably caused by the delays in the code, by the time the process has executed there is more serial data to read. In this instance you probably need to make sure that once you get a question mark you reset buffer_pos to zero.

Hi Dan,

This would be the last line?:

Reading incoming data in loop: Buffer_pos = 0 10

I added buffer_pos=0; just after the Serial.println("Got a ?");

    {
      Serial.println("Got a ?");
      buffer_pos=0;
      delay(100);
      while (SIM900.available())
      {
        delay(100);
        incoming_char=SIM900.read();

Is this correct?

Surely it's easy enough to try it and see? Leave all the debug stuff in there to check.

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.

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:

#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;
        }
      }
    }
  }
}

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.

Incidentally, the SIM900 has an RTC built in.