Sending Text Message (SMS) using ESP8266 and Arduino IDE

This tutorial will help you to send SMS to any registered mobile number from ESP8266. The ESP8266 is a powerful Wifi module which has been dominating the world of IOT projects. This is a Microcontroller independent tutorial meaning; you do not need any UC/UP like Arduino or PIC. We will directly program the ESP8266 module using the Arduino IDE. Once it is programmed we can send text messages from it to any pre-programmed mobile number. I am using the IFTTT Applets to accomplish this task. If you are new to ESP8266 then kindly visit the tutorials below which will help you to get started with ESP8266 and teach you how to program your ESP8266 module using the Arduino IDE.

  1. Getting Started with ESP8266
  2. Programming your ESP8266 with Arduino IDE

Other than this you do not need any pre-requisites for this tutorial for I will be explaining all the steps in details with Pictures and Videos. So let us get started....

 

Concept behind the Tutorial:

We know that the ESP8266 module can be configured both as AP or STA. Here we have configured it to work as station and have connected it to our Wifi Router. Once the connection is establish we have to find out a way to send SMS online. This online must also be easily accessible by out ESP8266 module. This is where we leverage the power of IFTTT (If This Then That) websiteUsing this website we can send SMS, E-mail, Whatsapp messages, Facebook updates, Twitter tweets and what not. They have a heck a lot of tools that can be used with little knowledge, but in this tutorial we are going to use the send message feature and leave the rest for other future tutorials.

 

After few tweaks in the IFTTT website we will able to get a HTTPS URL which when triggered will send a predefined Text message to a specific mobile number. Now, this URL has to be called by our ESP8266 module when needed and your SMS will be fired to your mobile number. Sounds fairly simple right!!? Let us see the steps involved.

 

Creating an IFTTT Applet:

Let us see how we can create an IFTTT Applet and get the URL for triggering the SMS to a specific mobile number. I have tried my best to explain them in steps, if you get stuck make use of the Video at the end of this tutorial.

Step 1: Visit www.IFTTT.com and sign up for new ID if you don’t have one already. After registering you will be sent a mail to your E-mail ID verify it and you will be logged into IFTTTT.

 

Step 2: Search for SMS Applet or visit this link.  Now Register your Mobile number with that Applet here I have used the number “00919612365489” as an example number. Always include the leading “00” followed by your country code and then your mobile number. Here I am from India hence my country code is “91” and my mobile number is “96123456789”. Once the number is entered click on “Send Pin” and verify your mobile number

SMS applet

 

Step 3: Now we have configured one Applet, we will another Applet called Maker Webhooks, so search for it or use this link. Now click on “Connect” and you will get the following Screen.

Configure applet

 

Step 4: Now it is time to create our own Applet that could sync both the above applets. To do this navigate to My Applets -> New Applet, or follow this link. You will taken to this page.

Create new applet

 

Step 5: The term IF THIS THEN THAT means if something happens on the “This” then we have do something on “that”. Here if the Maker Webhooks Applet is Triggered then an SMS must be sent. So click on “this” (the blue colour plus icon) and search for Maker Webhooks then click on it. Now, you will be asked to choose a Trigger, so click on “Receive a web request”

 

Step 6: Now you have to configure the Trigger by giving it an Event Name. I have named it “ESP” as shown below. You can use any event name, but remember this name for we have to use it later. Finally click on “Create Trigger”

Create trigger

 

 Step 7: You should get the below Screen where, you have to configure the “That” Applet.  Click on the Plus icon near “that”

if then that

 

Step 8: Search for SMS Applet and click it. Then for choosing an Action click on “Send me SMS”. You will taken to the below screen, where you have to enter the text message that has to be sent to your mobile. Finally click on “Create action”

Create action for sending sms

 

Step 9: You can review and Finish your Applet, it should be looking something like this below. Click on “Finish”

Review and finish applet

 

Step 10: Now, search for Maker Webhooks or use this link and click on “Documentation”. You should see something like this below

maker webhooks: key to trigger event

 

This is a very important page. This page will show you the key and instructions on how to trigger an event. Your page will display an unique key for your ID, keep it very confidential because anyone with this key can access all your Applets. I have hidden the last five digits of my API key so that it remains confidential.

 

Step 11: Now under “Make a Post or get web request”, you can see that we have an option to add the event name. Remember that in step 6 we created an Event named “ESP” so we have to use the same name and configure our URL like below.

configure URL

 

Once you have changed the Event name click on “Test it”. You should receive a message to your registered mobile number. In our case the message should be “The even name ESP occurred on the Maker Service. SMS sent from ESP8266 –CircuitDigest” You can configure your own SMS as per your wish. You can also send sensor values from ESP module using the value 1 and value 2 option.

 

That is it we now have an HTTPS URL which when triggered will send a particular message to a specified number.  You can copy this URL and keep it somewhere safe because we have to use this URL in our Arduino Program.  You can also simply copy and paste this URL on any web browser and you can notice that the event is triggered. You will also get a reply like this shown below. 

SMS sending event trigger reply message

 

Programming your ESP8266 Module:

Now we have to program our ESP8266 Module using the Arduino IDE. In this program we simply have to connect to the Router and call for the HTTPS URL that we just demonstrated above and then the message will be triggered by that URL. The complete program is given at the end of this tutorial but few important chunks of code which needs modification are explained below.

const char* ssid = "BPAS home";
const char* password = "cracksun";

In the above line of code, use your own Wifi Credentials in my case “BPAS home” was my Wifi signal name and “cracksun” was my password.

 

String url = "/trigger/ESP/with/key/b8h22xlElZvP27lrAXS3ljtBa0092_aAanYN1IXXXXX";

Make sure you have modified the above line based on your Event name and Key, In my case the Event name was ESP (will be the same for you if you have named it ESP in step 6) and my API KEY was b8h22xlElZvP27lrAXS3ljtBa0092_aAanYN1IXXXXX. Change it to the key that you received

 

Note: This step is very important for your code to work.

 

The rest of the lines in the code are self explanatory anyhow if you have any doubts you can use the comment section.

 

Once your code is ready with modifications you are ready to upload it to your ESP8266 module. You can upload program from Arduino IDE as discussed in this tutorial “Programming your ESP8266 with Arduino IDE

 

Output:

Once your upload is done open your serial monitor on Arduino IDE. You should see the following Debugging Information:

Sending SMS program debugging information

 

If your Serial monitor displays “Congratulation! You’ve fired the ESP event”, it means that everything has worked as it suppose to be. Now you can check your mobile and you should have received the message. The screenshot of the message is shown below

Received SMS sent by ESP8266

 

The video below will show you the complete working of the above discussed.

Code: 

/*Program to send SMS from ESP8266 via IFTTT.
 * For complete detials visit: www.circuitdigest.com
 * EXAMPLE URL: (do not use This)
 *  https://maker.ifttt.com/trigger/ESP/with/key/b8h22xlElZvP27lrAXS3ljtBa0092_aAanYN1IXXXXX
 *  
 */

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

const char* ssid = "BPAS home";
const char* password = "cracksen";

const char* host = "maker.ifttt.com";
const int httpsPort = 443;

 

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  WiFiClientSecure client;
  Serial.print("connecting to ");
  Serial.println(host);
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }

 

  String url = "/trigger/ESP/with/key/b8h22xlElZvP27lrAXS3ljtBa0092_aAanYN1IXXXXX";
  Serial.print("requesting URL: ");
  Serial.println(url);

  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: BuildFailureDetectorESP8266\r\n" +
               "Connection: close\r\n\r\n");

  Serial.println("request sent");
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }
  String line = client.readStringUntil('\n');

  Serial.println("reply was:");
  Serial.println("==========");
  Serial.println(line);
  Serial.println("==========");
  Serial.println("closing connection");
}

void loop() {
}

Video: 

Comments (13)

  • Jim's picture
    Jim

    Do you have an example that shows how to send the optional json body as described in the Webhooks setup routine. I would like to send a text on or off depending on the status of a pin on the ESP8266. Thanks, Jim

    Aug 11, 2017
  • Kaushik's picture
    Kaushik

    how to find httpsport number and host???

    Oct 08, 2017
  • B.Aswinth Raj's picture
    B.Aswinth Raj

    The invalid error key is because, you did not follow the step number  10 properly. Make sure the URL you are usig is a correct one. Simply load the URL on your browser and check if the message is received then proceed with the program.

    Oct 09, 2017
  • Kaushik's picture
    Kaushik

    request sent
    headers received
    reply was:
    ==========
    {"errors":[{"message":"You sent an invalid key."}]}
    ==========
    closing connection

    Oct 09, 2017
  • Kaushik's picture
    Kaushik

    Esp event fired successfully but no message came.

    Oct 09, 2017
  • B.Aswinth Raj's picture
    B.Aswinth Raj

    That sounds odd. Did you give your correct number with country code?

     

    Oct 10, 2017
  • Kaushik's picture
    Kaushik

    Message is coming one day late.why????

    Oct 11, 2017
  • Kaushik's picture
    Kaushik

    can u please tell me about using this" httpsPort = 443;" what does that mean??

    Oct 12, 2017
  • B.Aswinth Raj's picture
    B.Aswinth Raj

    That isp port number for accessing IFTTT links. You dont have it change it. leave it as default

    Oct 12, 2017
  • Kaushik's picture
    Kaushik

    When I am putting my phone number and requesting for 4 digit pin, the pin is coming one day late.As well as While Triggering the event the message is coming to my mobile one day late,why????

    Oct 13, 2017
  • B.Aswinth Raj's picture
    B.Aswinth Raj

    This problem is odd. Try using a different network carrier 

    Oct 13, 2017
  • Paul Stroud's picture
    Paul Stroud

    Hello and my compliments on your tutorial! Very thorough. I have completed all of the steps incluging testing the "POST" command and all works fine but I don't seem to be able to connect to my network. It just gets stuck in the "while" loop at line 21:

    while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    }

    and prints dots forever. I'm using a Doit ESP12F Dev kit V3 (Node MCU).
    Any suggestions?

    Nov 10, 2017
  • AISHA's picture
    AISHA

    Try some other example program and check if you ESP is able to function normally

    Nov 14, 2017

Leave a comment