Specific phone number should trigger command

I have a program that allows me to trun LED on and off with SMS sending to gprs shield. I need a program that does the same thing but only specific phone number is allowed to trigger the command.
This is my current code:

#include <SoftwareSerial.h> //software serial library for serial communication b/w arduino & GSM

SoftwareSerial mySerial(8, 9);//connect Tx pin of GSM to pin 8 of arduino && Rx pin of GSM to pin no 9 of arduino

int led = 7;
String message;


void setup()
{
  mySerial.begin(9600);   // Setting the baud rate of GSM Module  
  Serial.begin(9600);    // Setting the baud rate of Serial Monitor (Arduino)
  delay(100);

  mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
  delay(1000);
  pinMode(led,OUTPUT);
  digitalWrite(led,LOW);
}

void loop()
{
  if (mySerial.available()>0){
  message = mySerial.readString();
  }

  if(message.indexOf("ON") > -1){
      Serial.println("LED ON");
     digitalWrite(led,HIGH);
     }
    else if(message.indexOf("OFF") > -1){

      Serial.println("LED OFF");
      digitalWrite(led,LOW);

  }

  delay(10);
}

What should i add in order to specify phone number, any tips?

Look in the AT command set for the module, for a method to obtain the originating number.

Print the entire message you get to the serial port so you can see the format and hopefully, where the sender's number is.

aarg:
Look in the AT command set for the module, for a method to obtain the originating number.

There is AT command to see caller ID when i make a call to the shield and it is AT+CLIP but how do i make a condition out of it, it just prints the caller ID on serial monitor.

if (message == "xxxxx") {
   //do something
}

pcbbc:

if (message == "xxxxx") {

//do something
}

No, I tried that, that's not how it works.

If you can test for "ON" or "OFF" you should be able to test for "867-5309" or whatever the number is, in exactly the same way. Why can't you? Could you post the response to the AT+CLIP here, so we can see it?

another141:
No, I tried that, that's not how it works.

Try strcmp().

aarg:
If you can test for "ON" or "OFF" you should be able to test for "867-5309" or whatever the number is, in exactly the same way. Why can't you? Could you post the response to the AT+CLIP here, so we can see it?

When it comes to AT+CLIP i have a program where it is displayed when i call the shield, it looks like this:

#include <SoftwareSerial.h>


SoftwareSerial mySerial(9, 10); 

void setup()
{
  
  //Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
  Serial.begin(9600);
  
  //Begin serial communication with Arduino and SIM900
  mySerial.begin(9600);

  Serial.println("Initializing..."); 
  mySerial.print("AT+CLIP=1\r"); //This is for displaying caller ID
  delay(100);
}

void loop()
{
  updateSerial();
}

void updateSerial()
{
  delay(500);
  while (Serial.available()) 
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while(mySerial.available()) 
  {
    Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
  }
}

Now this is what i get in serial monitor when i call it

AT+CLIP=1
OK

RING

+CLIP: "+381601416081",145,"","",0

NO CARRIER

Now i need to turn on LED by calling or sending message but only from this specific phone number that is displayed in serial monitor, so other phone numbers can't turn LED on. All i need is a hint/advice on how to do it.

dougp:
Try strcmp().

So what i need to do is compare the specific phone number that is allowed to trigger the command with the incoming phone number and if they match, led will turn on otherwise it wont? Is it correct? Any tips?

Wow, I gave you the complete answer. All the logic for doing this is already built into your program. You are already recognizing "ON" and "OFF". They are strings. "+381601416081" is also a string. So recognize it exactly the same way you did "ON" and "OFF". It is really simple. If there is some special reason why you think that won't work, then you should explain it.

aarg:
Wow, I gave you the complete answer. All the logic for doing this is already built into your program. You are already recognizing "ON" and "OFF". They are strings. "+381601416081" is also a string. So recognize it exactly the same way you did "ON" and "OFF". It is really simple. If there is some special reason why you think that won't work, then you should explain it.

Yes you did kind sir and it works now, thank you! Now I can turn LED on by calling from this specific number.
Do you have any idea how can i also turn LED off by calling second time from this same phone number?

I tried to do it with bool, but it doesn't work. It looks like this:

if(callid.indexOf("+381601416081") > -1){
    if(change== false){
      Serial.println("LED ON");
     digitalWrite(led,HIGH);
     change = true;
     }
  }
    else if(callid.indexOf("+381601916081") > -1){
      if (change == true){
      Serial.println("LED OFF");
      digitalWrite(led,LOW);
      change = false;
      }
  }

Quite obviously if you have already matched on the if with...

 if(callid.indexOf("+381601416081") > -1){

How are you ever going to get to the else, which matches on the same condition...

 else if(callid.indexOf("+381601916081") > -1){

Edit: Sorry I see it’s NOT the same number. Well if that’s the correct number you have a different number to turn it off.

If you require the same number to act as a toggle you need....

 if(callid.indexOf("+381601416081") > -1){
    if(change== false){
       Serial.println("LED ON");
       digitalWrite(led,HIGH);
       change = true;
     } else {
       Serial.println("LED OFF");
       digitalWrite(led,LOW);
       change = false;
     }
  }

pcbbc:
Sorry I see it’s NOT the same number. Well if that’s the correct number you have a different number to turn it off.

No, my bad it is supposed to be the same number.

pcbbc:
If you require the same number to act as a toggle you need....

 if(callid.indexOf("+381601416081") > -1){

if(change== false){
      Serial.println("LED ON");
      digitalWrite(led,HIGH);
      change = true;
    } else {
      Serial.println("LED OFF");
      digitalWrite(led,LOW);
      change = false;
    }
  }

It actually blinks when i call it like that. I suppose it is because every new beep (ring) is new if condition.

Presumably the number is sent more than once while it is ringing.
Show us a complete list of what appears in the serial monitor.
You need to detect something that shows when a call starts to be received, then do the toggle action only once per call.

I doubt your explanation. Please post the complete updated sketch.

pcbbc:
Presumably the number is sent more than once while it is ringing.
Show us a complete list of what appears in the serial monitor.
You need to detect something that shows when a call starts to be received, then do the toggle action only once per call.

this appears in serial monitor when it rings only one time

LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
LED ON

This is full code:

  #include <SoftwareSerial.h> 
SoftwareSerial mySerial(9, 10);

int led = 3;
String callid;
bool change = false;

void setup()
{
  mySerial.begin(9600);  
  Serial.begin(9600);    
  delay(100);

  pinMode(led,OUTPUT);
  digitalWrite(led,LOW);
}

void loop()
{
  if (mySerial.available()>0){
  callid = mySerial.readString();
  }

 if(callid.indexOf("+381601416081") > -1){
    if(change== false){
       Serial.println("LED ON");
       digitalWrite(led,HIGH);
       change = true;
     } else {
       Serial.println("LED OFF");
       digitalWrite(led,LOW);
       change = false;
     }
  }

  delay(10);
}

I can't display phone number with AT+CLIP command in serial monitor but it doesn't really matter.
it prints so many LED ON/LED OFF if i call it even for a second.

Maybe, because every time through loop string 'callid' is still set to what is was previously when no new data is available?

Try...

if (mySerial.available()>0){
  callid = mySerial.readString();
}
else
{
  callid = "";
}

Edit: Or move the other tests inside the if (mySerial.available())" if statement.

If that doesn't help include what the modem is sending you and what you are receiving via callid = mySerial.readString(); in the debug...

 if (mySerial.available()>0){
  callid = mySerial.readString();
  Serial.println(callid);
  }

Then we can see what the modem sends you (maybe it sends the phone number more than once per call), and maybe for some other messages it sends we can decide when a call starts and when a call stops. When we know that we can make the toggle do something only once per initiated call.

pcbbc:
Maybe, because every time through loop string 'callid' is still set to what is was previously when no new data is available?

Try...

if (mySerial.available()>0){

callid = mySerial.readString();
}
else
{
  callid = "";
}

Yes, that was the problem. When i added "else {callid = "";} "it was fixed. Now it prints only one LED ON and LED OFF with every new beep. I don't know how to make the toggle do something only once per call but i limited it with millis by 10 seconds. Maybe it's not the best solution but it works now.

I upgraded my code and it looks like this:

 #include <SoftwareSerial.h> 
SoftwareSerial mySerial(9, 10);

int led = 3;
String callid;
bool change = false;
bool thisIsTheFirstTime = true;
unsigned long ledTurnedOffAt;
unsigned long ledTurnedOnAt;

void setup()
{
  mySerial.begin(9600);  
  Serial.begin(9600);    
  delay(100);

  pinMode(led,OUTPUT);
  digitalWrite(led,LOW);
}

void loop()
{
  if (mySerial.available()>0){
  callid = mySerial.readString();
  }else
  {
    callid = ""; //so callid is not set to what it was 
  }


 if(callid.indexOf("+381601416081") > -1){
    if(change== false && (millis() - ledTurnedOffAt >= 10000 || thisIsTheFirstTime)){ 
       Serial.println("LED ON");
       ledTurnedOnAt = millis();
       thisIsTheFirstTime = false;
       digitalWrite(led,HIGH);
       change = true;
     } if(change == true && millis() - ledTurnedOnAt >= 10000)  {
       Serial.println("LED OFF");
       ledTurnedOffAt = millis();
       digitalWrite(led,LOW);
       change = false;
     }
  }

  delay(50);
}

Well done on fixing it yourself with millis(). A lot of people might have fixed it with long delays.

another141:
I don't know how to make the toggle do something only once per call

The way to do that is find the string that comes from the GPRS modem when a call is started.

The only way for us to know what that trigger string should be (or at least those of us who do not have your particular GPRS shield to play with) is for YOU to provide US with the messages that YOUR shield is sending you.

From the previous debug output you provided in post #8 it seems like it might send "RING" when a call starts? Is that correct?

In which case, assuming it only sends "RING" once per call, but the number multiple times, you can do...

#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);

int led = 3;
String callid;
bool change = false;
bool newRing = false;

void setup()
{
  mySerial.begin(9600);
  Serial.begin(9600);
  delay(100);

  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
}

void loop()
{
  if (mySerial.available() > 0) {
    callid = mySerial.readString();

    if (callid.indexOf("RING") > -1)
    {
      newRing = true;
    }

    if (newRing && callid.indexOf("+381601416081") > -1) {
      if (change == false)
      {
        Serial.println("LED ON");
        digitalWrite(led, HIGH);
        change = true;
      }
      else
      {
        Serial.println("LED OFF");
        digitalWrite(led, LOW);
        change = false;
      }
      newRing = false;
    }
  }
}

Otherwise you need to look for something that occurs only once per call and use that to set the 'newRing' flag.