Get phone number from a sent SMS

Hi guys, how can I get the phone number of the sender that is received by the GSM module and use it for sending a reply to that number instead of manually setting the phone number on the code? I am working with this code as of now.

#include <TinyGPS.h>
#define GPRS Serial1
#define GPS Serial2

TinyGPS Neo6m;

String TextMessage;
String AlarmStatus = "off";
String PhoneNumber = "+63**********";
int aled = 4;
int bled = 5;

void setup() {
  pinMode(aled, OUTPUT);
  pinMode(bled, OUTPUT);

  Serial.begin(9600);
  delay(500);
  GPRS.begin(9600);
  delay(500);
  GPS.begin(9600);

  delay(5000);
  Serial.print("SYSTEM IS READY!");
  GPRS.print("AT+CMGF=1\r");
  delay(100);
  GPRS.print("AT+CNMI=2,2,0,0,0\r");
  delay(100);
}

void loop() {
  
  if (GPRS.available() > 0) {
    TextMessage = GPRS.readString();
    Serial.print(TextMessage);
    delay(10);
    TextMessage.toUpperCase();
  }
  if (TextMessage.indexOf("RING") >= 0) {
    digitalWrite(aled, HIGH);
    digitalWrite(bled, HIGH);
    AlarmStatus = "on";
    Serial.println("Command: Alarm set to ON!");
    TextMessage = "";
  }
  if (TextMessage.indexOf("STOP") >= 0) {
    // Turn off relay and save current state
    digitalWrite(aled, LOW);
    digitalWrite(bled, LOW);
    AlarmStatus = "off";
    Serial.println("Command: Alarm set to OFF!");
    TextMessage = "";
  }
  if (TextMessage.indexOf("STATE") >= 0) {
    String AlarmStatusMessage = "Alarm is " + AlarmStatus;
    AlarmStatusSMS(AlarmStatusMessage);
    Serial.println("Command: Alarm status request!");
    TextMessage = "";
  }
  while (GPS.available()) {
    if (Neo6m.encode(GPS.read())) {
      if (TextMessage.indexOf("LOCATION") >= 0) {
        float latitude, longitude;
        Neo6m.f_get_position(&latitude, &longitude);
        float lat = latitude;
        float lng = longitude;

        String meslat = String(lat, 6);
        String meslon = String(lng, 6);
        String com = ",";
        String link = "https://www.google.com/maps/?q=";
        String CoordinateMessage = link + meslat + com + meslon;
        Serial.println(CoordinateMessage);
        delay(500);
        GPRS.print("\r");
        delay(500);
        GPRS.print("AT+CMGF=1\r");
        delay(500);
        GPRS.print("AT+CMGS=\"");
        GPRS.print(PhoneNumber);
        GPRS.print("\"\r");
        delay(500);
        GPRS.print(CoordinateMessage);
        delay(500);
        GPRS.write(0x1A);
        delay(500);
        Serial.println("Command: Coordinates request!");
        TextMessage = "";
      }
    }
  }
}
void AlarmStatusSMS(String AlarmStatusMessage) {
  GPRS.print("AT+CMGF=1\r");
  delay(100);
  GPRS.print("AT+CMGS=\"");
  GPRS.print(PhoneNumber);
  GPRS.print("\"\r");
  delay(100);
  GPRS.print(AlarmStatusMessage);
  delay(100);
  GPRS.print((char)26);
  delay(100);
}

Thanks in advance.

how can I get the phone number of the sender that is received by the GSM module

I would guess that there is some AT command to do that. What that specific AT command is is not consistent across all GPRS modules. Since you haven't said which GPRS module you have, you'll have to determine the AT commands it understands, and send it the proper one.

You can do it 2 ways

a. using the specific AT command to read the memory (as previous post). Of course (if not the default) first you have to energise "storing" .

b. extracting from the received message: Search the message for a valid sequence of characters.
ie if the number start with 693xxxxx search for 693
be carefull : automated messages do not follow above rule : for example, in this sms:

"34@637w6@64756","","16/07/14,13:58:33+12"
EXEIS 12.22 EwS 19/09/2016.

the "number" is 34@637w6@64756 (and the text is "EXEIS 12.22 EwS 19/09/2016").

PaulS:
I would guess that there is some AT command to do that.

I've searched for that AT command and it is AT+CNUM sir.

PaulS:
Since you haven't said which GPRS module you have, you'll have to determine the AT commands it understands, and send it the proper one.

I'm using Sim900 sir.

demkat1:
You can do it 2 ways

a. using the specific AT command to read the memory (as previous post). Of course (if not the default) first you have to energise "storing" .

b. extracting from the received message: Search the message for a valid sequence of characters.
ie if the number start with 693xxxxx search for 693
be carefull : automated messages do not follow above rule : for example, in this sms:

"34@637w6@64756","","16/07/14,13:58:33+12"
EXEIS 12.22 EwS 19/09/2016.

the "number" is 34@637w6@64756 (and the text is "EXEIS 12.22 EwS 19/09/2016").

I'm trying to use the AT command sir.

Update: The AT+CNUM does not work sir, it only replies OK.

Darren-June:
Update: The AT+CNUM does not work sir, it only replies OK.

you must find the manual. Google "sim900 at commands"

EDIT : must look the manual for format.

EDIT : the following is NOT correct
"AT&CNUM" returns :ok
"AT&CNUM=?" returns what you want, but must look the manual for format.

Is there any other way sir? Easier way, maybe.

Hello Darren-june.did you find a way around it? I am using a gsm sim 800l but I can't figure a way to extract the received text.