0

I have a software serial link between an Arduino Uno and a TC35 GSM Module to send and receive SMS messages. Sending SMS'/calls is not a problem as it is a matter of sending the appropriate AT command to the GSM module. However I wish to use the AT+CMGR=1 command (which checks the first SMS stored on the SIM card) to check if there is any messages and store the message as a char array so that I can then check if the SMS contains the word 'on' or 'off' to activate a LED.

The AT+CMGR=1 command should return the following:

AT+CMGR=1
+CMGR: "REC READ","+3538xxxxxxxx",,"13/03/23,14:29:37+00"
Set

OK

But in the method below when I print 'data' it just returns:

Message contains:

AT

Any pointers would be much appreciated.

void checkMessage() {

    gsmSerial.println("AT+CMGR=1");    //Reads the first SMS

    for (x=0;x < 255;x++){            
        data[x]='\0';                        
    } 
    x=0;
    do{
        while(gsmSerial.available()==0);
        data[x]=gsmSerial.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("\r\nMessage contains: \r");
    Serial.println(data);    //shows the message

    delay(1000);
}
joce
  • 9,624
  • 19
  • 56
  • 74
BLL27
  • 921
  • 5
  • 13
  • 27
  • What is `data`? What is `gsmSerial`? – Drew Dormann Mar 25 '13 at 20:20
  • data is the char array; char data[256]. gsmSerial is a software serial connection between Arduino and GSM Module – BLL27 Mar 25 '13 at 20:25
  • For simplicity of code, I would recommend you have to have **ONLY** the word *ON* (all caps) so you don't have to search the whole message. This would also prevent accidental "Is this thing on?" to turn on the LED. – Anonymous Penguin Mar 25 '13 at 20:45

1 Answers1

3

I don't understand your intent of doing this:

if(data[x-1]==0x0D&&data[x-2]=='"'){ 
    x=0; 
}

It appears you're discarding your data and reading new data whenever you hit a line containing a trailing quote and newline. So the response to the command of interest is being discarded. I haven't tested it, but I think it would work if you deleted those three lines.

I should also mention that [x-1] and [x-2] are referencing memory prior to the data buffer. That's a very bad practice that can and will cause undefined behavior. You should only check the index minus some value when the result of that computation will be a positive value. A negative array index will access memory prior to the start of the array.

Carey Gregory
  • 6,836
  • 2
  • 26
  • 47