In this article, we will write our own code that can be used for ‘Reading SMS with Arduino’. We use Arduinos in different projects. When using GSM modules, we may need to use SMS to give commands to the device. Besides using a big library like FONA, this small code will help you a lot.

Disclaimer:

Advertisements

SMS response to GSM modules:

When there is a SMS, the GSM module gives an output of

“SM” 1

or the slot number.

This way, we can only open the SMS with “AT+CMGR= slot number “. But with the “AT+CNMI=1,2,0,0,0” command, the text directly arrives on the serial terminal. And from that, we can detect the text.

Code:

#include <SoftwareSerial.h>
#define RX 4
#define TX 3
SoftwareSerial gsm = SoftwareSerial(TX, RX);


void setup()
{

  Serial.begin(115200);
  gsm.begin(4800);
  delay(1000);
  gsm.println("AT\r\n"); delay(100);
  gsm.println("AT+CMGF=1\r\n");//goto text mode
  gsm.println("AT+CNMI=1,2,0,0,0\r\n");//enable direct sms notification
}

void loop()
{

  while (gsm.available())
  {
    String line = gsm.readStringUntil('\n');
    if (line.startsWith("*"))
    {
      line = line.substring(line.indexOf("*") + 1, line.indexOf("#"));
      int line_len = line.length() + 1;
      char text[line_len];
      line.toCharArray(text, line_len);
      if (line_len >= 2)
      {
        Serial.print("SMS:");
        Serial.println(text);
      }
    }
  }
}



//

What to do?

Simply send a command in this format: *text#; Here * is the start point and # is the end point. Now let’s see what is the response.

Result:

Arduino SMS library
Arduino SMS library

So what’s the difference? Simply using a start and stop mark, we can send a command through SMS to the GSM project and command execution become easier. Rather than using a big library, this small piece of code helps a lot in GSM SMS projects.

Now, you can integrate this function into your GSM project very easily. I hope this code will help you in your SMS project. Thanks for reading, don’t forget to share it with your friends.

For Professional Designs or Help:

Loading

You may read this: Caller ID detection using Arduino


MKDas

Mithun K. Das; B. Sc. in EEE from KUET; Head of R&D @ M's Lab Engineering Solution. "This is my personal blog. I post articles on different subjects related to electronics in the easiest way so that everything becomes easy for all, especially for beginners. If you have any questions, feel free to ask through the contact us page." Thanks.

4 Comments

ARIF ALEEM SHAMSI · 22/12/2021 at 2:09 pm

Sir, I am happy for your projects, I only view not practical, but I suppose to think they all will
working.

while I actually require AC Voltmeter and AC Current meter.
You solve my one problem of AC Voltmeter.

thanks

Djalltra · 29/12/2021 at 1:07 am

How do I integrate this code in mikroc as Arduino has a lot of shortcuts

    MKDas · 29/12/2021 at 11:06 am

    It is a little tough integrating with mikroC. Because, microC as well as most of the PIC micros can not handle that much UART, Sting handling as arduino can handle.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *