Help with Modifying GSM_GPRSLib Example Code? Please and Thank You

I’m trying to modify some example code for a GSM Shield.

The example code “does” work as advertized.

It will send a text to my cell phone and receive a text to the serial monitor.

But now I’d like to modify the code to blink an led, turn on a relay on, yatta yatta yatta

Here is the Example Code:

#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 numdata;
boolean started=false;
char smsbuffer[160];
char n[20];

void setup()
{
     //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()
{
     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);

               // Begin Code Modification
               if (smsbuffer == 'a'){
                Serial.println ("If Statement is a Success");
                }// end if 
                else {
                  Serial.println ("If Statement is has Failed");
                  }//end else
                // End Code Modification  
          }
          delay(1000);
     }
};

I’ve added an If statement to the example code but I don’t understand the result I am receiving. I expected to see “If Statement is a Success”

But got this instead:

+13471234567

a

If Statement is has Failed

ATT: +CMGL
RIC: 
OK

I’ve tried to download many instructables GSM Relay sketches but none of them work with my GSM Shield.

The only code that works is the one from the example library that goes with my DuinoPeak Sim808 all in one shield. So I figured editing the working example code would be a good place to start.

Any help would be greatly appreciated.

               if (smsbuffer == 'a'){

The address of the buffer is unlikely to equal the literal 'a'.

smsbuffer[0] might contain 'a', so that might match.

There is a difference between a bucket and what's in the bucket.

Thank You PaulS!!!

Changing:

if (smsbuffer == 'a'){

to:

if (smsbuffer [0] == 'a'){

Worked. :slight_smile: :slight_smile: :slight_smile:

Now I'm wondering how can I do multi character?

I tried:

if (smsbuffer [0] == 'on'){

But it did not work. :frowning:

I understand I have to use arrays 0 and 1 somehow but don't know the correct syntax for that. (yet)

I'm still studying c++ but I'm also trying to work on my own projects at the same time.

Do some research on strcmp();

I’ve been banging my head against the wall for about 2 weeks now.

I got my code working where I can sms the characters ‘A’,‘B’,‘C’,‘D’,‘X’,or ‘Z’ and it will turn relays on and off.

But instead of sms ing single characters such as ‘A’,‘B’,‘C’,‘D’,‘X’,or ‘Z’ I’d much rather be able to sms something like ‘On’ or ‘Off’ (multi characters)

I’ve tried

if (smsbuffer |0| == ‘On’

But that does not work,

and I’ve tried

if (smsbuffer.indexOf(“On”) >= 0){

But that does not work either.

Can somebody please help me?

// An Arduino Sketch to Turn on and OFF my Semi-Truck's Coolant and Cabin Air Heaters as well as Lock and Unlock my Truck's Doors.

byte ProHeatDigitalOutputPin = 11;      // Connected to a relay that pushes the 7 day timer's        On/Off button.
byte EsparDigitalOutputPin = 10;        // Connected to a relay that pushes the DigiMax Controller's On/Off button.

boolean RelayOn = LOW;                  // My dumb retarded relay board has a reverse HIGH/LOW trigger.
boolean RelayOff = HIGH;                // My dumb retarded relay board has a reverse HIGH/LOW trigger.

#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 numdata;
boolean started = false;
char smsbuffer[160];
char n[20];

void setup()
{
  digitalWrite (ProHeatDigitalOutputPin,  RelayOff); // Make sure the default relay status is OFF.
  digitalWrite (EsparDigitalOutputPin,    RelayOff); // Make sure the default relay status is OFF.

  pinMode (ProHeatDigitalOutputPin, OUTPUT);
  pinMode (EsparDigitalOutputPin,   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()
{

  byte delayTime = 100;                   // Amount of time to press On/Off button
  int EsparDigiMaxLED;                    // Espar Digi Max LED's     voltage represented as 1023 degrees of resolution.
  int ProHeat7DayTimerLED;                // ProHeat 7Day Timer LED's voltage represented as 1023 degrees of resolution.
  const byte ProHeatAnalogInputPin = A1;  // Arduino Analog ProHeat Sensor Input Pin
  const byte EsparAnalogInputPin   = A0;  // Arduino Analog Espar   Sensor Input Pin
  int threshold = 300;                    // LED voltage threshold represented as 1023 degrees of resolution.
  //                                      // Min 432 / Max 454 when LED is on. Min 0 / Max 0 when LED is OFF.

  if (started) {
    //Read if there are messages on SIM card and print them.
    if (gsm.readSMS(smsbuffer, 160, n, 20)) {

     
      if (smsbuffer [0] == 'A' || smsbuffer [0] == 'X') { //Turns >>ON<< Espar Airtronic D2 Cabin Air Heater. http://www.esparofmichigan.com/img/d2.png
        Serial.println ("Turn On Espar If smsbuffer [0] value is 10 or 50");

        //check the green On/Off LED Status on the Espar DigiMax Controller. http://www.esparparts.com/images/photo.JPG
        EsparDigiMaxLED = analogRead (EsparAnalogInputPin);                 // Sample Espar Digi Max LED's voltage with 1023 degrees of resolution.
        Serial.print ("Espar DigiMax LED voltage is "); Serial.print (EsparDigiMaxLED * (5.0 / 1023.0)); Serial.println ("v");

        while (EsparDigiMaxLED < threshold) { //While the green LED indicator light is OFF (less than threshold value), do the following;
          Serial.println ("While the green LED indicator light is OFF (0.00v):");

          // DigiMax Controller has momentary On/Off push button that toggles the On/Off status.
          digitalWrite (EsparDigitalOutputPin, RelayOn); delay (delayTime);    // 'pushes'   the DigiMax On/Off button for (delayTime) sec
          Serial.print ("Push Button for "); Serial.print (100.0 / delayTime); Serial.println (" Seconds");
          digitalWrite (EsparDigitalOutputPin, RelayOff); delay (delayTime);   // 'releases' the DigiMax On/Off button for (delayTime) sec
          Serial.print ("Release Button for "); Serial.print (100.0 / delayTime); Serial.println (" Seconds");

          // re-sample Espar DigiMax green On/Off LED Status
          Serial.println ("Re-Sample Espar DigiMax LED value");
          EsparDigiMaxLED = analogRead (EsparAnalogInputPin);                 // Sample Espar DigiMaxLED's voltage with 1023 degrees of resolution.
          Serial.print ("Espar DigiMax LED voltage is "); Serial.print (EsparDigiMaxLED * (5.0 / 1023.0)); Serial.println ("v");
          Serial.println ("   ");
        }//end while
      }//end if

      
    }
    delay(1000);
  }
};

smsbuffer is a char array. You can't treat it as a class instance. You CAN use functions like strcmp().

Am I getting any closer? lol

if ( strcmp(smsbuffer, "ON") == 0 )

Still doesn't work but am I getting warmer?

Still doesn’t work but am I getting warmer?

Yes. You need to post ALL of your code, and your debug output, for us to help you understand what you are doing wrong.

You may, for example, have “ON” in smsbuffer, which is not the same as “ON”. You may not have a NULL terminator in the right place, so “ONxxsgSDHGasrfyh” won’t match “ON”.

Here is all of my code:

Compiling...
Uploading...
Done Uploading.
Sketch uses 11,616 bytes (36%) of program storage space. Maximum is 32,256 bytes.
Global variables use 1,457 bytes (71%) of dynamic memory, leaving 591 bytes for local variables. Maximum is 2,048 bytes.

It compiles with no errors.

line 80 has the new string compare. When I text ON to my arduino, none of my relays do anything. :frowning:

lines 106, 133, and 160 use still use single characters ('B','C','D','X', and 'Z') at the moment and the relays do turn on and off as expected. :slight_smile:

I'm trying to replace lines 80, 106, 133, and 160 with working multi char code such as ON, OFF, etc.

I guess it's easier to just nest a bunch of

if (statements){

together like so:

if (smsbuffer |0| == 'T' ) {
if (smsbuffer |1| == 'U' ) {
if (smsbuffer |2| == 'R' ) {
if (smsbuffer |3| == 'N' ) {
if (smsbuffer |4| == '_' ) {
if (smsbuffer |5| == 'O' ) {
if (smsbuffer |6| == 'F' ) {
if (smsbuffer |7| == 'F' ) {

//Do SomeThing OFF

}//end if
}//end if
}//end if
}//end if
}//end if
}//end if
}//end if
}//end if

I was just trying to avoid it because it make the code messy and harder to read but it does at least work.

I was really hoping for something more elegant like

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

But that doesn't seem to work.

Thank You PaulS and Dan95

But that doesn't seem to work.

It most certainly does IF smsbuffer contains ONLY "TURN_OFF". Why can't you PROVE that this is the case AND show us the proof?

Hey Paul,

You probably need to use strncmp() rather than strcmp(). strcmp assumes you are dealing with nul-terminated strings strncmp has an additional parameter to tell it how many characters to check. This:

[color=orange]if[/color] ( [color=green]strcmp[/color](smsbuffer, [color=teal]"TURN_OFF"[/color]) == 0 ){

only works if smsbuffer contains ['T','U','R','N','_','O','F','F',0]. This:

[color=orange]if[/color] ( [color=green]strncmp[/color](smsbuffer, [color=teal]"TURN_OFF"[/color], [b]8[/b]) == 0 ){

is what you want.

PaulMurrayCbr:
You probably need to use strncmp() rather than strcmp(). strcmp assumes you are dealing with nul-terminated strings strncmp has an additional parameter to tell it how many characters to check. This:

[color=orange]if[/color] ( [color=green]strcmp[/color](smsbuffer, [color=teal]"TURN_OFF"[/color]) == 0 ){

only works if smsbuffer contains ['T','U','R','N','_','O','F','F',0]. This:

[color=orange]if[/color] ( [color=green]strncmp[/color](smsbuffer, [color=teal]"TURN_OFF"[/color], [b]8[/b]) == 0 ){

is what you want.

It worked!!

:wink: :smiley: :grin: :money_mouth_face: :grinning: :cry:

Thank You Paul. People named Paul must be very smart.

Thank You Guys So Much!!!!