Random Nerd Tutorials

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.

Before continue reading this project, please complete the following tutorials or use them as a reference:

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


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.

ifttt signup

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:

Next, you need to grant access permissions:

Let your IFTTT account connect to your Gmail account. A new page loads when you finish connecting your Gmail.

Complete the Applet

Fill the Applet with your own information. Follow these instructions:

  1. Type “door_status” in your event name
  2. Add an email address
  3. Edit the subject and body of your email. Do not delete the Value1 in the subject
  4. Press the “Save” button

Now, go to this URL: https://ifttt.com/maker_webhooks and open the “Settings” tab.

Copy you secret key to a safe place (you’ll need them later in this project). In my example my secret key is: b6eDdHYblEv2Sy32qLwe

Test Your Applet

Let’s test if your request is working properly. Replace YOUR_API_KEY from the following URL:


With your API KEY:


Open your URL with your API KEY in your browser.

ifttt triggered

You should see something similar to the preceding Figure. Then go to your email client and new email should be there!


Parts List

Here’s the hardware that you need to complete this project:

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.

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”.

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:
 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";
            door_state = "closed";
        flag = true;

void setup() {
    Serial.println("Preparing the Door Status Monitor project...");
    pinMode(pin, OUTPUT);
    attachInterrupt(digitalPinToInterrupt(pin), changeDoorStatus, CHANGE);

    Serial.print("Connecting to ");
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
    Serial.println("WiFi connected");  
    Serial.println("IP address: ");

void loop() {

          Serial.print("connecting to ");
          WiFiClient client;
          const int httpPort = 80;
          if (!client.connect(host, httpPort)) {
            Serial.println("connection failed");
          String url = "/trigger/door_status/with/key/";
          url += apiKey;
          Serial.print("Requesting 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;

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.

magnetic contact switch_bb


For prototyping/testing you can apply the magnetic reed switch to your door using Velcro.

esp8266 featured image

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

Learn ESP32 with Arduino IDE

This is our complete guide to program the ESP32 with Arduino IDE, including projects, tips, and tricks! The registrations are open, so SIGN UP NOW »

Recommended Resources

Home Automation using ESP8266 »
Build IoT projects and home automation gadgets with the ESP8266 Wi-Fi module.

Build a Home Automation System »
Learn how to build a automation system using open-source hardware and software from scratch.

Arduino Step-by-Step Projects »
Build 25 cool Arduino projects with our course even with no prior experience!

Leave a Comment:

Add Your Reply