Introduction: ESP8266 to IFTTT Using Arduino IDE

Hey People,

Although there are plenty of Instructables out there on using the ESP8266 module, I couldn't find one specific to loading data to the free IFTTT (If This Then That) platform so I thought i'd share my project in case it helps anyone out there.

The project basically demonstrates sending a message to my phone if a change is detected in a couple of door sensors. At present the project is only in the testing phase but it should give you an understanding on connecting it up ready for installation. The project is designed for my shed so i didn't want any wires between the house and the location of the sensors. The ESP handles the interaction between wireless communications and up to 3 sensors attached directly to it. You will need to create a free account on IFTT.com and also connect to the maker channel from within IFTTT. You can download the IFTTT app onto an Iphone or an android device.

You will also need Arduino installed on your computer and we will be using the ESP8266 libraries.

This project will use the ESP-12-E module bought for around 2 Euros.

Other parts needed for this project:

USB -> Serial adapter Any adapter should do (Mine is an FDTI 230X chip)

LM1117 3.3v LDO Regulator or similar

2 x 10uF capacitors (for the regulator)

A tactile switch (not essential but makes resetting the module easier)

LED

~ 300 ohm resistor

Breadboard and jumpers

Step 1: Setting Up the Components

I started by soldering short wires to the IO's on the ESP module, although they are not all needed for this project, it is useful to connect them all for testing and further development.

Once the wires are soldered, position them into the breadboard as shown.

I also sketched the IO pins onto a scrap of paper to make wiring easier once positioned as the GPIO's are all labeled on the underside of the board.

For this demonstration you will need to make the following connections:

GND -> Common GND

VCC -> 3.3v output of the regulator

EN -> 3.3v

RESET -> Tactile Switch pos1

GND -> Tactile Switch pos2

GPIO15 -> GND

GPIO 4, 5 Will be used as inputs for the sensors

GPIO 0 Will need to be tied to GND during firmware uploading

3.3v -> Regulator Vout

10uF cap between GND and Regulator Vin (5v)

10uF cap between GND and Regulator Vout(3.3v)

GPIO 13 -> the resistor

LED between GND and the resistor

For anyone not too familiar with these components - make sure the short legs of the capacitors and the LED are placed into the GND slots ( the legs represent the 'flow' of current - the short leg represents GND and the longer leg is +ve )

ESP -> USB ADAPTER

TX -> RX

RX -> TX

GND -> GND

5v -> Regulator Vin

The 2 dangling blue wires on the sketch are the inputs for the 2 sensors

The dangling yellow wire is connected to GPIO0 , This should be connected to GND when uploading the firmware to the device.

Step 2: Getting Your Personal Key and Create Recipe

Maker Key

As stated in the introduction, you will need an IFTTT profile with the Maker channel connected to it. Once you have set that up you will need to get your unique top secret maker key. This can be accessed as soon as the channel is created.

Record this key as it will be needed in the Arduino sketch

Create The Recipe

From IFTTT.com create a new recipe

  • Click on 'This'
  • Select the Maker channel
  • Click on 'Receive a web request'
  • In the event name text box type: ESP
  • Click 'Create Trigger'
  • Click on 'That' and select IF Notification
  • Click 'Send a notification'
  • In the notification text box, type: Your Front door is: {{Value1}} Your Garage door is: {{Value2}}
  • Click 'Create Action' then 'Create Recipe'

That's it, the recipe is now ready for action. Any device with an internet connection and the IF app installed should show a notification when the recipe is run.

Next we have to setup the firmware for the ESP


Step 3: Setting Up the Firmware

Installing ESP8266 Community Files

You will need to have installed the ESP8266 commulity library in order to upload anything to the ESP from Arduino. If you haven't already done so.

To do this open Arduino, Goto File -> preferences and enter: http://arduino.esp8266.com/stable/package_esp8266... into the 'additional boards manager URL's' text box.

Then return to the main screen and goto Tool -> Board -> Boards Manager Scroll down to the ESP community files and install the package.

The clever thing about these libraries is that they turn your ESP into a virtual Arduino as well as keeping the wifi capability, pretty amazing really!

Change Sketch To Match Your Details

Now download the ESP8266_To_IFTTT.ino sketch and DataToMaker.h files from here and open in the Arduino IDE.

You will need to change 3 constants in order to get the sketch working correctly:

  • const char* myKey = This will be the Maker key you got from the previous step
  • const char* ssid = This will be your router ssid
  • const char* password = This will be your router password

Step 4: Uploading the Sketch for Testing

Once the firmware has been setup you should be ready to upload the sketch to the ESP.

To get the firmware uploading there are only a couple of settings to change from standard Arduino boards.

From the tools menu select:

  • Board -> NodeMCU 1.0
  • Frequency -> 80 MHz
  • Upload Speed -> I seem to get away with 115200 but it's best to start at 9600 and work up
  • Port -> This will be your FTDI board port
  • Programmer -> USBasp

Ok thats all good to go, All you need to do now is pull GPIO 0 low and reset the ESP, this will force it into flash boot mode which will enable us to upload the sketch. It's best to hold the reset button for a couple of seconds just to make sure it really is reset!

Now you can upload your sketch to the ESP - you should see the progress in orange at the bottom of the sketch window, at 9600 baud this will take a couple of minutes - at 115200 baud it should only take 20 seconds or so.

Step 5: Testing It All Works!

Once the sketch has uploaded, if all has gone to plan the LED you wired in should start flashing after a few seconds. If it does this indicates the board is connected to your wifi and is reading the sensor data (every second).

To see what's going on, open the serial port monitor from the Arduino IDE, make sure the baud rate is set to 115200 and 'line ending' is set to carriage return. (The baud rate is fixed at 115200 and is not related to the firmware upload speed set in the previous step!) If the firmware upload was successful you should see the current status of the ESP print out on the screen. If it looks anything like my screenshot then congrats, all the settings are good!

Once you are happy that the settings are correct and the sketch is successfully uploaded to the board you can disconnect GPIO 0 from GND and either pull it high or just leave it unplugged! This way, when the board is reset it will run your firmware instead of booting into flash mode.

Now when you toggle GPIO's 4 & 5 between GND and 3.3V you should receive a notification on your IFTTT app with the updated status of the 'test' doors!

Step 6: Taking It Further

You can now hack the sketch to death, changing GPIO's and descriptions to your hearts content! At present the Maker channel only supports up to 3 values so bear that in mind when adding to the sensor array. However there is no reason that you can't create another instance of the MakerToData class in the sketch and post to a different event name with more sensors. As far as i know all GPIO's on the ESP should be usable and directly relate to the pin numbers in the sketch.

I have ordered a couple of magnetic door sensors for 1 Euro each so that i can install the project in the shed. Then i will transfer the project to some strip board and solder a USB lead straight into the Vin on the regulator, this way i can use an old USB charger to power the project. (This will eliminate the need for the FTDI chip once the firmware is on the ESP) One improvement that I probably should make to the sketch is the use of ISR's and software debouncing, This would eliminate having to poll the IO's in the loop and would also react immediately to any sensor changes.

All in all this project provides a very cheap standalone way for your home sensors to communicate with you wherever you are (as long as you have internet available!)

If anyone has any ideas or improvements i'd be happy to hear them, The ESP is relatively new to me so I am still finding out it's true potential, so far I've been impressed at it's capabilities as both a WiFi device and a standalone micro.

Comments

author
diy_bloke made it!(author)2017-05-03

just a heads up for recent users: it seems that the screens in IFTTT have changed abit. The "M" doesnt appear anymore. It is now called "Maker Webhooks"

author
CarsonD1 made it!(author)2017-03-16

A little late to this tutorial, but is there a way to have multiple webhooks on one device? I tried this but can't get it to work:

DataToMaker event(myKey, "RELAY0ON");

DataToMaker event1(myKey, "RELAY10N");

DataToMaker event2(myKey, "RELAY0OFF");

DataToMaker event3(myKey, "RELAY1OFF");

and then call the various events by using

eventX.connect()

eventX.post()

author
CarsonD1 made it!(author)2017-03-16

I am looking to make a Wi-Fi remote of sorts and would like to have as many individual events as possible. I'm not very good at programming from scratch but I'm pretty good at adapting stuff to fit my needs.

Here is my entire code:

#include <ESP8266WiFi.h>

#include <arduino.h>

#include "DataToMaker.h"

#define SERIAL_DEBUG // Uncomment this to dissable serial debugging

// define gpio pins here:

#define RELAY_1_ON 5 // GPIO5

#define RELAY_1_OFF 4 // GPIO4

#define RELAY_0_ON 14 //GPIO13

#define RELAY_0_OFF 13

// Define program constants

*REMOVED*

// define program values

// declare new maker event with the name "ESP"

DataToMaker event(myKey, "RELAY0ON");

DataToMaker event1(myKey, "RELAY10N");

DataToMaker event2(myKey, "RELAY0OFF");

DataToMaker event3(myKey, "RELAY1OFF");

// LEAVE SET

bool connectedToWiFI = false;

void setup()

{

#ifdef SERIAL_DEBUG

Serial.begin(115200);

delay(200);

Serial.println();

#endif

delay(10); // short delay

pinMode(RELAY_1_ON, INPUT);

pinMode(RELAY_0_ON, INPUT);

pinMode(RELAY_1_OFF, INPUT);

pinMode(RELAY_0_OFF, INPUT);

WiFi.mode(WIFI_STA);

ConnectWifi();

}

void loop() {

if (wifiConnected)

{

if (digitalRead(RELAY_0_ON) == HIGH)

{

debugln("connecting...");

if (event.connect())

{

debugln("Connected To Maker");

event.post(); //

debugln("Relay 0 is on");

delay(2000);

}

else debugln("Failed To Connect To Maker!");

}

delay(1000); // pause for 1 second

if (digitalRead(RELAY_1_ON) == HIGH)

{

debugln("connecting...");

if (event1.connect())

{

debugln("Connected To Maker");

event1.post();

debugln("Relay 1 is on");

delay(2000);

}

else debugln("Failed To Connect To Maker!");

}

delay(1000); // pause for 1 second

if (digitalRead(RELAY_0_OFF) == HIGH)

{

debugln("connecting...");

if (event2.connect())

{

debugln("Connected To Maker");

event2.post();

debugln("Relay 0 is off");

delay(2000);

}

else debugln("Failed To Connect To Maker!");

}

delay(1000); // pause for 1 second

if (digitalRead(RELAY_1_OFF) == HIGH)

{

debugln("connecting...");

if (event3.connect())

{

debugln("Connected To Maker");

event3.post();

debugln("Relay 1 is off");

delay(2000);

}

else debugln("Failed To Connect To Maker!");

}

delay(1000); // pause for 1 second

}

else

{

delay(60 * 1000); // 1 minute delay before trying to re connect

ConnectWifi();

}

}

bool ConnectWifi()

{

// Connect to WiFi network

debugln();

debugln();

debug("Connecting to ");

debugln(ssid);

unsigned long startTime = millis();

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED && startTime + 30 * 1000 >= millis()) {

delay(500);

debug(".");

}

if (WiFi.status() == WL_CONNECTED)

{

debugln("");

debugln("WiFi connected");

}

else

{

WiFi.disconnect();

debugln("");

debugln("WiFi Timed Out!");

}

}

bool wifiConnected()

{

return WiFi.status() == WL_CONNECTED;

}

void debug(String message)

{

#ifdef SERIAL_DEBUG

Serial.print(message);

#endif

}

void debugln(String message)

{

#ifdef SERIAL_DEBUG

Serial.println(message);

#endif

}

void debugln()

{

#ifdef SERIAL_DEBUG

Serial.println();

#endif

}

Any suggestions on how to do this better?

author
ebox4662 made it!(author)2016-12-18

Nice approach. Much more robust then the way I was doing it. Thank you.

author
ebox4662 made it!(author)2016-12-18

The .h file I meant btw.

author
JasonB379 made it!(author)2016-08-17

I'm wondering what the reason is for needing to reset the pin each time. I would think a simple open\close comparison would be better. Currently I need to close the wire to 3.3v to cause a change, then back to ground to cause another change. Most door switches will only have open\close. I am use to door sensors that don't need to be reset, ie, open the sensor and an even is triggered, close the sensor and an event is triggered. Thoughts?

author
Mysol made it!(author)2016-08-18

Hi Jason,

If I understand your question correctly, what you probably need to do is use a pullup / pulldown resistor when connecting the door sensor.

For example, if the door switch is connected to GND then you'd tie a 10k resistor between the IO port and +3.3v. Then when the door switch is open the resistor will pullup the voltage to 3.3 and when it's closed the switch will pull it to GND. If you don't use the resistor then the connection will float when the switch is open and will cause unpredictable results.

If the switch is connected to +3.3v then you'd just do the opposite of the previous example!

I hope that helps, let me know if you need any more info.

author
JasonB379 made it!(author)2016-08-18

That's exactly what I ending up doing. Thanks for the reply and very nice project by the way.

author
Mysol made it!(author)2016-08-18

Great stuff. Thanks for the feedback :)

author
woody4165 made it!(author)2016-02-24

Hi

Very nice job.

I tried to "grab" the DataToMaker.h and use it in another sketch.

I haven't modified the .h but I used the calls.

I'm not able to make the event.post work.

I made the IFTTT recipe with ESP in the event name, same as in your sketch.

This is the part where I call the post function:

Serial.println("connecting to Maker...");

if (event.connect())

{

Serial.println("Connected To Maker");

event.post();

}

else Serial.println("Failed To Connect To Maker!");

and I get the Connected to Maker message.

I have set up the

DataToMaker event("mykey", "ESP");

and of course included the

#include "DataToMaker.h"

But I don't get the notification.

Any suggestions?

Thanks

Vittorio

author
Mysol made it!(author)2016-02-24

HI Vittorio,

Thanks, I'm glad you have got some useful info out of it :)

Have you set the event values before posting the data?

You need to use the event.setValue(int valueNumber, string message) method

If none of the 3 values are set then they will not post out to the maker site.

In the ESP8266_To_IFTTT sketch this is done in the DetectChange() method:

'event.setValue(i + 1, doorStates[val]);'

Maybe just try this before event.post() and see if it fires: event.setValue(1, "Test");

Let me know how you get on!

author
woody4165 made it!(author)2016-02-24

Thanks

It's working perfectly. I thought that parameters were optional, I haven't seen than in your code was required.

All solved.

I also found that it's possible to send notification via Pushbullet that might be interesting to test beside IFTTT.

author
Mysol made it!(author)2016-02-24

You're dead right, it is supposed to be optional. I hadn't fully tested the code, I have updated the DataToMaker.h file and uploaded to github, you should now be able to omit the values and it will just post the event name to Maker.

Thanks for the tip on Pushbullet, I'll be giving that a go when i get a spare 5 mins!

author
woody4165 made it!(author)2016-02-24

Here is where i find the pushbullet sketch.

http://www.esp8266.com/viewtopic.php?f=29&t=7116

author
Mysol made it!(author)2016-02-24

Thank's for sharing the link, it looks like a great way to sync your notifications with the devices.

I guess the benefit of IFTTT is that you are not just limited to communication with your computers and phones, you could for instance turn on a light (Philips Hue) when a trigger is set or add a row to a spreadsheet in Dropbox.

Just for info, another great platform for DIYers is the Adafruit IO, this gives you a few nice features like graphs and post history, this way you could get the data as well as posting it.. could be worth checking out:

https://io.adafruit.com/

author
seamster made it!(author)2016-02-22

Great info, and very well put together. Thank you for sharing this!

author
Mysol made it!(author)2016-02-23

Thanks seamster :~) , it was a bit rushed but hopefully there is enough information there to point folks in the right direction!

About This Instructable

26,516views

104favorites

License:

More by Mysol:ESP8266 To IFTTT Using Arduino IDE
Add instructable to: