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: Electricity is always dangerous. Proper skill is required to work with electricity. Do work at your own risk. The author will not be responsible for any misuse or harmful act or any mistake you make. The contents of this website are unique and copyright protected. Kindly don’t do any nonsensical act copying and claiming it as yours. Most of the articles published here are kept open-source to help you. Take the knowledge for free and use it, but if you are interested you can buy the ready resources offered here. If you need any help or guide feel free to comment below, the author will try to help you. Also, there can be affiliation links in the article. Which will not affect you anyway, but helps the author with some commission. So please don’t take it otherwise. Thanks.

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.

Loading

Also let me know what you want to get as the next article, comment below!

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.