SIM800 – Controlling Arduino with SMS (IoT Backup Route)

This post further discusses on extracting commands sent to an Arduino in form of an SMS, using SIM800 module. Discussed setup can be used as a backup route to an IoT device, enabling access in case of a failure in the primary network connectivity.

A backup route to be used in case your internet connections went down or maybe “the thief” took down the router first, leaving no way for you to connect with your master mind IoT security system. (wink)

This is the continuation of a previous post titled “Quickstart SIM800 (SIM800L) with Arduino“, which is recommended for anyone new to SIM800.

Reading Incoming SMS

Looking an AT command manual for SIM800 we found in the previous post it is clear that “ AT+CMGL “REC UNREAD” 0 ” can be used to list all the unread SMS messages. Using the index obtained by “CMGL”, it is possible to use “AT+CMGR <index> 0” to read contents of each unread message. But with this approach, it is necessary to pull messages stored in SIM periodically. This can be very inefficient in case of a system that does not get commands very frequently. If Arduino is running off of a battery above check will drain the battery pretty fast. Moreover, commands sent will not apply immediately.

Due to above limitations and inefficiencies, it is best to use “AT+CNMI” command (“New SMS Message Indications”) to get notified of any new incoming SMS message. Trying “AT+CNMI 1,2,0,0,0” on Arduino IDE “Serial Monitor” results in below, upon SMS arrival :



Enhanced Sample

With below code it is possible to process incoming “New SMS Message Indications” in form of “+CMT”, check the next line for the actual message content and do a certain action based on the command received. For simplicity, below code will only turn ON and OFF the LED built into Arduino (digital pin 13).

It is obvious that same logic can be applied even to operate an A/C powered device (using a relay).

Below sample is available for download at :

#include <gprs.h>
#include <softwareserial.h>

#define TIMEOUT    5000
#define LED_PIN    13

bool ledStatus;
GPRS gprs;

void setup() {

  Serial.println("Starting SIM800 SMS Command Processor");

  while(0 != gprs.init()) {
      Serial.print("init error\r\n");

  //Set SMS mode to ASCII
  if(0 != gprs.sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", TIMEOUT)) {
  //Start listening to New SMS Message Indications 
  if(0 != gprs.sendCmdAndWaitForResp("AT+CNMI=1,2,0,0,0\r\n", "OK", TIMEOUT)) {

  Serial.println("Init success");

//Variable to hold last line of serial output from SIM800
char currentLine[500] = "";
int currentLineIndex = 0;

//Boolean to be set to true if message notificaion was found and next
//line of serial output is the actual SMS message content
bool nextLineIsMessage = false;

void loop() {
  //Write current status to LED pin
  digitalWrite(LED_PIN, ledStatus);
  //If there is serial output from SIM800
    char lastCharRead =;
    //Read each character from serial output until \r or \n is reached (which denotes end of line)
    if(lastCharRead == '\r' || lastCharRead == '\n'){
        String lastLine = String(currentLine);
        //If last line read +CMT, New SMS Message Indications was received. 
        //Hence, next line is the message content.
          nextLineIsMessage = true;
        } else if (lastLine.length() > 0) {
          if(nextLineIsMessage) {
            //Read message content and set status according to SMS content
            if(lastLine.indexOf("LED ON") >= 0){
              ledStatus = 1;
            } else if(lastLine.indexOf("LED OFF") >= 0) {
              ledStatus = 0;
            nextLineIsMessage = false;
        //Clear char array for next line of read
        for( int i = 0; i < sizeof(currentLine);  ++i ) {
         currentLine[i] = (char)0;
        currentLineIndex = 0;
    } else {
      currentLine[currentLineIndex++] = lastCharRead;

Turning LED ON :



Turning LED OFF :



32 comments on “SIM800 – Controlling Arduino with SMS (IoT Backup Route)

  1. RAF March 24, 2016 9:48 PM

    hi, I’m having a problem with $gt; and &lt, it says “‘gt’ was not declared in this scope” when I try to upload the code, do you know what could be the problem?

    • Ayoma April 19, 2016 2:22 PM

      Hi RAF,

      If I understood your problem properly, your browser might have copied the code with the HTML encoding. Can you replace all “&gt” with “>” signs and “&lt” with “<" signs. Maybe there are other encoded items as well. Let me know if you find other errors. If so, I will provide a text file with code for you to download. Regards, Ayoma

  2. Stefan April 22, 2016 7:10 PM

    After replace all “&gt” with “>” signs and “&lt” with “<" signs
    is it exit for verify the sketch from row 83 to row 88

    exit status 1 row83
    expected ')' before ';' token
    84: error: 'i' was not declared in this scope
    90: error: expected declaration before '}' token
    exit status 1
    expected unqualified-id before 'else'

    can you please load up the working code ,

    Thank you anyway for your simm800 post's

    • Ayoma April 29, 2016 2:28 PM

      Looks like I’ll have to change the syntax highlighter soon 🙂 I have corrected the post and added additional download link for the code. Please note that this is continuation of “Quickstart SIM800 (SIM800L) with Arduino” (linked on top of the post). You will need Seeeduino_GPRS library ( to get this working. Read through the first post for relevant details.

  3. Ankur Chaudhari July 20, 2016 2:14 PM

    SIM800 moduel showing power check failed! error after “Starting SIM….”

  4. amir August 5, 2016 8:47 PM

    Hi rx and tx pins are connected to what I base ?

  5. Luis August 10, 2016 7:51 AM

    where can i get the gprs.h???

  6. Gurcharan Singh October 12, 2016 10:21 PM

    hello sir,

    Thank your sir for the code, the code provided by you is working …sir i have a question assume i have two numbers (number1 & number 2) if i send command from number1 the led should on..but if i send LED ON command from number2 the led should not on …then how could we edit the code to worked like that?
    please reply soon..

  7. Amal October 21, 2016 12:20 AM

    thanks for your codes.
    to delete the all sms i used below codes
    //if(0 != gprs.sendCmdAndWaitForResp(“AT+CMGD=1,4\r\n”, “OK”, TIMEOUT))
    //{Serial.println(“SMS was deleted”);}
    // {Serial.println(“SMS was not deleted”);}

    but it was not work out.
    what is the problem behind that.

  8. Amal October 21, 2016 1:02 AM

    Oh finally AT+CMGDA=1 was work out with below codes
    if(0 != gprs.sendCmdAndWaitForResp(“AT+CMGDA=1\r\n”, “OK”, 5))
    {Serial.println(“SMS was deleted”);}
    {Serial.println(“SMS was not deleted”);}
    it takes some times to delete sms, so then set timeout=5 //timerEnd – timerStart > 1000 * timeout

  9. Salam November 12, 2016 8:22 PM

    Hi. Ayoma
    I used your code on Arduino ouno and all things is right just when I send on or off led, no action will happen and led not turns on.
    Pleas help me.

    • Roshan Chamal November 14, 2016 12:13 AM

      add pin configuration line as describe below:

      GPRS gprs;

      void setup() {
      //setup pin 13
      pinMode(13, OUTPUT);

      • Roshan Chamal November 14, 2016 12:14 AM

        need to change this section:

        //Read message content and set status according to SMS content
        if (lastLine.indexOf( “LED ON”) >= 0) {
        //ledStatus = 1;
        digitalWrite(LED_PIN, HIGH);
        } else if (lastLine.indexOf( “LED OFF”) >= 0) {
        //ledStatus = 0;
        digitalWrite(LED_PIN, LOW);

        • NADIR November 17, 2017 5:45 PM

          i want to controlle more leds so your change can help me or not ?

    • veerendra March 8, 2018 9:31 PM

      HI .i am also facing same problem,i am using arduino uno r3.i changed the code as per below but ,i am not getting that.
      it’s showing Starting SIM800 SMS Command Processor
      Init success
      when I send led on or led off , no action will happen and led not turns on.
      please help me.

  10. alrythmond January 7, 2017 10:35 PM

    Hi, Im having trouble, it says gprs was not declared in this scope thank you

  11. Aalekh February 16, 2017 12:05 AM

    To wich pin of arduino shud i connect TX and RX pins of SIM800 module????Plz let us knw ur connection also..It wil be of great help

    • Aloysius Teo February 23, 2017 9:02 PM

      tx (9), rx (10)
      try this if ur using uno

  12. Simeon February 18, 2017 1:01 AM

    I have unreadable message:
    +CMT: “+359xxxxxxxxxxxx”,””,”17/02/17,21:28:03+08″
    Content: ….

    My message is Jjjjjj….
    This is ASCII CIPHERTEX … but how can i convert it from ASCII CIPHERTEX to READABLE String … for example UTF-8

    • Phuc July 10, 2017 11:27 AM

      type AT+CMGF=1
      which means show in text mode
      you’re in PUD mode which is HEX code

  13. Aloysius Teo February 23, 2017 9:01 PM

    hi, does this code work in arduino mega? i tried it with uno its work. but i have problem working it with mega.

  14. salam March 6, 2017 12:46 PM

    is it possible change the rx and tx pins???
    pleas help me its so necessary for me

  15. SAMPATH P K March 23, 2017 8:47 AM

    hi thanks for your code ,
    led was not on i tri
    led is on

  16. TELBeast April 17, 2017 10:55 PM

    Is there a way to only allow the program to run if a particular specified phone number is sending the sms?

  17. Willson July 17, 2017 3:16 PM

    Do not understand why (AT+CMGR=1 ) can drain the battery and (AT+CNMI) not.

    In both cases, the SIM memory is readed and the power consumption is similar. But if AT+CNMI is used, the stored system configuracion has changing.

    • Willson July 19, 2017 5:49 PM

      Ok after re-read the program now i understand, the script is listening the GSM port for intakes…

  18. jmmwah October 5, 2017 3:13 PM

    hey sir I need your help. How to fix this kind of error? Thank you for your answer..

    C:\Users\jmmwah\Documents\Arduino\GSM_GPRSLibrary_SMSas\GSM_GPRSLibrary_SMSas.ino: In function ‘void setup()’:

    C:\Users\jmmwah\Documents\Arduino\GSM_GPRSLibrary_SMSas\GSM_GPRSLibrary_SMSas.ino:41:44: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

    if (sms.SendSMS(“095048”, “Arduino SMS”))


    C:\Users\jmmwah\Documents\Arduino\GSM_GPRSLibrary_SMSas\GSM_GPRSLibrary_SMSas.ino:41:44: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

    • ANDREAS PAWELSKI December 28, 2017 4:55 AM

      me succedo lo stessa cosa ho provato prima con arduino uno e poi con arduino mega non vorrei che sia una impostazione del programma.
      per caso hai trovato una soluzione???

  19. Arrvin January 22, 2018 5:03 PM

    where i can get gprs library file? can you send me the link please?

  20. Laurent Kempeneers December 13, 2019 1:53 AM

    Link to “Quickstart SIM800 (SIM800L) with Arduino” seems to reference an empty page…

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">