# Using IFTTT and Webhooks with the ESP8266

After covering the Arduino IoT Cloud and Arduino SIM / Sigfox in my previous posts, I want to stick to the topic of IoT (Internet of things) with this article about IFTTT (if this then that) and webhooks. But this time there is no need to invest in high-priced boards, everything is explained using the inexpensive ESP8266 ESP-01 as an example.

Tracking readings online or switching devices is a fine thing. But wouldn’t it be nice to be informed by email, text message, notification or even by a phone call that a temperature is out of control, the water level is too high or the alarm system has gone off? No problem with IFTTT, which by the way is pronounced like “ift” in “gift”. But I also show you how to view the measured values online in your browser or how to switch devices.

As usual, I explain all this step by step. However, I do not go into the basics of the ESP8266 ESP-01. If needed, you can find details about this topic in my post on it.

The advanced users can skip one or the other step in this post. It is structured as follows:

### What is IFTTT?

IFTTT is a very flexible Internet service that allows you to link various other services in the sense of “if this, then that”. IFTTT was developed by Linden Tibbets and went into operation in 2010.

The principle is simple: you define a trigger in a service and then specify which action is triggered by which other service. Trigger and action together form the so-called applet. There is an almost unmanageable number of services that can be used as triggers or for action.

It becomes clearer when we go through a concrete, simple example that has nothing to do with Arduino, ESP and Co. yet.

## An IFTTT Introduction

That’s it – you have your IFTTT account. Skip the next window (“Get Started”).

IFTTT is actually incredibly simple, but from my point of view not overall intuitive. A helpful rule is: if you want to do anything on IFTTT, then a click on your picture or the gray symbol at the top right is a good starting point – try it and a menu will appear:

Go to “Account”. There you can see some settings, e.g. to which other accounts your IFTTT account is connected. For the following example, you must be connected to your Google account.

### A simple IFTTT example

As a trigger application, we take the Google Assistant on your smartphone. If you haven’t installed it, you’ll find it on Google Play. You could also take Siri, Cortana or Alexa. With Cortana, I tried it successfully, Siri and Alexa I don’t use. The trigger itself is the voice instruction “sende mir eine E-mail” (= e-mail me). Sorry – I did not find the time to repeat everything in English, so you will see some German in the pictures. As an action, IFTTT should send an e-mail.

IFTTT is also available as a smartphone app. So, you might as well do the following steps on your smartphone.

Let’s start: navigate to IFTTT, click on your profile picture, then click on “Create” and finally on “+ This”:

You will then be prompted to “Choose a service”. Enter “Google” and select “Google Assistant”.

Select “Say a simple phrase”:

Before you type the phrase in the next window, you should test speak it to Google Assistant and adjust the spelling if necessary. In this example, “email me”, i.e. without the hyphen, would not work.

Insert the phrase and maybe one or two more variations. You can also have the Google Assistant respond. I chose “na gut” (= okay).

Select “Create Trigger”. Then you click on “+That” in the next   window:

Now we come to the action part. For this, you type “email” in the next window (Choose action   service) and select “Email”. In another window, click “Send me an email”. Then you can give free rein to your creativity in terms of theme and content:

Click “Create Action” and in the next screen click “Finish”.

That’s it:

Google Assistant responds with the previously defined response:

After a few seconds, you should receive an IFTTT email that looks similar to this one:

If you want to modify your applet later, then go to your profile picture at the top right and select “My Applets”. Select the applet in question. If you want to change the key phrase then select Settings at the top right. There you can also delete the applet. If you want to change the trigger or action, go to the respective symbol. In case you want to disable the applet, click “Connected”.

It’s also worth trying out some of the different examples on IFTTT. To do this, go to “Explore”. However, to take a closer look at the examples, you first need to connect them. Without that, you can’t access the settings.

## Preparations

### Wiring

For the further course we need something to switch and something to read out. I chose an LED and a DHT22 temperature and humidity sensor. Of course, you can take anything else. My wiring looks like this:

• With the DHT22, don’t forget the pull-up resistor.
• As you can see, I connected the ESP8266 ESP-01 module via a USB-to-serial adapter. Alternatively, you can take an Arduino. How this works in detail is described here.
• The power supply via the adapter was not sufficient. I needed an external one. For breadboards, I recommend power supply adapters

### Test sketch

The following sketch is only for checking the circuit. If you have any problems later, you can exclude this factor.

#include "DHT.h"

#define LED_PIN 0
#define DHTPIN 2
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup(){
Serial.begin(9600);
delay(1500);
dht.begin();
pinMode(LED_PIN, OUTPUT);
}

void loop() {
static unsigned long lastMeasurement = millis();
float myTemp = 0.0;
if((millis() - lastMeasurement) > 3000){
Serial.print("Temperatur: ");
Serial.print(myTemp);
Serial.println(" C");
lastMeasurement = millis();
toggleLED();
}
}

void toggleLED(){
static bool ledStatus = true;
digitalWrite(LED_PIN, ledStatus);
Serial.print("The light is ");
if(ledStatus){
Serial.println("ON");
}
else{
Serial.println("OFF");
}
ledStatus = !ledStatus;
}

## Create a trigger via a sketch

Now we get to an interesting point: how can one create a trigger with the ESP8266? This is where the webhooks come in. Hooks are interfaces in programs that allow access for other programs. And with webhooks, this just happens via the web.

### The webhook on the IFTTT page

We create a new applet. Go back to your picture in IFTTT — > Create — > +This — > and then select “Webhooks” as your service. IFTTT suggests “Receive a web request” — > click on it. Assign a name for the event. I called it ESP8266_Event, but I could have taken “potato salad”. It does not matter, is what I am saying.

Click on “Create trigger” and then turn to “That”. As “Action service” select “Notifications” and click on the icon. You can choose from two actions:

We take the simple, left variant. In the next window, click on “Add ingredient”:

You can customize the content and add variables that are later passed through the trigger service. To do this, go to the text where you want to insert the variable, and then click the variable in the list. “Event Name” was already defined in IFTTT (ESP8266_Event). Here’s what it might look like (sorry again for the German lessons):

The quotation mark after “Event_Name” is an oversight – I removed it later.

Then click “Create action” and “Finish” in the next window. If you want to change the text later, that’s not a problem. Go to the profile picture – My Applets – Select applet – Settings.

Now you click the webhook icon ….

… and “Documentation” in the next window at the top right. You see a window like this (I made my key unrecognizable):

You need the key and event name right away for the Arduino sketch.

### On the ESP8266 side

The question now is what exactly you need to send from the ESP8266 to IFTTT to act as a trigger and how you send it. To the “what”: in the simplest case, it is a web request with the following content:

If you want to send variables, you do so in JSON format. But you probably don’t want to deal with these details. So it’s nice that there is a solution to this, here on GitHub. Go to “Code” and download the ZIP file. All you need is the file AnotherIFTTTWebhook.h. You save it somewhere from where you can include it, e.g. in the folder of your (yet to be created) sketch. Side note: if you like the solution, give the author a star on GitHub. I know from myself that this is motivating!

AnotherIFTTTWebhook.h provides you with the function send_webhook() that “assembles” the request to IFFFT in JSON format. What you have to pass to the function will come in a moment. In doing so, we have clarified the ‘how’.

#### The base sketch

You still need to customize the following sketch by entering the name of your Wi-Fi, the Wi-Fi password and your IFTTT key and modifying the event name if necessary.

In send_webhook(), you can pass three strings, which are then interpreted by IFTTT as “Value1”, “Value2” and “Value3”. In this example, I didn’t use Value3, but I still need to pass a dummy.

// Wolfgang Ewald: this sketch has been adapted from Simon Ogden's Example Sketch on Github:
//
// https://github.com/Siytek/AnotherIFTTTWebhook/blob/master/AnotherIFTTTWebhook_example.ino
//
// ****************************************************************************************
//
//
// Copyright (c) 2020 Simon Ogden
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// ====================================================================
// ====================================================================
// Sample application using an adapted version of IFTTT Trigger
//
// How to use:
// 1. Place AnotherIFTTTWebhook.h in the same directory as your project
// 3. Send webhook using function in main file:
//    send_webhook(EVENT, KEY, Value1, Value2, Value3);
//
// Visit me @ https://www.siytek.com
// ====================================================================
// ====================================================================

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

// Set WiFi credentials
#define WIFI_SSID "Name of your Wifi"

// Set IFTTT Webhooks event name and key
#define IFTTT_Event "ESP8266_Event" // or whatever name you have chosen

void setup() {
Serial.begin(9600); // Serial output only for information, you can also remove all Serial commands

// Connecting to WiFi...
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}

// Connected to WiFi
Serial.println();
Serial.println(WiFi.localIP());

// Send Webook to IFTTT
send_webhook(IFTTT_Event,IFTTT_Key,"Moin, hier ist dein ESP8266!","25","value 3");
}

void loop() {
// we have no repeated action in this example
}

Now, if you run the sketch, you should receive a notification on your phone within a few seconds:

If your phone is locked, it depends on your settings whether the notification appears on the lock screen or not. But at the latest when you unlock it, it should be visible.

### The sketch with “real” temperatures

Now we extend the sketch with the temperature measurement. The temperature is measured every five seconds. I have defined that I will only be informed if the temperature falls below 20 degrees. I created this condition with cold spray. Of course, you can also take an upper limit, which you exceed e.g. by blowing on the DHT22 or touching it.

Then I defined that I would only be notified once a minute so that I would not be bombarded with notifications when the temperature drops below 20 degrees. If all conditions are met, the temperature is converted from float to a char array and then passed to send_webhook().

The one-off call send_webhook() in setup() is only for checking whether the webhook is working.

Of course, you can also output the temperature readings on the serial monitor. I deliberately didn’t integrate this into the sketch to make it clearer.

#include <ESP8266WiFi.h>
#include <DHT.h>
#include "AnotherIFTTTWebhook.h"

#define DHTPIN 2
#define DHTTYPE DHT22

// Set WiFi credentials
#define WIFI_SSID "name of your Wifi"

// Set IFTTT Webhooks event name and key
#define IFTTT_Event "ESP8266_Event"

String tString = "999"; // temperature as String

DHT dht(DHTPIN, DHTTYPE);

void setup() {

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

dht.begin();
delay(2000); // better to give some time

send_webhook(IFTTT_Event,IFTTT_Key,"Test","Test","dummy"); // just a Test
}

void loop() {
static unsigned long lastMeasure = 0;
if((millis()-lastMeasure) > 5000){
updateTemp();
lastMeasure = millis();
}
}

void updateTemp() {
static unsigned long lastIFTTT = 0;
char tChar[7]; // temperature as char array
tString = String(myTemp); // convert temperature as float into String
if((myTemp < 20.0) && ((millis()-lastIFTTT)) > 60000){
tString.toCharArray(tChar, tString.length()); // convert temp. as String into char array
send_webhook(IFTTT_Event,IFTTT_Key,"Moin! ",tChar,"dummy");
lastIFTTT = millis();
}
}

And here’s the result:

## Output and control with the browser

Now we briefly leave the IFTTT functionalities and output the temperature readings in the browser. In addition, we switch the LED in this way. Later, the IFTTT functionality is added again. I don’t want to overwhelm less experienced readers, so I’m doing it step by step. It also makes troubleshooting easier if something doesn’t work.

In my article about the ESP8266 ESP-01 I had already explained how to control or read an LED or a temperature sensor via browser. I repeat this here, but I will not go into details. If you don’t understand the following things, please go back to the post (or just take it as given).

Before you upload the following sketch, you need to make adjustments again. In addition, you need to adjust the IP address (line 13). We need the IP address because this is a server application. You must assign a free IP address from your home network. You can check this in the router. It is also best to make sure that your ESP8266 ESP-01 always gets the same address so that another device doesn’t possibly jump the queue.

#include "ESP8266WiFi.h"
#include "ESP8266WebServer.h"
#include "DHT.h"

#define LED_PIN 0
#define DHTPIN 2
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

const char* ssid = "your Wifi name";
ESP8266WebServer server(80);

String led1= "<BR><BR><a href=\"/led_an\">LED An</a>";
String led0= "<BR><BR><a href=\"/led_aus\">LED Aus</a>";
String message1
"<h1>Temperaturprogramm ESP8266</h1><BR><BR>"
"Die Temperatur betr&aumlgt: ";
String message = "";
String tString = "999";
bool ledStatus = false;

void ledan(){
ledStatus = true;
digitalWrite(LED_PIN, HIGH);
message = message1 + tString;
message += led0;
server.send(200, "text/html", message);
}

void ledaus(){
ledStatus = false;
digitalWrite(LED_PIN, LOW);
message = message1 + tString;
message += led1;
server.send(200, "text/html", message);
}

void handleRoot(){
ledaus();
}

void setup() {
WiFi.begin(ssid, pass);
WiFi.config(ip, gateway, subnet);

server.on("/",handleRoot);
server.on("/led_an", ledan);
server.on("/led_aus", ledaus);
server.begin();

dht.begin();
pinMode(LED_PIN, OUTPUT);
}

void loop() {
static unsigned long lastMeasure = 0;
server.handleClient();
if((millis()-lastMeasure) > 5000){
updateTemp();
lastMeasure = millis();
}
}

void updateTemp(){
tString = String(myTemp);
message = message1 + tString;
if(ledStatus){
message += led1;
}
else{
message += led0;
}
server.send(200, "text/html", message);
}

Upload the sketch, launch it, and start your favorite browser. There you enter the above defined IP address in the address line. You can now see the temperature, and you can switch the LED. Admittedly, the appearance is quite spartan. If you’re fit in HTML, you can still make it up.

Maybe you want to access the project from outside your home network? The safest method is to access your home network via VPN and call the internal IP address from there. If you have a FritzBox, that’s not a problem at all. However, it should also work with many other routers.

## All combined: browser control and IFTTT

In the final step, we combine the browser control with the IFTTT functionality. If you have understood the other sketches, then you should have no problem with this one. But I think if I had started with this sketch, one or the other might not have been able to follow.

#include "ESP8266WiFi.h"
#include "ESP8266WebServer.h"
#include "DHT.h"
#include "AnotherIFTTTWebhook.h"

#define LED_PIN 0
#define DHTPIN 2
#define DHTTYPE DHT22

// Set WiFi credentials

// Set IFTTT Webhooks event name and key
#define IFTTT_Event "ESP8266_Event"

DHT dht(DHTPIN, DHTTYPE);

ESP8266WebServer server(80);

String led1= "<BR><BR><a href=\"/led_an\">LED An</a>";
String led0= "<BR><BR><a href=\"/led_aus\">LED Aus</a>";
"<h1>Temperatur-LED Programm ESP8266</h1><BR><BR>"
"Die Temperatur betr&aumlgt: ";
String message = "";
String tString = "999";
bool ledStatus = false;

void ledan(){
ledStatus = true;
digitalWrite(LED_PIN, HIGH);
message = message1 + tString;
message += led0;
server.send(200, "text/html", message);
}

void ledaus(){
ledStatus = false;
digitalWrite(LED_PIN, LOW);
message = message1 + tString;
message += led1;
server.send(200, "text/html", message);
}

void handleRoot(){
ledaus();
}

void setup() {

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

WiFi.begin(WIFI_SSID, WIFI_PASS);
WiFi.config(ip, gateway, subnet);

server.on("/",handleRoot);
server.on("/led_an", ledan);
server.on("/led_aus", ledaus);
server.begin();

dht.begin();
pinMode(LED_PIN, OUTPUT);
delay(10000); // some waiting time

send_webhook(IFTTT_Event,IFTTT_Key,"Test","Test","dummy");
}

void loop() {
static unsigned long lastMeasure = 0;
server.handleClient();
if((millis()-lastMeasure) > 5000){
updateTemp();
lastMeasure = millis();
}
}

void updateTemp() {
static unsigned long lastIFTTT = 0;
char tChar[7];
tString = String(myTemp);
if((myTemp < 20.0) && ((millis()-lastIFTTT)) > 30000){
tString.toCharArray(tChar, tString.length());
send_webhook(IFTTT_Event,IFTTT_Key,"Moin! ",tChar,"dummy");
lastIFTTT = millis();
}
message = message1 + tString;
if(ledStatus){
message += led1;
}
else{
message += led0;
}

server.send(200, "text/html", message);
}

## Webhooks with the Arduino IoT Cloud

### Arduino IoT Cloud and IFTTT

Then I tried to use the webhooks from Arduino IoT Cloud with IFTTT. In principle, it’s simple. You take the webhook from IFTTT, replace “{Event_Name}” by the variable of your property (here: “myTemp”) and apply it in the “Webhooks” tab of your “Thing”.

If this is all Greek to you, read my article about the Arduino IoT Cloud.

The problem is that with every call of ArduinoCloud.update() an IFTTT action is triggered. If, on the other hand, you only call the function in case a certain condition occurs, you will not be able to track the readings. At least not as long as the measured values are within the limit values. I couldn’t find a way to call the webhook manually in the sketch – that would be the solution. I don’t say that it can’t be done somehow, but I’ve been trying for quite some time unsuccessfully.

### Other Webhook Applications in the Arduino IoT Cloud

But to rescue the reputation of the Arduino IoT Cloud, I have to say that there are still some cool other webhook applications.

If you’re interested in this, take a look at these examples:

## Acknowledgement

I thank Siytek for his AnotherIFTTTWebhook.h adaptation on Github.