Help with code

Hello, i have an icomsat v1.1 gsm shield. it is a wonderful shield but i lack understanding on it. i want to activate a relay via a particular SMS. for example, when i SMS “on” a Pin on arduino will be HIGH and vice versa.

i tried modifying a code, but the result is every time The GSM receives ANY SMS, the pin will be high. can someone assist on making the SMS more specific?
the Bold parts are the parts i added

/* GSM Shield example
 
 created 2011
 by Boris Landoni
 
 This example code is in the public domain.

 
 http://www.open-electronics.org
 http://www.futurashop.it
 */
[b]int led = 13;[/b]

#include <SoftwareSerial.h> 
#include <GSM_Shield.h>

//**************************************************************************
char number[]="+39123456789";  //Destination number 
char text[]="hello world";  //SMS to send
byte type_sms=SMS_UNREAD;      //Type of SMS
byte del_sms=0;                //0: No deleting sms - 1: Deleting SMS
//**************************************************************************

GSM gsm;
char sms_rx[122]; //Received text SMS
//int inByte=0;    //Number of byte received on serial port
char number_incoming[20];
int call;
int error;


void setup() 
{
[b]pinMode(led, OUTPUT);[/b] 
  Serial.begin(9600);
  Serial.println("system startup"); 
  gsm.TurnOn(9600);          //module power on
  gsm.InitParam(PARAM_SET_1);//configure the module  
  gsm.Echo(0);               //enable AT echo 

}


void loop()
{ 
  char inSerial[5];   
  int i=0;
  delay(2000);
  
    Check_Call(); //Check if there is an incoming call
    Check_SMS();  //Check if there is SMS 
    //Check data serial com 
    
    if (Serial.available() > 0) 
    {             
       while (Serial.available() > 0) {
         inSerial[i]=(Serial.read()); //read data  
         i++;      
       }
       inSerial[i]='\0';
      Check_Protocol(inSerial);
    }
       
}  

void Check_Protocol(String inStr)
{   
       Serial.print("Command: ");
       Serial.println(inStr);
       
  Serial.println("Check_Protocol");
  
    switch (inStr[0])
      {
       case 'a' :  //Answer        
           if (gsm.CallStatus()==CALL_INCOM_VOICE){
             gsm.PickUp();
             Serial.println("Answer");
           }
           else
           {
             Serial.println("No incoming call");
           }
         break;
       
    
       case 'c': // C  //Call
         if (inStr.length()<2)  //To call variable 'number'    comand   c
         {
           Serial.print("Calling ");
           Serial.println(number);         
           gsm.Call(number);
         }
         if (inStr.length()==2)  //To call number in phone book position   comand   cx where x is the SIM position
         {
             error=gsm.GetPhoneNumber(inStr[1],number);
             if (error!=0)
             {
               Serial.print("Calling ");
               Serial.println(number);
               gsm.Call(number);
             }
             else 
             {
               Serial.print("No number in pos ");
               Serial.println(inStr[1]);
             }
         }
         break;
          
       case 'h': //H //HangUp if there is an incoming call
         if (gsm.CallStatus()!=CALL_NONE)         
         {
           Serial.println("Hang");
           gsm.HangUp();              
         }
         else
         {
           Serial.println("No incoming call");
         }    
         break;
         
         
       case 's': //S //Send SMS
         Serial.print("Send SMS to ");
         Serial.println(number);
         error=gsm.SendSMS(number,text);  
         if (error==0)  //Check status
         {
             Serial.println("SMS ERROR \n");
         }
         else
         {
             Serial.println("SMS OK \n");             
         }
         break;
              
       case 'p':  //Read-Write Phone Book
         if (inStr.length()==3)
         {
           
           switch (inStr[1])
           {
             case 'd':  //Delete number in specified position  pd2
               error=gsm.DelPhoneNumber(inStr[2]);
               if (error!=0)
               {
                 Serial.print("Phone number position ");
                 Serial.print(inStr[2]);
                 Serial.println(" deleted");
               }
               break;
               
               
               
             case 'g':  //Read from Phone Book position      pg2
               error=gsm.GetPhoneNumber(inStr[2],number);
               if (error!=0)  //Find number in specified position
               {
                 Serial.print("Phone Book position ");
                 Serial.print(inStr[2]);
                 Serial.print(": ");
                 Serial.println(number);
               }
               else  //Not find number in specified position
               {
                 Serial.print("No Phone number in position ");
                 Serial.println(inStr[2]);
               }
               break;
             case 'w':  //Write from Phone Book Position    pw2
               error=gsm.WritePhoneNumber(inStr[2],number);
               if (error!=0)
               {
                 Serial.print("Number ");
                 Serial.print(number);
                 Serial.print(" writed in Phone Book position ");
                 Serial.println(inStr[2]);
               }
               else Serial.println("Writing error");
               break;
               
               
               
           }
           
         }
         break;
         
       }
   
    delay(1500);
    
    return;
 }
 
 
 void Check_Call()  //Check status call if this is available
 {     
     call=gsm.CallStatus();
     switch (call)
     {    
       case CALL_NONE:
         Serial.println("no call");
         break;
       case CALL_INCOM_VOICE:
         gsm.CallStatusWithAuth(number_incoming,0,0);        
         Serial.print("incoming voice call from ");     
         Serial.println(number_incoming);
         break;
       case CALL_ACTIVE_VOICE:
         Serial.println("active voice call");    
         break;
       case CALL_NO_RESPONSE:
         Serial.println("no response");
         break;
     }
     return;
 }
 
 
 void Check_SMS()  //Check if there is an sms 'type_sms'
 {
     char pos_sms_rx;  //Received SMS position     
     pos_sms_rx=gsm.IsSMSPresent(type_sms);
     if (pos_sms_rx!=0)
     {
      [b]  digitalWrite(led, HIGH);[/b] 
//Read text/number/position of sms
       gsm.GetSMS(pos_sms_rx,number_incoming,sms_rx,120);
       Serial.print("Received SMS from ");
       Serial.print(number_incoming);
       Serial.print("(sim position: ");
       Serial.print(word(pos_sms_rx));
       Serial.println(")");
       Serial.println(sms_rx);
       if (del_sms==1)  //If 'del_sms' is 1, i delete sms 
       {
         error=gsm.DeleteSMS(pos_sms_rx);
         if (error==1)Serial.println("SMS deleted");      
         else Serial.println("SMS not deleted");
       }
     }
     return;
 }

your help is much appreciated!

It looks like your added code turns on Pin 13 if there is ever an unread SMS. You should probably figure out how to look at the contents of the unread SMS and check to see that it says "on" before tuning on Pin 13.

Hi and thanks for the reply! yeah im still working on it, by any chance can you explain this part to me?

 void Check_SMS()  //Check if there is an sms 'type_sms'
 {
     char pos_sms_rx;  //Received SMS position     
     pos_sms_rx=gsm.IsSMSPresent(type_sms);
     if (pos_sms_rx!=0)
     {

I dont understand the " type_sms" part

thanks a bunch!

OP, do you have the library or shield manual? You can't always tell what it is or does from the code. A program using a library is pretty much like an essay written with local dialect. You could under the English language but still don't know what a local person means by "you betcha" unless you have a local literature book or reference. I am using another gsm hardware and library where such manual is not available so all I can do is to ask the library/hardware expert like a lost tourist. :frowning:

desmondttm123:

 void Check_SMS()  //Check if there is an sms 'type_sms'

{
     char pos_sms_rx;  //Received SMS position     
     pos_sms_rx=gsm.IsSMSPresent(type_sms);
     if (pos_sms_rx!=0)
     {




I dont understand the " type_sms" part

near the tp of the sketch is:

byte type_sms=SMS_UNREAD;      //Type of SMS

From this I'm assuming that gsm.IsSMSPresent(type_sms); means "give me the index of the first unread SMS message, or zero if there are no unread SMS messages". You will have to research your particular shield and library to figure out how to fetch the contents of an SMS messages given the index number.

Hello. I’m got this code from a friend and when I went to verify it it had errors. I’m new to coding and Arduino. What is wrong with this code?

#include <Tone.h>

Tone freq1;
Tone freq2;

const int ledPin = 6;
const int transistorGnd = 7;
const int repulsorSpeaker = 11;
const int speakerGnd = 10;
boolean readyToFire = true;
boolean beginCount = true;
boolean notDeactivated = true;
int repulsorStable = 10;
const int tilt_s1 = A2;
const int tilt_s2 = A1;

long previousMillis = 0;        // will store last time LED was updated
long interval = 12000; 

int thisPitch1;
int thisPitch2;

void setup() {
  // put your setup code here, to run once:
  
  Serial.begin(9600);
  
  freq1.begin(repulsorSpeaker);
  freq2.begin(repulsorSpeaker);
  pinMode(ledPin, OUTPUT);
  pinMode(speakerGnd, OUTPUT);
  digitalWrite(speakerGnd, LOW);
  pinMode(transistorGnd, OUTPUT);
  digitalWrite(transistorGnd, LOW);
  pinMode(A3, OUTPUT);
  digitalWrite(A3, LOW);
  pinMode(tilt_s1, INPUT);
  pinMode(tilt_s2, INPUT);
  
  analogWrite(ledPin, repulsorStable);
  
}

void loop() {
  // put your main code here, to run repeatedly: 
  
  int tilt = getTiltPosition();  // read the position of the tilt sensor
  Serial.print("Ready: ");
  Serial.print(readyToFire);  // status of whether or not the hand has been reset to prone position
  Serial.print(", Pos: ");
  Serial.println(tilt);  // position of tilt sensor
  delay(10);  // slight delay to get better readings of sensor
 
  
  if (tilt == 1) {  // if hand is in prone position
    
    if (readyToFire == false) {  // if blast has been fired 
      beginCount = true;  // begin counting time since hand has been in prone position
    }
    
    readyToFire = true;  // ability fire has been reset
    
    if (beginCount == true) {
      unsigned long previousMillis = millis();  // reset counter
      beginCount = false;  // counter will not be reset until it has reached
    }
    
    unsigned long currentMillis = millis();
    
    if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED 
      previousMillis = currentMillis;
      
      if (notDeactivated == true) {
        deactivate();
        notDeactivated = false;
      }
      
      do { 
      
        analogWrite(ledPin, 0);
        tilt = getTiltPosition();
        Serial.print("Pos: ");
        Serial.println(tilt);
        delay(10);
      
      } while (tilt == 1);
      
    }
    
  } else if (tilt == 2 && readyToFire == true) {
    
    readyToFire = false;
    repulsorFire();
    notDeactivated = true;
    
  }
  

}

int getTiltPosition(){
  int s1 = digitalRead(tilt_s1);
  int s2 = digitalRead(tilt_s2);
  return (s1 << 1) | s2; //bitwise math to combine the values
}

void repulsorFire() {
  
  for (int i = 2; i < 252; i+=2) {  // from 0 to 254
                
    analogWrite(ledPin, i);
    
    if(i > 125) { 
      delay(5);
    }

    delay(5);  // cause slight linear delay between fade steps
    thisPitch1 = 0;
    thisPitch2 = map(i, 2, 252, 1200, 1700);
    playDTMF(thisPitch1, thisPitch2, 30);

  }  // end for loop  
  
  for (int j = 50; j > 0; j--) {
      
    for (int k = 0; k < 20; k+=2) {
      
      analogWrite(ledPin, 12*k);  
      thisPitch1 = map(k, 20, 0, (k*14), (j*10));
      thisPitch2 = map(k, 20, 0, (k*36), (j*30));
      playDTMF(thisPitch1, thisPitch2, 30);
      delay(1);
    
    } // end for loop
  analogWrite(ledPin, 12*j);
  delay(1);
    
  } // end for loop
  
  analogWrite(ledPin, repulsorStable);

}  // end repulsorFire function

void deactivate () {
  
  for (int i = 200; i > 0; i-=2) {  // from 200 to 0
                
    analogWrite(ledPin, i/20);
    
    if(i < 100) { 
      delay(10);
    }

    delay(5);  // cause slight linear delay between fade steps
    thisPitch1 = 0;
    thisPitch2 = map(i, 200, 0, 1500, 1200);
    playDTMF(thisPitch1, thisPitch2, 30);

  }  // end for loop
  
}

void playDTMF(int number1, int number2, long duration)
{
  freq1.play(number1, duration);
  freq2.play(number2, duration);
}

Jabblaze:
Hello. I’m got this code from a friend and when I went to verify it it had errors. I’m new to coding and Arduino. What is wrong with this code?

#include <Tone.h>

Tone freq1;
Tone freq2;

const int ledPin = 6;
const int transistorGnd = 7;
const int repulsorSpeaker = 11;
const int speakerGnd = 10;
boolean readyToFire = true;
boolean beginCount = true;
boolean notDeactivated = true;
int repulsorStable = 10;
const int tilt_s1 = A2;
const int tilt_s2 = A1;

long previousMillis = 0;        // will store last time LED was updated
long interval = 12000;

int thisPitch1;
int thisPitch2;

void setup() {
  // put your setup code here, to run once:
 
  Serial.begin(9600);
 
  freq1.begin(repulsorSpeaker);
  freq2.begin(repulsorSpeaker);
  pinMode(ledPin, OUTPUT);
  pinMode(speakerGnd, OUTPUT);
  digitalWrite(speakerGnd, LOW);
  pinMode(transistorGnd, OUTPUT);
  digitalWrite(transistorGnd, LOW);
  pinMode(A3, OUTPUT);
  digitalWrite(A3, LOW);
  pinMode(tilt_s1, INPUT);
  pinMode(tilt_s2, INPUT);
 
  analogWrite(ledPin, repulsorStable);
 
}

void loop() {
  // put your main code here, to run repeatedly:
 
  int tilt = getTiltPosition();  // read the position of the tilt sensor
  Serial.print("Ready: “);
  Serial.print(readyToFire);  // status of whether or not the hand has been reset to prone position
  Serial.print(”, Pos: ");
  Serial.println(tilt);  // position of tilt sensor
  delay(10);  // slight delay to get better readings of sensor

if (tilt == 1) {  // if hand is in prone position
   
    if (readyToFire == false) {  // if blast has been fired
      beginCount = true;  // begin counting time since hand has been in prone position
    }
   
    readyToFire = true;  // ability fire has been reset
   
    if (beginCount == true) {
      unsigned long previousMillis = millis();  // reset counter
      beginCount = false;  // counter will not be reset until it has reached
    }
   
    unsigned long currentMillis = millis();
   
    if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;
     
      if (notDeactivated == true) {
        deactivate();
        notDeactivated = false;
      }
     
      do {
     
        analogWrite(ledPin, 0);
        tilt = getTiltPosition();
        Serial.print("Pos: ");
        Serial.println(tilt);
        delay(10);
     
      } while (tilt == 1);
     
    }
   
  } else if (tilt == 2 && readyToFire == true) {
   
    readyToFire = false;
    repulsorFire();
    notDeactivated = true;
   
  }

}

int getTiltPosition(){
  int s1 = digitalRead(tilt_s1);
  int s2 = digitalRead(tilt_s2);
  return (s1 << 1) | s2; //bitwise math to combine the values
}

void repulsorFire() {
 
  for (int i = 2; i < 252; i+=2) {  // from 0 to 254
               
    analogWrite(ledPin, i);
   
    if(i > 125) {
      delay(5);
    }

delay(5);  // cause slight linear delay between fade steps
    thisPitch1 = 0;
    thisPitch2 = map(i, 2, 252, 1200, 1700);
    playDTMF(thisPitch1, thisPitch2, 30);

}  // end for loop 
 
  for (int j = 50; j > 0; j–) {
     
    for (int k = 0; k < 20; k+=2) {
     
      analogWrite(ledPin, 12k); 
      thisPitch1 = map(k, 20, 0, (k
14), (j10));
      thisPitch2 = map(k, 20, 0, (k
36), (j30));
      playDTMF(thisPitch1, thisPitch2, 30);
      delay(1);
   
    } // end for loop
  analogWrite(ledPin, 12
j);
  delay(1);
   
  } // end for loop
 
  analogWrite(ledPin, repulsorStable);

}  // end repulsorFire function

void deactivate () {
 
  for (int i = 200; i > 0; i-=2) {  // from 200 to 0
               
    analogWrite(ledPin, i/20);
   
    if(i < 100) {
      delay(10);
    }

delay(5);  // cause slight linear delay between fade steps
    thisPitch1 = 0;
    thisPitch2 = map(i, 200, 0, 1500, 1200);
    playDTMF(thisPitch1, thisPitch2, 30);

}  // end for loop
 
}

void playDTMF(int number1, int number2, long duration)
{
  freq1.play(number1, duration);
  freq2.play(number2, duration);
}

This is very bad behavior. We are trying to help OP. You want some help you should start your own post, especially when your question has nothing to do with the OP’s question.

Oh I'm sorry. Like i said I'm new to this stuff. Sorry again.

Thanks for the replies! really appreciate it! i have read through the shields manual and it says nothing, it gave me an AT Command list, but i do not know how to apply it. Normally i would use PuTTy. But that would mean i am configuring the shield right? i would still need a sketch for the Arduino. is it possible for me to find out the functions by viewing the .cpp files?

This is the GSM library that iteadstudio recommends so I will assume that's the one you have:

I downloaded the documentation:

OpenOffice: https://github.com/jgarland79/GSM_Shield/raw/master/Library%20GSM_Shield.odt
Microsoft Word: https://github.com/jgarland79/GSM_Shield/raw/master/Library%20GSM_Shield.doc

In the documentation they show several functions for retrieving SMS text. I'd use this one:

char position;
char phone_number[20]; // array for the phone number string
char *sms_text;
    position = gsm.IsSMSPresent(SMS_UNREAD);
    if (position) { // read new SMS
        gsm.GetGSM(position, phone_number, &sms_text);
    }

You already have the "IsSMSPresent()" call. You just have to add the function to get the SMS. Then you can look at the text to see if it says "on".

Hi johnwasser, yes i am using the iComSat Shield, i have read through the documents, and tried using them, but i cannot get it to compile, it comes out errors. what do you mean by having a function to read the SMS?
would it be something like

if (sms_text) = "on"
pin 13 = HIGH

i came across a simpler code, but i do not know why it only work once

#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);
    }
    delay(1000);
  }
};

Thanks!