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:
Handling electricity carries inherent risks. It’s essential to have the appropriate skills to manage it safely. Proceed at your own risk, as the author disclaims responsibility for any misuse, harm, or errors. All content on this website is unique and copyrighted; please avoid unauthorized copying. While most articles are open-source for your benefit, feel free to use the knowledge provided. If you find our resources helpful, consider purchasing available materials to support our work.
For assistance or guidance, leave a comment below; the author is committed to helping. Some articles may contain affiliate links that support the author with a commission at no additional cost to you. Thank you for your understanding and support.
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.
For Professional Designs or Help:
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.