sms commands via a gsm modem working !

Ive noticed loads of people are having trouble reading sms from a gsm modem using AT commards . I have a simple solution it works well but may not be the best way though i thought i would share it.

Sending the following AT commard at start up : AT+CNMI=1,2,0,0,0 to format new sms in the following format.

“LED_ON” is Sent from a phone…

+CMGR: “REC UNREAD”,“077*********”,“10/07/19,13:18:44+04”

In this example we are only going only read “LED_ON” which is in the message body. We are not going to look at infomation in the header.

Using a serial analyzer i found the modem returns commards in the following format


Using the following code by Luke Orland you can read the line

You can now act on sms body example sms send “LED_ON”

void loop() {
  if (lineAvailable(MAX_LINE,line))
    if (strcmp (line,"LED_ON") == 0)



If anyone can add info on how to split the header into array that that be good.

Hope this helps .


I would think you could use the below string library to parce out most anything in your "line" string.

hey thanks for your suggestion , Im a bit unsure where to start can you suggested a method of getting the telephone number out of the following string : +CMGR: "REC UNREAD","077*********",,"10/07/19,13:18:44+04"

perhaps can we string right ect ? or maybe split it using "," as delimater. ect. ?

You could use the indexOf method to locate a ,. Then, form a substring of the data before or after the comma. As long as the number of fields is constant, the nth field should always be the phone number.

If the number of fields varies, you'd need to check the length of the phone number string to verify that it is at least long enough to be a phone number, but not too long.

Below is what I use to test parcing info out of strings. I put my string in the code, upload it to the arduino, then start the serial monitor, which will restart the arduino and display the results in the serial monitor. I print out the results of each parcing action to see if the results match what is expected (correct character counts and such).

#include "WString.h"

void setup()

 #define maxLength 100
 String url = String(maxLength);
 String teststring = String(maxLength);
 String finalstring = String(maxLength);

 int ind1 = 0;
 int ind2 = 0;
 int pos = 0;

  url.append("GET /?-0p1500t1000-1p2200s1000-2p1500-3p500 HTTP/1.1");
  Serial.println("url before replace: ");

  url.replace('-', '#');
  Serial.println("url after replace: ");

  Serial.println("URL length: ");
  pos = url.length();
  ind1 = url.indexOf('#');
  Serial.println("location of first #: ");
  teststring = url.substring(ind1, pos);
  Serial.println("intermediate teststring: "); 
  ind2 = teststring.indexOf(' ');
  Serial.println("location of space: ");

  finalstring = url.substring(ind1, ind2+ind1);

  Serial.println("finalstring: ");

void loop() {}