Programmatically read SIM phone number

Hi,
Is that possible?
Nino

Hello Nino,

Thank you so much for sharing your question.

Please find answer below:

Yes it is possible. Please refer to the modem documentation from U-Blox:

On page 84 find Network service:

Subscriber number +CNUM: Returns the MSISDNs related to this subscriber. If the subscriber has different MSISDN for different services, each MSISDN is returned in a separate line.

MSISDN is read from the SIM.

Hope this helps!

Thanks for your clear reply Nachoherrera.

As I am not using AT commands in my program, only MKRGSM library functions, is there an alternative way or I must implement AT communication with the module?

You really can’t use AT commands directly in your library with any ease. I had some special functions I needed for my program so I edited the GSM library. First save your library in case you screw up. Open GSMModem.cpp and go to the bottom. Add something like this:
[
String GSMModem::getphnum() //This part goes into GSMModem.cpp
{
String _phnum;

_phnum.reserve(35);

MODEM.send(“AT+CNUM”);
MODEM.waitForResponse(100, &_phnum);

if (_phnum.startsWith("+CNUM: ")) {
_phnum.remove(0,6);
} else {
_phnum = “”;
}
return _phnum;
}

]

[
String getphnum(); //This part goes into GSMModem.h
]

Next go to GSMModem.h and add the final line at the bottom of the list of commands before the ENDIF:
When you recompile it will now have a new command getphnum() which will return a string with all but the +CNUM: part. You can figure out how to trim it based on your personal needs. Be sure to create a string and assign it to that string when you call the function like string phnum = getphnum();

In fact I was able to send commands and get responses by first setting up the u-blox:

pinMode(GSM_RESETN, OUTPUT);
digitalWrite(GSM_RESETN, HIGH);
delay(100);
digitalWrite(GSM_RESETN, LOW);
SerialGSM.begin(115200);
while(!SerialGSM);

and then sending the command:

SerialGSM.print("AT+");
SerialGSM.print(AT_COMMADN HERE);
SerialGSM.print("\r");
sleep(500);
String res = String("");
while (SerialGSM.available()) {
  char c = SerialGSM.read();
  res += c;
}

and it worked well but I had to stop using it because it turns that when I was writing the flash (using it as eeprom) and then going through above code, the board just hangs and needed a reset.

Since all I needed was to fetch the SIM phone number, I am now sending SMS from the board to a VOIP number which reviles the number.

Thanks anyways, I may go back to try your suggestion.