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 ⚠️
Working with electricity involves serious risk. Ensure you have the necessary skills and take proper safety precautions before attempting any electrical projects. Proceed at your own risk — the author assumes no responsibility for any damage, injury, or issues resulting from the use or misuse of the information provided.
All content on this website is original and protected by copyright. Please do not copy or reproduce content without permission. While most of the resources shared here are open-source and freely accessible for your learning and benefit, your respect for our intellectual effort is appreciated.
If you find our tutorials helpful, consider supporting us by purchasing related materials or sharing our work — it helps keep the content flowing.
Need help or have questions? Leave a comment below — the author is always happy to assist!
Table of Contents
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:
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.
Liked this article? Subscribe to our newsletter:
or,
Visit LabProjectsBD.com for more inspiring projects and tutorials.
Thank you!
You may read this: Caller ID detection using Arduino
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
MKDas · 23/12/2021 at 5:02 pm
It is a little fuzzy to understand your requirement. I think you are asking for a voltmeter. You can check this article: https://labprojectsbd.com/category/meters-and-displays/page/2/
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.