简体   繁体   中英

How to prevent other phone/mobile numbers to send SMS command to Arduino?

INTRO:

I'm a novice when it comes to Arduino programming and using AT commands. I already tried to search the whole internet and asked on Arduino forum, but I have no luck and nobody seems to give me a clear idea about it there.

PROBLEM:

So, I have this code where an SMS command can switch on and off the light and it will response to a specific phone number only. My problem is, the program response even when I'm using different phone numbers. I hope there's a way which it can only whitelist a specific number so no one can prank the program without the owner's knowledge.

FOR EXAMPLE:

  • The owner's phone number is +631234567890
  • Some random phone number: +63xxxxxxxxxx

The owner can switch on and off the light. [YES]
But supposedly, the random phone number CAN NOT and will never have the authority to switch the lights on and off. Only the owner can.

HERE'S MY CURRENT CODE: CCTO

#include <SoftwareSerial.h>
SoftwareSerial GPRS(10, 11);
String textMessage;
String lampState;
const int relay = 12;

void setup() {  
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);

  Serial.begin(9600); 
  GPRS.begin(9600);
  delay(5000);
  Serial.print("GPRS ready...\r\n");
  GPRS.print("AT+CMGF=1\r\n"); 
  delay(1000);
  GPRS.print("AT+CNMI=2,2,0,0,0\r\n");
  delay(1000);
}

void loop(){
  if(GPRS.available()>0){
    textMessage = GPRS.readString();
    Serial.print(textMessage);    
    delay(10);
  } 
  if(textMessage.indexOf("ON")>=0){
    // Turn on relay and save current state
    digitalWrite(relay, HIGH);
    lampState = "ON";
    Serial.println("Lamp set to ON\r\n");  
    textMessage = "";
    GPRS.println("AT+CMGS=\"+631234567890\"");
    delay(500);
    GPRS.print("Lamp was finally switched ON.\r");
    GPRS.write( 0x1a );
    delay(1000);
  }
  if(textMessage.indexOf("OFF")>=0){
    // Turn off relay and save current state
    digitalWrite(relay, LOW);
    lampState = "OFF"; 
    Serial.println("Lamp set to OFF\r\n");
    textMessage = "";
    GPRS.println("AT+CMGS=\"+631234567890\"");
    delay(500);
    GPRS.print("Lamp was finally switched OFF.\r");
    GPRS.write( 0x1a );
    delay(1000);
  }
  if(textMessage.indexOf("STATUS")>=0){
    String message = "Lamp is " + lampState;
    GPRS.print("AT+CMGF=1"); 
    delay(1000);
    Serial.println("Lamp state resquest");
    textMessage = "";
    GPRS.println("AT+CMGS=\"+631234567890\"");
    delay(500);
    GPRS.print("Lamp is currently ");
    GPRS.println(lampState ? "ON" : "OFF");
    GPRS.write( 0x1a );
    delay(1000);
  }
}

How can I do that?

Your textMessage must contain an information about sender and time of the message. Something like this:

+CMGL: 2,"REC UNREAD","+63xxxxxxxxxx",,"07/02/18,00:07:22+32" A simple demo of SMS text messaging.

So, you need to extract the phone number at compare it with the authorized one.

You can define the desired phone number in the code and compare it with the number assigned to the incoming SMS. If the number matches the one in your code, proceed with the algorithm. Otherwise, ignore the message.

Got this code from the official Arduino Forum, which you can check out from the link given at the end. You can code out these two functions:


String CellNumtemp;
String CellNum;


// check if there are incoming SMS
void ricezione(){
  Serial.println ("controllo ricezione SMS");
  // AT command to set mySerial to SMS mode
  mySerial.print("AT+CMGF=1\r");
  delay(100);
  // Read the first SMS saved in the sim
  mySerial.print("AT+CMGR=1\r");
  delay(10); 
  if(mySerial.available()>0){
    textMessage = mySerial.readString();
    Serial.print(textMessage); 
    delay(10);
  }
  // check if the SMS is "STATO"
  if(textMessage.indexOf("STATO")>=0){   
    Serial.println("Invio info stato arnia");
    //save the phone number of the senders in a string (this works with italian region you must adapt to   yours) 
    CellNumtemp = textMessage.substring(textMessage.indexOf("+39"));
    CellNum = CellNumtemp.substring(0,13);
    smsstato();
    CellNumtemp = "";
    textMessage = "";   
  }
  mySerial.print("AT+CMGD=1\r");
  delay(100);
  mySerial.print("AT+CMGD=2\r");
  delay(100);   
}


// Send sms with all the information to the number stored
void smsstato(){
  // delete the first SMS
  mySerial.print("AT+CMGD=1\r");
  delay(100);
  mySerial.print("AT+CMGF=1\r");   
  delay(1000);
  mySerial.print("AT+CMGS=\"");
  mySerial.print(CellNum);
  mySerial.print("\"\r"); 
  delay(1000);
  //The text of the message to be sent.
  mySerial.print("INFO: Latitudine: ");
  mySerial.print(latitude);
  mySerial.print(", Longitudine: ");
  mySerial.print(logitude);
  mySerial.print(", Ampere: ");
  mySerial.print(consumotemp);
  delay(1000);
  mySerial.write(0x1A);
  delay(1000);
  Serial.println("sms stato");
}```

Thanks to the user ilteo85 on the https://forum.arduino.cc.
Check this out for reference: https://forum.arduino.cc/index.php?topic=637264.0

you could just use the whitelisting function in gsm module

the cmd is AT+CWHITELIST=mode,index,number

Parameters 0 Disable 1 Enable only call white list 2 Enable only SMS white list 3 Enable call and SMS white list The index of phone number, scope: 1-30 Phone number to be set

example AT+CWHITELIST=2,1,+9198xxxxxxx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM