reading sms

Hi, using arduino uno r3 and sim900 shield to receive sms's and switch on
on board L.E.D,with the sketch below,i am able to read all sms's pefectly but
i need to capture the data and activate an output,if i sms the word on to
the shield,pin13 must go high,please help complete the sketch.thank you.

/*
*  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L.
*  http://www.libelium.com
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see .
*
*  Version 0.1
*  Author: Alejandro Gállego
*/


int led = 13;
int onModulePin = 9;        // the pin to switch on the module (without press on button) 

int timesToSend = 1;        // Numbers of SMS to send
int count = 0;

int n_sms,x,sms_start;
char data[256];

void switchModule(){
    digitalWrite(onModulePin,HIGH);
    delay(2000);
    digitalWrite(onModulePin,LOW);
}

void setup(){

    Serial.begin(19200);                // UART baud rate
    delay(2000);
    pinMode(led, OUTPUT);
    pinMode(onModulePin, OUTPUT);
    switchModule();                    // switches the module ON

    for (int i=0;i < 5;i++){
        delay(5000);
    } 

    Serial.println("AT+CMGF=1");         // sets the SMS mode to text
    
    delay(1000);
}

void loop(){
    
    
        Serial.println("AT+CMGL");    //Reads the first SMS
         Serial.println("AT+CMGD=1,4");
         
        Serial.flush();
        for (x=0;x < 255;x++){            
            data[x]='\0';                        
        } 
        x=0;
        do{
            while(Serial.available()==0);
            data[x]=Serial.read();  
            x++;           
            if(data[x-1]==0x0D&&data[x-2]=='"'){
                x=0;
            }
        }while(!(data[x-1]=='K'&&data[x-2]=='O'));

        data[x-3]='\0';        //finish the string before the OK

        Serial.println(data);    //shows the message
        

        delay(5000);

}
        Serial.flush();

Why?

I'm pretty sure that your code does not look like that. Modify your post. Select the code, delete it. Select the # icon, and re-paste your code. Save the change.

Which version of the IDE are you using?

If you are using 1.0 or later, Serial.flush() blocks until the last character from the send buffer has been grabbed for sending. Is that really useful or necessary in your application?

Are you talking to the phone on the serial port, or the Serial Monitor? You seem to be trying to do both.

        Serial.println(data);    //shows the message

Does it? What does the message look like?

Parsing the message, if you've collected it correctly, is pretty easy. But, it would help if we saw what the message was.

what i really want to achieve is
to switch on the on board led when i sms the word 'on' to the shield and
off to turn the led off ,in stand alone mode

What does "in stand alone mode" mean?

Use strcmp() to see if data and "on" are equal. If so, turn the LED on.
Use strcmp() to see if data and "off" are equal. If so, turn the LED off.

PaulS:
Are you talking to the phone on the serial port, or the Serial Monitor? You seem to be trying to do both.

You may not have realized the significance of this comment from PaulS.

You can only have one device at each end of the serial link. Your sketch is using the Arduino's hardware serial port to talk to the GSM shield. You must not connect anything else to the same serial port. The USB/Serial connection to the host PC uses the same serial port so you must not connect the USB lead at the same time as the shield is connected; that would result in both sources trying to drive the same serial Tx/Rx lines which is a bad idea.

i am still trying to figure out how the Strcmp string works and how i would use it

if(strcmp(data, "on") == 0)
{
   // Turn the pin on
}
if(strcmp(data, "off") == 0)
{
   // Turn the pin off
}

do i need any new variables on the top,sorry,little knowledge of c++

Not for that code.

    if(strcmp(data, "on") == 0)
{
  digitalWrite(led,HIGH);
}
if(strcmp(data, "off") == 0)
{
   digitalWrite(led,LOW); // Turn the pin off
   
    Serial.println("AT+CMGD=1,4");
 }

What is up
with the random
indenting? Use Tools +
Auto Format
to get the code looking reasonable.

Why is that AT command sent only when the light is to be turned off?

Why is there a 5 second delay after receiving on or off before you do it?

You're the one with the hardware. Does it work?

Change some of your code to look like this:

        Serial.print("data: [");
        Serial.print(data);    //shows the message
        Serial.println("]");
        if(strcmp(data, "on") == 0)
        {
           Serial.println("Turning LED on");
           digitalWrite(led,HIGH);
        }
        if(strcmp(data, "off") == 0)
        {
           Serial.println("Turning LED off");
           digitalWrite(led,LOW); // Turn the pin off
        }

Show us the serial output.

after adding parts to the sketch,

"I changed my code, but I'm not going to show it to you." Did I paraphrase that correctly?

when i send sms's it works perfectly,but i have problems reading sms's and activating outputs,for some reason the loop in the sketch stops after the first check when ever i try to read the serial data,

And, yet, still no code.

maybe my delete command wrong;

One can help with coding without understanding which AT command does what. You should have functions that send exactly one AT command. The name of the function should clearly reflect what the AT command being sent does.

If

  Serial.println("AT+CMGR");    //Reads the first SMS

reads the message, and

  Serial.println("AT+CMGD=1,4");

deletes it, then, yes, you have a problem with the delete command.

data no longer gets a value, because you no longer actually read anything. So, the fact that sending the Arduino a SMS causes nothing to happen is not really surprising.

"OK, I made some more code changes that I'm not going to show you..." Haven't we been down this path before?

We can NOT see what you are doing.

We've hired a psychic, but he's not due to start until the 12th of never. Until then, you have to help out.

  while(Serial.available()==0);

  Serial.print("data: [");
  Serial.print(data);    //shows the message
  Serial.println("]");
  if(strcmp(data, "on") == 0)
  {
    Serial.println("Turning LED on");
    digitalWrite(led,HIGH);
  }

Send a command. Wait until there is a reply. Ignore the reply. Expect, magically, the data to have been read anyway.

Can anyone else see a problem with this?

What happened to the code to actually read the reply?

ok,this is my full code,does not loop when checking for sms's,

What output do you get?

Why is that Serial.flush() back? GET RID OF IT!!!

stops checking after ;

You don't know that. All you know is that your reading loop never ends. So, what is happening in that loop?

  for (x=0;x < 255;x++){            
    data[x]='\0';                        
  }

If you understood C strings, you'd know why this was unnecessary.

data[0] = '\0';

is all that is needed.

Make some changes.
x=0;
do{
while(Serial.available()==0);
data[x++]=Serial.read();
data[ x ] = '\0'; // Keep string NULL terminated
if(data[x-1]==0x0D&&data[x-2]=='"')
{ // Down here where it belongs!
x=0;
data[ 0 ] = '\0';
}
Serial.print("data: [");
Serial.print(data);
Serial.println("]");
}
while(!(data[x-1]=='K'&&data[x-2]=='O'));

Show us the output (and the code).

  x=0;
  do{
  }
  while(!(data[x-1]=='K'&&data[x-2]=='O'));

How valid do you suppose -1 and -2 are as indices into data?

You really need to ditch the do/while loop, and use some other construct to wait for data, and then read all data until OK appears. But, before you get started on that, think about what happens if the AT command fails, and the return does NOT include OK.

all i have is this perfect code

        x=0;
        do{
        }while(!(data[x-1]=='K'&&data[x-2]=='O'));

When x is 0, as it is initially, it gets incremented to 1 inside the body of the do/while statement. Then, at then end you check the values in data[ 0 ] and data[ -1 ], to see if they are O and K. And, you call code that references an invalid element of the array "perfect". I don't think so.

Add the if(strcmp(data, "on") == 0) block to that code. After the if block, print data again. With the '[' before and ']' after it. Does data get corrupted by the strcmp() call?

hi sir, not sure if its done correctly but there's my attempt,
yes the data does get corrupted.

No, it doesn't. If it did, the output would be correct before the if statement, and corrupt after. It is not correct at any point.

The fact that you are trying to talk to the phone and the PC on the same serial port is the problem.

found a sketch for sim900,that should activate led pin output when receives an sms with on but does not,my serial monitors shows the sketch reading the sms without any problems but no led lights up.Got the gsm shield set on software serial and using uno R3,also tried using it in hardware serial mode,did not work,in hardware serial mode pin1 and 2 are (txrx),not sure how to re-configure in the sketch and not sure why led is not high when sms with ,on is sent.

Look through the output you showed. Where do you see "on" or "off" in that output?