How to change phone number in a program [solved]

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);   
    }
  }
}

do you need to check for a specific phone number? why not simply set the LED based on message from any phone that include "LED ON" or "LED OFF"?

should only check for above under the condition where available() is true

Only defined number should be able to turn LED on or off, otherwise everyone would be able to control my program and that's not what I want.

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.

make the phone number string a variable

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

Perhaps a master phone's number should be hardcoded, so that the master phone alone has the power to add or revoke another number to the whitelist.

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 ":").

just overwrite it

consider (the MyHW stuff allows me to simulate it on my hardware)

#undef MyHW
#ifdef MyHW
# define SIM900  Serial
int led = 13;
String telNum = "+123456789012";

#else
# include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
int led = 9;
String telNum = "+381601416081";
#endif


String message;

// -----------------------------------------------------------------------------
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())
        return;

    message = SIM900.readString();

    if (message.indexOf(telNum) == -1)  {
        Serial.println ("invalid phone number");
        return;
    }

    if (message.indexOf("NEW PHONE:") > -1) {
        int idx = message.indexOf(":");

        telNum = message.substring(idx+1, idx+1+telNum.length());
        Serial.println (telNum);
    }

    else 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);
    }
}

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

When telNum is printed, number stays the same, minus 2 last numbers which can be fixed by adding 3 instead of 1 to variable idx.

 idx+3+telNum.length() 

Any idea why is this part of code not overwriting old phone number?

you need to enter the existing phone number for the sms message to be accepted.

+381601416081 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);
    }

Ty for helping me solve the problem kind sir!