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?
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 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?
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.
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:
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.
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
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.
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.
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...