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 as 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. 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

JLCPCB – Only $2 for PCB Prototype (Any Color)

24 Hours fast turnaround, Excellent quality & Unbeatable prices

$18 Welcome Bonus for new registrations Now!!! https://jlcpcb.com


You may read this: Caller ID detection using Arduino


Lab Projects BD

This is a personal electronics blog. The author loves to write on different topics and shares his knowledge to help others.

0 Comments

Leave a Reply

Avatar placeholder

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