Door Status Monitor using the ESP8266
In this project you’re going to monitor the status of a door using an ESP8266. The goal of this project is to show the endless possibilities that this $4 WiFi module offers when integrates with a free platform that I’m about to show you.
https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/featured-image-resized.jpg?resize=300%2C169&ssl=1 300w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/featured-image-resized.jpg?resize=768%2C432&ssl=1 768w" sizes="(max-width: 700px) 100vw, 700px" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">
Before continue reading this project, please complete the following tutorials or use them as a reference:
- How to get started with the ESP8266
- How to Install the ESP8266 Board in Arduino IDE
- Monitor Your Door Using Magnetic Reed Switch and Arduino
If you like the ESP and you want to do more projects you can read my eBook Home Automation Using ESP8266.
Let’s get started!
First, watch the video demonstration below
About IFTTT
In order to accomplish this task you have to sign up for one free service called IFTTT which stands for “If This Then That”.
IFTTT is a platform that gives you creative control over dozens of products and apps.
You can make apps work together. For example when you send a request to IFTTT, it triggers a recipe that sends you an email alert.
Creating Your IFTTT Account
Creating an account on IFTTT is free!
Go the official site: https://ifttt.com/ and click the “Sign Up” button in top of the page.
Complete the form with your personal information (see figure below) and create your account.
https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/ifttt-signup.png?resize=300%2C257&ssl=1 300w" sizes="(max-width: 402px) 100vw, 402px" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">
After creating your account, follow their getting started tutorial.
Open the Applet
I’ve created an Applet that integrates perfectly in this project and you can also use it.
If you’re logged in at IFTTT and you open this URL below to use my Applet instantly:
Press the “Turn on” button:
https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/turn_applet_on.png?resize=88%2C150&ssl=1 88w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/turn_applet_on.png?resize=177%2C300&ssl=1 177w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/turn_applet_on.png?resize=47%2C80&ssl=1 47w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/turn_applet_on.png?resize=129%2C220&ssl=1 129w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/turn_applet_on.png?resize=59%2C100&ssl=1 59w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/turn_applet_on.png?resize=140%2C238&ssl=1 140w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/turn_applet_on.png?resize=244%2C415&ssl=1 244w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/turn_applet_on.png?resize=287%2C487&ssl=1 287w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/turn_applet_on.png?resize=350%2C595&ssl=1 350w" sizes="(max-width: 230px) 100vw, 230px" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">
Next, you need to grant access permissions:
https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/select_ok.png?resize=141%2C150&ssl=1 141w, https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/select_ok.png?resize=282%2C300&ssl=1 282w, https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/select_ok.png?resize=75%2C80&ssl=1 75w, https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/select_ok.png?resize=207%2C220&ssl=1 207w, https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/select_ok.png?resize=94%2C100&ssl=1 94w, https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/select_ok.png?resize=224%2C238&ssl=1 224w, https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/select_ok.png?resize=391%2C415&ssl=1 391w" sizes="(max-width: 399px) 100vw, 399px" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">Let your IFTTT account connect to your Gmail account. A new page loads when you finish connecting your Gmail.
https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/configure-applet.png?resize=82%2C150&ssl=1 82w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/configure-applet.png?resize=164%2C300&ssl=1 164w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/configure-applet.png?resize=44%2C80&ssl=1 44w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/configure-applet.png?resize=120%2C220&ssl=1 120w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/configure-applet.png?resize=55%2C100&ssl=1 55w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/configure-applet.png?resize=130%2C238&ssl=1 130w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/configure-applet.png?resize=227%2C415&ssl=1 227w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/configure-applet.png?resize=266%2C487&ssl=1 266w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/configure-applet.png?resize=325%2C595&ssl=1 325w" sizes="(max-width: 288px) 100vw, 288px" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">
Complete the Applet
Fill the Applet with your own information. Follow these instructions:
- Type “door_status” in your event name
- Add an email address
- Edit the subject and body of your email. Do not delete the Value1 in the subject
- Press the “Save” button
https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/configure-applet-2.png?resize=66%2C150&ssl=1 66w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/configure-applet-2.png?resize=131%2C300&ssl=1 131w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/configure-applet-2.png?resize=35%2C80&ssl=1 35w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/configure-applet-2.png?resize=96%2C220&ssl=1 96w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/configure-applet-2.png?resize=44%2C100&ssl=1 44w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/configure-applet-2.png?resize=104%2C238&ssl=1 104w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/configure-applet-2.png?resize=182%2C415&ssl=1 182w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/configure-applet-2.png?resize=213%2C487&ssl=1 213w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/configure-applet-2.png?resize=260%2C595&ssl=1 260w" sizes="(max-width: 267px) 100vw, 267px" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">
Now, go to this URL: https://ifttt.com/maker_webhooks and open the “Settings” tab.
https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/settings-tab.png?resize=150%2C58&ssl=1 150w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/settings-tab.png?resize=300%2C116&ssl=1 300w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/settings-tab.png?resize=768%2C298&ssl=1 768w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/settings-tab.png?resize=80%2C31&ssl=1 80w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/settings-tab.png?resize=220%2C85&ssl=1 220w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/settings-tab.png?resize=250%2C97&ssl=1 250w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/settings-tab.png?resize=280%2C108&ssl=1 280w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/settings-tab.png?resize=510%2C198&ssl=1 510w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/settings-tab.png?resize=750%2C291&ssl=1 750w" sizes="(max-width: 826px) 100vw, 826px" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">
Copy you secret key to a safe place (you’ll need them later in this project). In my example my secret key is: b6eDdHYblEv2Sy32qLwe
https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/copy-ifttt-api-key.png?resize=300%2C232&ssl=1 300w" sizes="(max-width: 525px) 100vw, 525px" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">
Test Your Applet
Let’s test if your request is working properly. Replace YOUR_API_KEY from the following URL:
https://maker.ifttt.com/trigger/door_status/with/key/YOUR_API_KEY
With your API KEY:
https://maker.ifttt.com/trigger/door_status/with/key/b6eDdHYblEv2Sy32qLwe
Open your URL with your API KEY in your browser.
https://i1.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/ifttt-triggered.png?resize=300%2C148&ssl=1 300w" sizes="(max-width: 592px) 100vw, 592px" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">
You should see something similar to the preceding Figure. Then go to your email client and new email should be there!
https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/email.png?resize=300%2C64&ssl=1 300w, https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/email.png?resize=768%2C164&ssl=1 768w" sizes="(max-width: 788px) 100vw, 788px" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">
Parts List
Here’s the hardware that you need to complete this project:
- ESP8266 – read Best ESP8266 Wi-Fi Development Boards
- FTDI programmer
- 1× Magnetic Reed Switch
- 1× 10kΩ resistor
- 1× breadboard
- Jumper wires
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Schematics (3.3V FTDI Programmer)
The schematics to upload code to your ESP8266 are very straight forward. You only need to establish a serial communication between your FTDI programmer and your ESP8266 to upload some code.
https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2015/05/Flashing-Firmware-FTDI-Programmer.png?resize=300%2C81&ssl=1 300w" sizes="(max-width: 756px) 100vw, 756px" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">
Uploading your ESP8266 code
Having the ESP8266 add-on for the Arduino IDE installed (How to Install the ESP8266 Board in Arduino IDE).
Go to Tools and select “Generic ESP8266 Module”.
https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2015/09/Arduino-IDE-select-esp8266.png?resize=255%2C300&ssl=1 255w" sizes="(max-width: 600px) 100vw, 600px" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">
Copy the sketch below to your Arduino IDE. Replace the SSID, password and the IFTTT API Key with your own credentials.
/*
Created by Rui Santos
All the resources for this project:
http://randomnerdtutorials.com/
Based on some ESP8266 code examples
*/
#include <ESP8266WiFi.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const char* host = "maker.ifttt.com";
const char* apiKey = "YOUR_IFTTT_API_KEY";
int pin = 2;
volatile int state = false;
volatile int flag = false;
const char* door_state = "closed";
unsigned long previousMillis = 0;
const long interval = 3000;
void changeDoorStatus() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
state = !state;
if(state) {
door_state = "opened";
}
else{
door_state = "closed";
}
flag = true;
Serial.println(state);
Serial.println(door_state);
}
}
void setup() {
Serial.begin(115200);
delay(100);
Serial.println("Preparing the Door Status Monitor project...");
pinMode(pin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(pin), changeDoorStatus, CHANGE);
Serial.println();
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());
}
void loop() {
if(flag){
Serial.print("connecting to ");
Serial.println(host);
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
String url = "/trigger/door_status/with/key/";
url += apiKey;
Serial.print("Requesting URL: ");
Serial.println(url);
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: 13\r\n\r\n" +
"value1=" + door_state + "\r\n");
flag = false;
}
delay(10);
}
After modifying my sketch upload it to your ESP8266 (If you can’t upload code to your ESP8266, read this troubleshooting guide).
Final Circuit
Now follow the schematics below to create your Door Status Monitor.
https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/magnetic-contact-switch_bb-2.png?resize=295%2C300&ssl=1 295w, https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/02/magnetic-contact-switch_bb-2.png?resize=768%2C780&ssl=1 768w" sizes="(max-width: 406px) 100vw, 406px" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto;">
Demonstration
For prototyping/testing you can apply the magnetic reed switch to your door using Velcro.
Now when someone opens/closes your door you get notified via email (watch the video at the beginning of this project for a live demonstration).
Wrapping Up
Do you have any questions? Leave a comment down below!
Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog.
P.S. If you got stuck during this tutorial make sure you read “ESP8266 Troubleshooting Guide“
ESP8266 Multisensor Shield with Node-RED