For our final IFTTT article, we are going to make the Arduino UNO and an ESP8266 module tweet the temperature and humidity readings of a DHT sensor every hour.

IFTTT

Let’s have a review.

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. It weaves these services in a way that Instagram can use the functions of another service like Dropbox, without a hitch.

IFTTT works just like a standard if statement– if this condition is triggered, then do this action.

Furthermore, IFTTT uses Applets. These are tiny programs that use a trigger to carry out events. An Applet can do several things, such as posting tweets at regular intervals like our project today.

To integrate IFTTT with ESP8266, we need webhooks. Webhooks is a service that triggers events via HTTP requests.

A simple webhooks system works like below:

Figure 1: Webhooks system

The initial trigger of a webhooks system is an HTTP request. 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 a key and 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

For this project, we are going to interface a DHT sensor with Arduino UNO, then transfer the temperature and humidity values to the ESP8266 every hour. The ESP8266 module is set to send a POST request when it receives the data. Ultimately, the POST request triggers the applet, which then writes a tweet to the account you’ve registered in IFTTT.

In order to do that, we need the following components:

Figure 2: Connections

Setting Up IFTTT

First, go to IFTTT’s website and create an account.

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

How to Send an Email With ESP8266 and IFTTT - Creating Account
Figure 3: Creating an Applet

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

How to Send an Email With ESP8266 and IFTTT - Creating Account Step 2
Figure 4: This condition

Next, search for Webhooks.

Figure 5: Webhooks

Under webhooks, choose to receive a web request. Click create trigger after naming your event.

Figure 6: Creating the Trigger

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

How to Send an Email With ESP8266 and IFTTT - Action Setting
Figure 7: That condition

Next, search for Twitter as action service.

Figure 8: Choosing Twitter

If you don’t have a twitter account paired with IFTTT yet, a prompt will appear. Click connect to register your twitter. You need to log in your account after this.

Figure 9: Connecting Twitter

Next, authorize IFTTT to access your twitter account.

Figure 10: Authorizing IFTTT

After your account has been registered, return to IFTTT. Select “Post a Tweet” as the action.

Figure 11: Choosing action

Lastly, compose the tweet. There are convenient variables you can use to compose your message: 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.

Figure 12: Constructing tweet

Check whether you have put the correct details.

Figure 13: Review and Finish

The applet is now ready. Now, to enable your android device for the SMS notifications, search IFTTT in Google Playstore and install.

After installing, log in with your IFTTT credentials to pair with your account.

Acquiring the Key

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 the webhooks under services.

Figure 14: Checking documentation

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. Finally, let’s proceed to our code.

Figure 15: Acquiring the Key

Code for the Arduino

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

#define DHTPIN 5
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float t,h;

SerialTransfer myTransfer;

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

void loop()
{
  t = dht.readTemperature();
  h = dht.readHumidity();  
  Serial.println(t);
  Serial.println(h);
  myTransfer.txObj(status, sizeof(t));
  myTransfer.sendData(sizeof(h));
  delay(3600000);
}

Code for the ESP8266 Module

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

SerialTransfer myTransfer;

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";

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

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

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

  myTransfer.begin(Serial);
}

void loop() 
{
  if(myTransfer.available())
  {
    float t,h;
    myTransfer.rxObj(myFloat, sizeof(myFloat));
    HTTPClient http;
    http.begin(serverName);
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    String httpRequestData = "value1=" + String(t) + "&value2=" + String(h);           
    int httpResponseCode = http.POST(httpRequestData);
    http.end();
  }
  delay(10000);
}  

Code for NodeMCU

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <DHT.h>

#define DHTPIN D5
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
int t,h;

const char* ssid = "Centropolis409";
const char* password = "ChitoMikeArnelRuz409!";

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

unsigned long lastTime = 0;
unsigned long timerDelay = 10000;

void setup() 
{
  Serial.begin(115200);
  dht.begin();
  delay(100);
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("Timer set to 1 hour (timerDelay variable), it will take 1 hour before publishing the first reading.");
}

void loop() 
{
  if ((millis() - lastTime) > timerDelay) 
  {
    if(WiFi.status()== WL_CONNECTED)
    {
      HTTPClient http;
      http.begin(serverName);
      t = dht.readTemperature();
      h = dht.readHumidity();  
      Serial.println(t);
      Serial.println(h);
      http.addHeader("Content-Type", "application/x-www-form-urlencoded");
      String httpRequestData = "value1=" + String(t) + "&value2=" + String(h);           
      int httpResponseCode = http.POST(httpRequestData);
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
      http.end();
    }
    else 
    {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

Code Explanation

Just like before, we use the SerialTransfer.h library to send the sensor data from the Arduino UNO to the ESP module. You can download it from here.

These are the important lines we’ve used from the library:

  • SerialTransfer myTransfer – creates a SerialTransfer instance called myTransfer.
  • myTransfer.begin(Serial) – starts serial communication using baud rate Serial.
  • myTransfer.txObj(status, sizeof(status)) – creates an object specifically made for status.
  • myTransfer.sendData(sizeof(status)) – sends status in a packet right for its size.
  • myTransfer.rxObj(status, sizeof(status)) – receives status from the sender.

First, we write the program for the Arduino UNO. The UNO’s main job is to read DHT sensor data then send it to the ESP module every hour. We do that by using the DHT.h library, which you can get from this link. To properly use the library, you need to specify the pin where the sensor is connected and the type of DHT sensor you’re using. Then, we create a DHT object using these two. The code should look like this: DHT dht(DHTPIN, DHTTYPE). After creating the object, we initialize the sensor using dht.begin() to use functions like dht.readTemperature() and dht.readHumidity to get the sensor data. Finally, we use the SerialTransfer.h to send the data to the ESP module.

On the other hand, the program for the ESP module simply detects the incoming data from the Arduino and sends them to IFTTT using HTTP POST. First, to connect to the internet, we concatenate strings for your WiFi name, password, web request URL, and the IFTTT maker server using const char*. Next, using WiFi.mode(WIFI_STA), we declare the ESP8266 module as a station to prevent it from getting defaulted into other modes. With WiFi.begin and myTransfer.begin, we initialize WiFi connection and serial communication respectively. Unlike our previous article on sending text messages using Arduino, we now use ESP8266HTTPClient.h and WiFiClient.h libraries to ease our sending of the HTTP request. So now we only need to call the http.POST(httpRequestData) function to send the request. No more cryptic symbols needed.

I’ve also included the original code where I based my programs. It’s for a NodeMCU development kit. You can read the full tutorial here. The good thing about using the NodeMCU is that you don’t need to use serial communication to send the sensor data. This speeds things up a bit as serial connection is slow. It also makes your project easier to debug since it uses lesser components.

Demonstration

After uploading the code, the Arduino sends the POST request right away.

Figure 16: First Tweet

Then, after an hour, I got another tweet.

Figure 17: Second Tweet