Hi, I have a simple program where LED is truned ON or OFF by specific SMS content from a specific phone number. (I am using GPRS GSM sim900 shield).
Is there a way to change that specific phone number that turns LED on or off by sending some SMS to the shield, so I don't have to change it in a code every time I want to use another phone number to turn LED on/off.
Here's full code:
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
String message;
int led = 9;
void setup() {
SIM900.begin(19200);
Serial.begin(19200);
SIM900.print("AT+CMGF=1\r");
SIM900.print("AT+CNMI=2,2,0,0,0\r");
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
void loop() {
if (SIM900.available() > 0) {
message = SIM900.readString();
} else
{
message = "";
}
if (message.indexOf("+381601416081") > -1) { //phone number that I want to change with SMS command
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);
}
}
}
If this is going to work with any phone, I would at least add a password check as a security feature, like, say: 123456# LED ON. Random folk turning an LED on and off might not be a problem for the time being, but I suppose that the OP has some other application in mind.
char telNum [] = "+381601416081";
...
if (message.indexOf(teNum) > -1)
add a command to allow a new phone number to be recognized, for example "NEW PHONE:", and overwrite the phone number string with the text following the ":".
So complete SMS content would be for example: "NEW PHONE: +XXXXXXXXX" and then i need to exclude "NEW PHONE" string so only phone number is stored in a variable?
That's what i was thinking about but I don't know how to exclude "NEW PHONE" string and only store phone number (text following the ":").
I understand the logic but for some reason old number is not overwritten by new number.
This is what i get in serial monitor when i write NEW PHONE:+123456789123
It resulted in printing old phone number again but i found a solution.
int idx = message.indexOf(":");
Starts counting from the first ":" ("time and date" string also appears for some reason and it uses colons), and when message is sent ":" appears before the colon (":") it needs to index from, so I took the index of ("PHONE:") and added 6 to variable idx to make sure it stats from the colon (":") I need.
This works:
if (message.indexOf("NEW PHONE:") > -1) {
int idx = message.indexOf("PHONE:"); //changed from (":")
telNum = message.substring(idx+6, idx+3+telNum.length());
Serial.println (telNum);
}