Go Down

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

thandana

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

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


dannable

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

thandana

Hi Dan,

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

dannable

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

thandana

#34
Oct 27, 2014, 05:15 pm Last Edit: Oct 27, 2014, 05:17 pm by thandana
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:
Code: [Select]

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


Is this correct?

dannable

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

thandana

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

#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.

thandana

Hi Dan,

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

Regards,

dannable

Take a screen capture of Serial Monitor when you have sent one SMS and post it here. Keep it a sensible size but legible.
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

thandana

Hi Dan,

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

dannable

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

Code: [Select]
  SIM900.print("ATE0\r");

(this turns the echo off) and repeat the capture screen shot exercise?
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

thandana

Hi Dan,

Please find attached screenshot.

I used the below code:
Code: [Select]

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

dannable

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

thandana

Hi Dan,

This would be the last line?:

Code: [Select]
Reading incoming data in loop: Buffer_pos = 0 10


I added buffer_pos=0; just after the Serial.println("Got a ?");
Code: [Select]

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


Is this correct?

dannable

Surely it's easy enough to try it and see? Leave all the debug stuff in there to check.
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

Go Up