Reading SMS messages in SIM900

Hi,

I'm a newbie in gsm & arduino programming.

I created a simple code that will open on/off my relay.

When I send "ON" it will open and when I send "OFF" it will closed.

Here's my simple code.

#include <SoftwareSerial.h> 
SoftwareSerial SIM900(7, 8);

String inData;
char inchar; 

int led1 = 10;
int led2 = 11;
int led3 = 12;
int led4 = 13;

void setup()
{

  Serial.begin(19200);

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);

  SIM900power(); 
  SIM900.begin(19200);
  delay(20000); 
  SIM900.print("AT+CMGF=1\r");  
  delay(100);
  SIM900.print("AT+CNMI=2,2,0,0,0\r"); 
  delay(100);
}

void SIM900power()
{
  digitalWrite(9, HIGH);
  delay(1000);
  digitalWrite(9, LOW);
  delay(7000);
}

void loop() 
{

  while (SIM900.available()>0)
    {
        char recieved = SIM900.read();
        inData += recieved; 
        delay(200);

        if (recieved == '\n')
        {        
        
           if (inData == "ON") {
              digitalWrite(led1, HIGH);
            } else if (inData == "OFF") {
              digitalWrite(led1, LOW);
            } else {
              Serial.print("Invalid Command");
            }
            
            if(inData == "+++\n"){ 
              Serial.print("OK. Press h for help.");
            }   
           inData = ""; // Clear received message buffer
        }
    }
        
}

My problem is within the loop area. Can someone please explain why isn't behaving the way I wanted it to be?

Thank you and appreciate your help.

(I always get confused when this applies and it doesnt.. so take it with a grain of salt!) :slight_smile:

But how is the date (characters) received?

I think *(Robin2? not sure of member name.. do a search of the stickies.. or maybe I'm thinking of his multiple things at once thread?).. there is a thread here about here somewhere....

here we go:

https://forum.arduino.cc/index.php?topic=288234.0

but I think the general idea is to have a starting packet character.. or at the very least an 'ending packet' character..

'THEN' check the data you have collected and check it against your conditions..

OP: Notwithstanding the fact there is no question in your post...!

An SMS message contains several \r \n chars...
Chances are that you are 'finding' the wrong ones with your one-line test.

The first newline char indicates the end of the header line, then an \n for each line of the text message.
The message is terminated with \r\n\n (IIRC)... you'll need to check.

It's not simple, but it just needs a little patience to debug your code, and you'll have learned a lot on the way through.

You may also find strstr(), strtok() and other functions useful to extract the date, time, and sender number from the header.

xl97 suggested adding some print() statements to see what's happening and in what order.

thank you guys I already find a better way of doing it.

It's working now. Appreciate your reply.

[Considered it closed]

it is polite to post your working code, so others can see how it's done later...