GSM shield HELP

Hello ! I have an Icomsat that activates a relay upob receiveing an "on" sms and turns off upon receiving an "off" sms. it works.....sometimes..... the code is bellow.

#include "SIM900.h"
#include <SoftwareSerial.h>
//If not used, is better to exclude the HTTP library,
//for RAM saving.
//If your sketch reboots itself proprably you have finished,
//your memory available.
//#include "inetGSM.h"

//If you want to use the Arduino functions to manage SMS, uncomment the lines below.
#include "sms.h"
SMSGSM sms;

//To change pins for Software Serial, use the two lines in GSM.cpp.

//GSM Shield for Arduino
//www.open-electronics.org
//this code is based on the example of Arduino Labs.

//Simple sketch to send and receive SMS.
int led = 7;
int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];

void setup() 
{
  pinMode(led, OUTPUT); 
  //Serial connection.
  Serial.begin(9600);
  Serial.println("GSM Shield testing.");
  //Start configuration of shield with baudrate.
  //For http uses is raccomanded to use 4800 or slower.
  if (gsm.begin(2400)){
    Serial.println("\nstatus=READY");
    started=true;  
  }
  else Serial.println("\nstatus=IDLE");


  if(started){
    //Enable this two lines if you want to send an SMS.
    //if (sms.SendSMS("3471234567", "Arduino SMS"))
    //Serial.println("\nSMS sent OK");
  }

};

void loop() 

{
  char  position1 = sms.IsSMSPresent(SMS_READ);
  char  position2 = sms.IsSMSPresent(SMS_UNREAD);
  char  position3 = sms.IsSMSPresent(SMS_UNREAD);
  char  position4 = sms.IsSMSPresent(SMS_UNREAD);
  if(started){
    //Read if there are messages on SIM card and print them.
    if(gsm.readSMS(smsbuffer, 160, n, 20))
    {
      Serial.println(n);
      Serial.println(smsbuffer);
    }
    delay(1000);


    if (position1) {
      sms.DeleteSMS(position1);
      if (position2) {
        sms.DeleteSMS(position2);
        if (position3) {
          sms.DeleteSMS(position3);
          if (position4) {
            sms.DeleteSMS(position4);
          }
        }
      }
    }
  }
Serial.print("smsbuffer is: [");
Serial.print(smsbuffer);
Serial.println("]");
Serial.print("The result of strcmp is : ");
Serial.println((strcmp (smsbuffer,"On")));
 
if (strcmp (smsbuffer,"On") == 13) 
  {
Serial.print("match On");
digitalWrite(led,HIGH);
  }
else
if (strcmp (smsbuffer,"On") == -8) 
{
  Serial.print("match Off");
digitalWrite(led,LOW);
 }
  }
void ProcessSms( String sms ){


}

the serial print is as bellow

GSM Shield testing.

status=READY
13/04/17,20:18:08+3
On

smsbuffer is: [On
]
The result of strcmp is : 13
match OnDesmond
Off

smsbuffer is: [Off
]
The result of strcmp is : -8
match OffDesmond
Off

smsbuffer is: [Off
]
The result of strcmp is : -8
match OffDesmond
+CMT: "+60168100114","Desmond","13/04/17,20:19:38+32"

smsbuffer is: [+CMT: "+60168100114","Desmond","13/04/17,20:19:38+32"
]
The result of strcmp is : -36
Desmond
On

smsbuffer is: [On
]
The result of strcmp is : 13
match OnDesmond
On

smsbuffer is: [On
]
The result of strcmp is : 13
match OnDesmond
+CMT: "+60168100114","Desmond","13/04/17,20:21:18+32"

smsbuffer is: [+CMT: "+60168100114","Desmond","13/04/17,20:21:18+32"
]
The result of strcmp is : -36
Desmond
+CMT: "+60168100114","Desmond","13/04/17,20:21:57+32"

smsbuffer is: [+CMT: "+60168100114","Desmond","13/04/17,20:21:57+32"
]
The result of strcmp is : -36
Desmond
+CMT: "+60168100114","Desmond","13/04/17,20:21:57+32"

smsbuffer is: [+CMT: "+60168100114","Desmond","13/04/17,20:21:57+32"
]
The result of strcmp is : -36
Desmond
+CMT: "+60168100114","Desmond","13/04/17,20:21:57+32"

smsbuffer is: [+CMT: "+60168100114","Desmond","13/04/17,20:21:57+32"
]
The result of strcmp is : -36
Desmond
+CMT: "+60168100114","Desmond","13/04/17,20:21:57+32"

smsbuffer is: [+CMT: "+60168100114","Desmond","13/04/17,20:21:57+32"
]
The result of strcmp is : -36
Desmond
+CMT: "+60168100114","Desmond","13/04/17,20:21:57+32"

smsbuffer is: [+CMT: "+60168100114","Desmond","13/04/17,20:21:57+32"
]
The result of strcmp is : -36
Desmond
+CMT: "+60168100114","Desmond","13/04/17,20:21:57+32"

smsbuffer is: [+CMT: "+60168100114","Desmond","13/04/17,20:21:57+32"
]
The result of strcmp is : -36

notice that there is a repeat. when the text is on, its suppose to be strcmp = 13 and strcmp = -8 when off.... however occasionally a -36 appears i dont know where this comes from. as in sometimes when i sms "on" instead of 13 it appears -36..... any ideas? also the serial print keeps repeating itself i have no idea why... is it because of void loop? your help is much appreciated!!!!!!

hmmm anyone?

Hi, just wanna try and ask for help again....had no reply yet. still cant solve the problem

This might be a clue:

]
The result of strcmp is : -36
Desmond
On

and

]
The result of strcmp is : 13
match OnDesmond
Off

Sometimes you have a space in the SMS, sometimes not

And, yes, it will repeat, due to the loop.

Hmm interesting point.... Will try to be more observant! Thanks

DMond, glad that I have same SIM900.h but haven't done yet as bit problem so still trying sort out. Hmm I see your code but same code"On" both

if (strcmp (smsbuffer,"On") == 13) 
.....
.....
....
else
if (strcmp (smsbuffer,"On") == -8)

Should "on" and "off"

if (strcmp (smsbuffer,"On") == 13) 
.....
.....
....
else
if (strcmp (smsbuffer,"Off") == -8)

Wondering your GSM all working done?

DMond, I copied your code I tested, I fixed "Off" after "On" both. same happened yours, there is a repeat, when the text is on, it was right problem.
Have you finally solved problem then its works fine with your code GSM?

hi, i am not quite sure what do you mean..but i hve an idea on how to solve it though, for the strcmp, i have to compare more

Here is another hint:

Serial.print("smsbuffer is: [");
// there is no line feed here
Serial.print(smsbuffer);
Serial.println("]");

// there is here
smsbuffer is: [On
]

If there was not a line feed returned in smsbuffer, it would look like this:

smsbuffer is: [On]

How about using strncmp.

if (strncmp (smsbuffer,"On",2) == 0)

if (strcmp (smsbuffer,"On") == 13)

You are using strcmp in the wrong way.
If you want compare smsbuffer to On you should compare it to 0 since if there is no diference between smsbuffer and On it will give you back a value of 0.
So the right way is:

if (strcmp(smsbuffer, "On")  == 0){
//Do usefull stuf
}

HugoPT:

if (strcmp (smsbuffer,"On") == 13)

You are using strcmp in the wrong way.
If you want compare smsbuffer to On you should compare it to 0 since if there is no diference between smsbuffer and On it will give you back a value of 0.
So the right way is:

if (strcmp(smsbuffer, "On")  == 0){

//Do usefull stuf
}

If there is a line feed in smsbuffer, that will never equal zero.

DMond:
hi, i am not quite sure what do you mean..but i hve an idea on how to solve it though, for the strcmp, i have to compare more

Wondering, have you solved the problem yet? same mine still problem.