# How to change phone number in a program \[solved\]

**URL:** <https://forum.arduino.cc/t/how-to-change-phone-number-in-a-program-solved/909442>\
**Category:** Programming\
**Created:** [September 27, 2021, 8:49am UTC](https://forum.arduino.cc/t/how-to-change-phone-number-in-a-program-solved/909442 "2021-09-27T08:49:45Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![another141](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@another141](https://forum.arduino.cc/u/another141)\
**Post date:** [September 27, 2021, 8:49am UTC](https://forum.arduino.cc/t/how-to-change-phone-number-in-a-program-solved/909442/1 "2021-09-27T08:49:45Z")

</div>

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:

```cpp
#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);   
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [September 27, 2021, 9:38am UTC](https://forum.arduino.cc/t/how-to-change-phone-number-in-a-program-solved/909442/2 "2021-09-27T09:38:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![another141](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@another141](https://forum.arduino.cc/u/another141)\
**Post date:** [September 27, 2021, 9:44am UTC](https://forum.arduino.cc/t/how-to-change-phone-number-in-a-program-solved/909442/3 "2021-09-27T09:44:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![330R](https://avatars.discourse-cdn.com/v4/letter/3/d78d45/32.png) [@330R](https://forum.arduino.cc/u/330R)\
**Post date:** [September 27, 2021, 9:45am UTC](https://forum.arduino.cc/t/how-to-change-phone-number-in-a-program-solved/909442/4 "2021-09-27T09:45:27Z")

</div>

> [@gcjr](#):
>
> why not simply set the LED based on message from any phone that include "LED ON" or "LED OFF"?

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.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [September 27, 2021, 9:53am UTC](https://forum.arduino.cc/t/how-to-change-phone-number-in-a-program-solved/909442/5 "2021-09-27T09:53:47Z")

</div>

make the phone number string a variable

> [@another141](#):
>
> ` if (message.indexOf("+381601416081") > -1)`

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

---

<div class="post-metadata">

**Author:** ![330R](https://avatars.discourse-cdn.com/v4/letter/3/d78d45/32.png) [@330R](https://forum.arduino.cc/u/330R)\
**Post date:** [September 27, 2021, 10:01am UTC](https://forum.arduino.cc/t/how-to-change-phone-number-in-a-program-solved/909442/6 "2021-09-27T10:01:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![another141](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@another141](https://forum.arduino.cc/u/another141)\
**Post date:** [September 27, 2021, 10:05am UTC](https://forum.arduino.cc/t/how-to-change-phone-number-in-a-program-solved/909442/7 "2021-09-27T10:05:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [September 27, 2021, 10:44am UTC](https://forum.arduino.cc/t/how-to-change-phone-number-in-a-program-solved/909442/8 "2021-09-27T10:44:06Z")

</div>

> [@another141](#):
>
> then i need to exclude "NEW PHONE" string so only phone number is stored in a variable?

just overwrite it

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

```arduino
#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);
    }
}

```

---

<div class="post-metadata">

**Author:** ![another141](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@another141](https://forum.arduino.cc/u/another141)\
**Post date:** [September 27, 2021, 11:22am UTC](https://forum.arduino.cc/t/how-to-change-phone-number-in-a-program-solved/909442/9 "2021-09-27T11:22:42Z")

</div>

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

 ![sm](https://europe1.discourse-cdn.com/arduino/original/4X/f/0/9/f09757ab0483f57dcab208550294551a16a16be2.png)

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.

```arduino
 idx+3+telNum.length() 
```

> [@gcjr](#):
>
> ```arduino
> if (message.indexOf("NEW PHONE:") > -1) {
> int idx = message.indexOf(":");
> 
> telNum = message.substring(idx+1, idx+1+telNum.length());
> Serial.println (telNum);
> }
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [September 27, 2021, 11:25am UTC](https://forum.arduino.cc/t/how-to-change-phone-number-in-a-program-solved/909442/10 "2021-09-27T11:25:23Z")

</div>

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

`+381601416081 NEW PHONE:+123456789123`

---

<div class="post-metadata">

**Author:** ![another141](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@another141](https://forum.arduino.cc/u/another141)\
**Post date:** [September 27, 2021, 12:10pm UTC](https://forum.arduino.cc/t/how-to-change-phone-number-in-a-program-solved/909442/11 "2021-09-27T12:10:55Z")

</div>

It resulted in printing old phone number again but i found a solution.

```cpp
 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:

```cpp
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!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 21, 2022, 10:43am UTC](https://forum.arduino.cc/t/how-to-change-phone-number-in-a-program-solved/909442/13 "2022-08-21T10:43:45Z")

</div>

Closed on August 21, 2022, 10:43am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
