GSM and Arduino

I have this code that recieves the ams, pushes it to the serial monitor and i then if the ams is “1”, i want to either send text to the serial monitor or i want to turn an led on or off, i have tried the ‘indexOf()’ command but it doesnt recognise it.
this is the code

#include <GSM.h>
char txtMsg[200]="Status";
int incomingByte = 0;
char inchar;

// PIN Number for the SIM
#define PINNUMBER ""

// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;

// Array to hold the number a SMS is retreived from
char remoteNumber[20]; 

const int led1 = 6;
int valled1;

void setup() 
{
  pinMode(led1, OUTPUT);
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  } 

  //Serial.println("SMS Auto Responder");
    
  // connection state
  boolean notConnected = true;
  
  // Start GSM connection
  while(notConnected)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }
  
  Serial.println("GSM initialized");
  //Serial.println("Waiting for messages");
}

void loop() 
{
  char c;
  
  // If there are any SMSs available()  
  if (sms.available())
  {
    //Serial.println("Message received from:");
    
    // Get remote number
    sms.remoteNumber(remoteNumber, 20);
    //Serial.println(remoteNumber);

    // An example of message disposal    
    // Any messages starting with # should be discarded
    if(sms.peek()=='#')
    {
      //Serial.println("Discarded SMS");
      //sms.flush();
    }
    
    // Read message bytes and print them
    while(c=sms.read())
      Serial.print(c);
    
      checkSMS();
        //sendSMS();
 
      
    //Serial.println("\nEND OF MESSAGE");
    
    // Delete message from modem memory
    sms.flush();
    //Serial.println("MESSAGE DELETED");
  }

  delay(1000);

}
void sendSMS(){

  //Serial.print("\nMessage to mobile number:\n ");
  //Serial.println(remoteNumber);

  // sms text
  //Serial.println("SENDING");
  //Serial.println();
  //Serial.println("Message:");
  //Serial.println(txtMsg);

  // send the message
  sms.beginSMS(remoteNumber);
  sms.print(txtMsg);
  sms.endSMS(); 
  Serial.println("\nSENT!\n");  
}

void checkSMS(){
  
      inchar=sms.read(); 
      if (inchar=='a')
      {
        delay(10);
        inchar=sms.read();
        
        if (inchar=='0')
        {
          Serial.println("on");
        } 
        else if (inchar=='1')
        {
          Serial.println("off");
        }
        delay(10);
   }
   else
        {
         Serial.println("broken"); 
        }
}

i have tried the 'indexOf()' command but it doesnt recognise it.

indexOf() isn't a command. It isn't a function, either. It's a method of the String class. You don't have, or want, an instance of the String class.

On the other hand, you don't have a string, either. You aren't actually collecting the data from the SMS.

PaulS:

i have tried the 'indexOf()' command but it doesnt recognise it.

indexOf() isn't a command. It isn't a function, either. It's a method of the String class. You don't have, or want, an instance of the String class.

On the other hand, you don't have a string, either. You aren't actually collecting the data from the SMS.

Could you please let me know of the best way to do this :slight_smile:

Could you please let me know of the best way to do this

You need an array:

char smsMsg[200]=;

You need an index variable:

byte index = 0;

In loop(), change:

    while(c=sms.read())
      Serial.print(c);

to

    index = 0;
    while(c=sms.read())
    {
      Serial.print(c);
      smsMsg[index] = c;
      index ++;
      smsMsg[index] = '\0';
    }

After this while loop, smsMsg will contain whatever was obtained from the phone. It is probably not going to be a complete message. You need to determine what constitutes a complete message. A complete message starts with some character (set index to 0 when that character arrives) and ends with some character. Only when that character arrives should you use smsMsg.

thank you, so after this, can i carry on with my if statement?

if (index=='0')
        {
          Serial.println("on");
        } 
        else if (index=='1')
        {
          Serial.println("off");
        }
        delay(10);
   }

thank you, so after this, can i carry on with my if statement?

index is an array index. It will equal 0, 1, 2, 3, etc. smsMsg might contain "1", "Old shoe", etc.

Sorry about all the questions, i real am a newb but i am learning, so this means that that if statement will work?
or what would be a suitable way of going about this statement?

so this means that that if statement will work?

No. If you print out smsMsg and index, you'll see why.

Ok. I will try when i get home, thank you, ill let you know what happens

PaulS:

so this means that that if statement will work?

No. If you print out smsMsg and index, you’ll see why.

I really do apologise for being so sloppy, i have used the method that you have given me and it works :slight_smile: althou i dont know how to use it in a comparison

this is what i have been messing with

#include <GSM.h>
char txtMsg[200]="Status";

char smsMsg[200]="";

int incomingByte = 0;
char inchar;

byte index = 0;

// PIN Number for the SIM
#define PINNUMBER ""

// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;

// Array to hold the number a SMS is retreived from
char remoteNumber[20]; 

const int led1 = 6;
int valled1;

void setup() 
{
  pinMode(led1, OUTPUT);
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  } 

  //Serial.println("SMS Auto Responder");
    
  // connection state
  boolean notConnected = true;
  
  // Start GSM connection
  while(notConnected)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }
  
  Serial.println("GSM initialized");
  //Serial.println("Waiting for messages");
}

void loop() 
{
  char c;
  
  // If there are any SMSs available()  
  if (sms.available())
  {
    //Serial.println("Message received from:");
    
    // Get remote number
    sms.remoteNumber(remoteNumber, 20);
    //Serial.println(remoteNumber);

    // An example of message disposal    
    // Any messages starting with # should be discarded
    if(sms.peek()=='#')
    {
      //Serial.println("Discarded SMS");
      //sms.flush();
    }
    
    // Read message bytes and print them
    index = 0;
    while(c=sms.read())
    {
      Serial.print(c);
      smsMsg[index] = c;
      index ++;
      smsMsg[index] = '\0';
    }
    //Serial.println();
    //Serial.print(index);
    Serial.println();
    
    Serial.print(smsMsg);
    Serial.println();
    
      checkSMS();
        //sendSMS();
 
      
    //Serial.println("\nEND OF MESSAGE");
    
    // Delete message from modem memory
    sms.flush();
    //Serial.println("MESSAGE DELETED");
  }

  delay(1000);

}
void sendSMS(){

  //Serial.print("\nMessage to mobile number:\n ");
  //Serial.println(remoteNumber);

  // sms text
  //Serial.println("SENDING");
  //Serial.println();
  //Serial.println("Message:");
  //Serial.println(txtMsg);

  // send the message
  sms.beginSMS(remoteNumber);
  sms.print(txtMsg);
  sms.endSMS(); 
  Serial.println("\nSENT!\n");  
}

void checkSMS(){
  
  
      char c;
      c=sms.read();
    
      smsMsg[index] = c;
      index ++;
      smsMsg[index] = '\0';
        
        if (c=='on')
        {
          Serial.println("on");
        } 
        else if (c=='off')
        {
          Serial.println("off");
        }
        
   else
        {
         Serial.println("broken"); 
        }
}

Confusing to me...

    index = 0;
    while(c=sms.read())
    {
      Serial.print(c);
      smsMsg[index] = c;
      index ++;
      smsMsg[index] = '\0';
    }
    //Serial.println();
    //Serial.print(index);
    Serial.println();
    
    Serial.print(smsMsg);
    Serial.println();
    
      checkSMS();

The only data that will be available for checkSMS to read will be those that arrive while smsMsg and two carriage returns and two line feeds are sent to the serial port. I don't understand why you are reading SMS text in two places.

Confusing to me...

I'm confused about what confuses you.

Hi, sorry. i have got all those read smsMsg there because i was trying something, it confuses me how to get this working...

Hi, sorry. i have got all those read smsMsg there because i was trying something, it confuses me how to get this working...

First, you must read sms data in only one place.

Second, you must explain what "get this working" means.

I was under the impression that you could read the text messages, and print index and smsMsg. If that is true, you should see that index is the number of characters in the message, and smsMsg is the message (text).

You can use strcmp() to compare smsMsg to "banana", to see if you should do whatever banana means. Or, something like that, where the text to compare to means something to you.

i want this to read the sms and turn led on or off.
so i could use

if(strcmp(smsMsg)=='banana')
                    {

                    }

would that work or what would i need to do to get that to work?

would that work

No. You clearly didn't look up any documentation of strcmp.

if(strcmp(smsMsg, "banana") == 0)
{
   // Message was banana
}

@PaulS
Some month ago but this post helped me a lot to.
So a final thank you!