There are many ways you can access different web services with the Arduino and an ESP8266. But perhaps the easiest is using IFTTT. In this tutorial, we are going to make an Arduino send an email when the temperature of thermistor crosses a threshold value. Let’s get started!

If This Then That

IFTTT (If This Then That) is a free web platform that connects devices and services such as Gmail, Twitter, Instagram, Dropbox, Fitbit, Amazon Alexa, and Google Assistant to do specific things they couldn’t do on their own. It weaves these services in a way that Instagram can use the functions of Dropbox seamlessly.

For the most part, IFTTT just does what its name says– if this condition is triggered, then do this action.

To accomplish this, IFTTT uses Applets which are programs that use a trigger to carry out events. An Applet can range from posting regular tweets about the weather to sending an SMS notification whenever someone opens a particular door.

You can never have an IoT web platform as straightforward as IFTTT. From the get-go, it’s pretty apparent with what it does, and this is just one of the reasons I love it. Integrating with ESP8266 is easy as well. We just need to use webhooks.

Webhooks

Webhooks is a service that triggers events via HTTP requests. This feature is especially helpful for DIY makers since it enables simple chips like ESP8266 to integrate with IFTTT services.

A simple webhooks system works like this:

First, we need an HTTP request as an initial trigger. These can be set off by a tactile switch, digital button, sound level, temperature threshold, and the likes. To create a web request to trigger Webhooks, you need to have the key and the event name. The key gives webhooks authentication to your IFTTT account while the event name specifies the action to be triggered when a condition is met. Without one of these two, there is no way to know whose and what action there is to trigger.

Preparing the Hardware

To start our little demonstration, connect the following parts as shown below:

Setting up Email Notifications Using ESP8266 and IFTTT - Schematic

We’ve only been using Arduino UNO as an external programmer in the previous articles, but this time we are going to do some branching out. Aside from using it as a programmer, we will use the UNO to interface with the thermistor. It will be easier this way since thermistors are analog sensors and the ESP-01’s analog pins are not exposed. The Arduino UNO, on the other hand, has 5 analog pins. It is safe to say using them for this project is the best option. Now to set up an IFTTT connection…

Setting Up IFTTT

First, go to the IFTTT website and create an account.

Once logged in, go to the top right toolbar on your homepage and select create. This directs you to a page where you can start creating an Applet.

Then, set a condition by clicking “+ This”.

Next, search for “webhooks”:

Under webhooks, choose to receive a web request and name the event. In this case, we name it ESP_Thermistor. After that, click ‘Create trigger’

Now to set the action, click “+ That”.

How to Send an Email With ESP8266 and IFTTT - Action Setting

Then, since we want to use Email notifications, select “Email” when it says “Choose action service”:

Next, write the message by completing the action fields. There are convenient variables you can use to compose your email: EventName which contains the name of the event; OccurredAt which displays the date and time when the event is triggered, and; Value that can contain any number you want to send to the IFTTT server.

Lastly, check whether you have put the correct details.

By now, the Applet is all fired up and ready to go.

Acquiring the Key

As mentioned before, to trigger the Applet recipe, we need the key along with the event name on our HTTP request. To obtain your unique IFTTT key, go to the homepage and search for Webhooks on the top-left search bar. You can find webhooks under services.

Now on the Webhooks page, click the Documentation link at the top-right corner of the page.

Finally, a page that contains your personal key, and the complete web request for your event appears. This key is unique to every IFTTT account. By including the event name with it, you can trigger an IFTTT action via Webhooks. Now let’s proceed with our code.

Code

We are going to use two sketches for this project. First, a program for the Arduino that reads thermistor values then transmits them to the ESP-01 via serial communication. Then a program for the ESP-01 that receives these values then toggles a GET request to IFTTT when the value is too high. Copy the sketches on your Arduino IDE and save them on different files.

For the Arduino UNO:

#include "SerialTransfer.h"
#include <SoftwareSerial.h>
#define ThermistorPin 0

int Vo;
float R1 = 1000;
float logR2, R2, T;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

SoftwareSerial mySerial(2, 3); // RX, TX
SerialTransfer myTransfer;

void setup() {
  Serial.begin(115200);
  mySerial.begin(9600);
  myTransfer.begin(mySerial);
}

void loop() {

  Vo = analogRead(ThermistorPin);
  R2 = R1*(1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  T = T - 273.15;
  
  //Serial.print("Temperature: "); 
  Serial.println(T);
  //Serial.println(" C"); 
    
    myTransfer.txObj(T, sizeof(T));
    myTransfer.sendData(sizeof(T));
    delay(5000);
}

For the ESP8266-01 module:

#include <ESP8266WiFi.h>
#include "SerialTransfer.h"

SerialTransfer myTransfer;

const char* ssid     = "WiFi Name";
const char* password = "WiFi password";

const char* resource = "https://maker.ifttt.com/trigger/ESP_Thermistor/with/key/bmsvPMsnwDjS4j5p2UsxGr";
const char* server = "maker.ifttt.com";

void setup() {
  Serial.begin(115200); 
  myTransfer.begin(Serial);
  WiFi.begin(ssid, password);            
  Serial.print("Connecting to ");
  Serial.print(ssid); Serial.println(" ...");

  int i = 0;
  while (WiFi.status() != WL_CONNECTED) { 
    delay(1000);
    Serial.print(++i); Serial.print(' ');
  }
  Serial.println('\n');
  Serial.println("Connection established!");  
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP()); 
}

void loop() 
{
  if(myTransfer.available())
  {
    float T;
    myTransfer.rxObj(T, sizeof(T));
  
    if (T>30)
    {
      WiFiClient client;

      client.print(String("GET ") + resource + 
                      " HTTP/1.1\r\n" +
                      "Host: " + server + "\r\n" + 
                      "Connection: close\r\n\r\n");
                  
      int timeout = 5 * 10; // 5 seconds             
      while(!!!client.available() && (timeout-- > 0))
      {
        delay(100);
      }
      while(client.available())
      {
        Serial.write(client.read());
      }
      client.stop();
    }
    else 
    {
      return;
    }
  }  
}

Code Explanation

Unlike other temperature sensors, thermistors are a little complicated to interface with an Arduino. Fortunately, CircuitBasics have done a tutorial explaining this from soup to nuts. Check the article using this link and come back here for the next part.

Now that we’re done with the sensor, the next problem to solve is sending the values to the ESP-01 using serial. Serial communication with Arduino, despite being handy, is very problematic with float values. Serial communication only sends one character at a time plus it requires you to convert everything to strings first. Honestly, I was about to publish this with a significantly longer code before I found the SerialTransfer.h library (download it here). This library is heaven-sent. Why? Take a look at this.

A sketch that sends a float value via serial:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  myTransfer.begin(Serial);
}

void loop()
{
  float myFloat = 25.25;
  myTransfer.txObj(myFloat, sizeof(myFloat));
  myTransfer.sendData(sizeof(myFloat));
  delay(100);
}

A sketch that receives a float value via serial:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  myTransfer.begin(Serial);
}

void loop()
{
  if(myTransfer.available())
  {
    float myFloat;
   
    Serial.print("New Data: ");
    myTransfer.rxObj(myFloat, sizeof(myFloat));
    Serial.println(myFloat);
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}

That’s less than 20 lines to send a float value, and a little more than that to receive it and check for errors! Until now, I’m still amazed. This library cuts up our code a scale hitherto undreamt of.

These are the important lines from the library:

SerialTransfer myTransfer;

which creates a SerialTransfer instance called myTransfer;

 myTransfer.begin(Serial);

which starts serial communication using baud rate Serial; and

  myTransfer.txObj(myFloat, sizeof(myFloat));
  myTransfer.sendData(sizeof(myFloat));

which creates an object specifically made for the myFloat value and sends it in a packet right for its size.

After transmitting the data, we now move on to the code for the ESP-01.

myTransfer.rxObj(myFloat, sizeof(myFloat));

On the other end of the communication line, use this to receive the float value.

const char* ssid     = "WiFi Name";
const char* password = "WiFi password";

const char* resource = "https://maker.ifttt.com/trigger/YOUR EVENT NAME HERE/with/key/YOUR KEY HERE";
const char* server = "maker.ifttt.com";

Next, concatenate strings to contain your WiFi name, password, web request URL, and the IFTTT maker server.

    if (T>30)
    {
      WiFiClient client;

      client.print(String("GET ") + resource + 
                      " HTTP/1.1\r\n" +
                      "Host: " + server + "\r\n" + 
                      "Connection: close\r\n\r\n");
                  
      int timeout = 5 * 10; // 5 seconds             
      while(!!!client.available() && (timeout-- > 0))
      {
        delay(100);
      }
      while(client.available())
      {
        Serial.write(client.read());
      }
      client.stop();
    }
    else 
    {
      return;
    }

This sends an HTTP GET request to the IFTTT server when the value of T exceeds 30. We use GET since we simply need to activate the trigger. We will use POST when we need to write a value to the IFTTT server. For instance, sending an email notification with the current temperature rating from the sensor.

Demonstration

After using a lighter to heat up the thermistor, I received an email from the device. The project has hit the home run!

That’s it for this tutorial. See you at the next one!