539Views11Replies

Author Options:

How can I let Arduino GSM shield respond to specific numbers? Answered

I am working on a project where i send 1 to my GSM Shield to switch on the LED connected to pin 13 and off by sending 0, but i would like to modify the code in order for the GSM Shield to respond only to the number specified. Here is my code, in my case I used an example phone number ... 


********************************************************************************************************************

// include the GSM library
#include <GSM.h>

// 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 senderNumber[20];
char adminNumber[20]="+123456789234";


void setup()
{
 
  // initialize digital pin 13 as an output.
  pinMode(13, 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 Messages Receiver");

  // 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;

  sms.remoteNumber(senderNumber, 20);
  sms.remoteNumber(adminNumber, 20);
 
    // If there are any SMSs available()
  if (sms.available())
  {
    Serial.println("Message received from:");

     // Get remote number
    sms.remoteNumber(senderNumber, 20);
    Serial.println(senderNumber);


       // Any number besides admin number will not change the state of the pin
    while(adminNumber == senderNumber){
        // Any messages starting with 1 will set pin 13 to HIGH
if (sms.peek() == '1'){
      digitalWrite(13, HIGH);
     sms.flush();
    }
        // Any messages starting with 0 will set pin 13 to LOW
else if(sms.peek() == '0'){
      digitalWrite(13, LOW);
   
        sms.flush()
}
    }

   

    // Read message bytes and print them
    while (c = sms.read())
      Serial.print(c);
 
    Serial.println("\nEND OF MESSAGE");

    // Delete message from modem memory
    sms.flush();
    Serial.println("MESSAGE DELETED");
  }

  delay(1000);


}

********************************************************************************************************************

Discussions

0
None
steveastrouk

2 years ago

What do YOU mean by "respond only to the number specified"

The way its written, it should respond only to an SMS from the "correct" number.

0
None
Mechatron147steveastrouk

Answer 2 years ago

That's what I want but its not working. By number specified I mean the number in the array "adminNumber"

0
None
steveastroukMechatron147

Answer 2 years ago

So its responding to every number, or not even the right number ?

0
None
Mechatron147steveastrouk

Answer 2 years ago

It skips the while loop where i compared the senderNumber to adminNumber... It displays the message content and the senderNumber but the LED doesn't switch on

0
None
steveastroukMechatron147

Answer 2 years ago

Your string contains extra characters. Search for the adminnumber as a string within the sendernumber, not an == statement

0
None
Mechatron147

2 years ago

Sorry I didn't get what you mean. Can you clarify?

0
None
Downunder35mMechatron147

Answer 2 years ago

??
There are two IF statements defining what happens when a number is received - change them to what you need.
Sorry but can't make it any clearer.

0
None
Mechatron147steveastrouk

Answer 2 years ago

Actually I used the Send SMS example from the IDE and modified it

0
None
Downunder35m

2 years ago

So why don't you change it, it is just two IF statements...