Over a million developers have joined DZone.

Make Your Own Wi-Fi Window/Door Sensor

With the right tools and a little elbow grease, you can make this handy monitor to make sure your home is shut tight.

· IoT Zone

Access the survey results 'State of Industrial Internet Application Development' to learn about latest challenges, trends and opportunities with Industrial IoT, brought to you in partnership with GE Digital.

Introduction

Welcome, class. It's time for another DIY project, bringing IoT to your home. Today, we're going to learn how to set up your own Wi-Fi-enabled sensor, which you can use to monitor windows, doors, and the like. Let's get started.



Door/window sensor historical chart.


Materials

ESP8266 WiFi module:

ESP8266 ESP-01 Serial WIFI Wireless Transceiver ModuleESP8266 ESP-01 Serial WIFI Wireless Transceiver Module

$2.05

$2.85
ESP8266 ESP-03 Serial WIFI Wireless Transceiver ModuleESP8266 ESP-03 Serial WIFI Wireless Transceiver Module

$2.45
ESP8266 ESP-12 Serial WIFI Wireless Transceiver ModuleESP8266 ESP-12 Serial WIFI Wireless Transceiver Module

$2.33

$2.83
ESP8266 ESP-05 Serial WIFI Wireless Transceiver ModuleESP8266 ESP-05 Serial WIFI Wireless Transceiver Module

$1.88

$3.89
ESP8266 ESP-07 Serial WIFI Wireless Transceiver ModuleESP8266 ESP-07 Serial WIFI Wireless Transceiver Module

$2.18

$1.87
ESP8266 Lua Nodemcu WIFI Network Development BoardESP8266 Lua Nodemcu WIFI Network Development Board

$7.43

$3.49


Door/window magnetic switch:

10 pcs MC-38 Wired Door Window Sensor Magnetic Switch10 pcs MC-38 Wired Door Window Sensor Magnetic Switch

$9.35

$10.93
 

3.3V power supply (battery or regulator):

5pcs 3.3V regulator module 800mA5pcs 3.3V regulator module 800mA

$1.2

$1.61
2xAA 3V Battery Holder Box Case Wire2xAA 3V Battery Holder Box Case Wire

$1.09

$0.94


EasyIoT Cloud Configuration 

Register to the EasyIoT Cloud service. If you do not receive a registration email, check your spam folder. If it's not there, contact us and we will activate your account.

Then go to Configure->Modules->Add Module and select Digital Input (DI). Next, Click Sensor.Parameter1.


For the first parameter, name it Sensor.Parameter1, then check off DB Logging and Chart Steps.

DB Logging means that we will log values in the database, and historical values can be displayed in a chart. Chart Steps means that chart values are not interpolated.


Press Save Parameter. After saving, you will see InstanceId/ParameterId — remember this value, you will need it later.


Go back to the module configuration.

Next, we will change the module icons. Available icons are visible under Configuration->Module Icons. You can also upload your own icon.

Add two parameters (Add Parameter button) and  set the first parameter's name to Settings.Icon1 and set the icon to Value to door_1.png. For the second parameter, set the name as Settings.Icon2 and make the icon Value: door_2.png.

We will also change the module text. Add two parameters and name the first one Settings.StatusText1. Set the Value to Open. Name the second parameter Settings.StatusText2, and for the value, set it to Close.

Now you should see five parameters under the module configuration:


Program

The program is written in the Arduino ESP8266 IDE. See the Arduino ESP8266 IDE tutorial to learn how to connect your ESP8266 module to a computer to upload the program. The program is available at our GitHub. You will also need the EIoTCloudRestApi library. In the library, set the access point username and password. 

In the program change EIOT_CLOUD_INSTANCE_PARAM_ID:

// EasyIoT Cloud definitions - change EIOT_CLOUD_INSTANCE_PARAM_ID

#define EIOT_CLOUD_INSTANCE_PARAM_ID    "xxxx"

Put your InstanceId/ParameterId in the parameter configuration.


If you change the GPIO pin on the ESP8266, also change the GPIO pin in the program.

/*
  Created by Igor Jarc
 See http://iot-playground.com for details
 Please use community forum on website do not contact author directly

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 version 2 as published by the Free Software Foundation.
 */
#include <ESP8266WiFi.h>
#include "EIoTCloudRestApi.h"

// EasyIoT Cloud definitions - change EIOT_CLOUD_INSTANCE_PARAM_ID
#define EIOT_CLOUD_INSTANCE_PARAM_ID    "xxxx"

#define INPUT_PIN        5

EIoTCloudRestApi eiotcloud;
bool oldInputState;

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

    pinMode(INPUT_PIN, INPUT_PULLUP);

    oldInputState = !digitalRead(INPUT_PIN);
}

void loop() {
    int inputState = digitalRead(INPUT_PIN);;  

    if (inputState != oldInputState)
    {
        eiotcloud.sendParameter(EIOT_CLOUD_INSTANCE_PARAM_ID, (inputState == 1));
        oldInputState = inputState;
    }
}


Hardware

In our case, we used a NodeMcu ESP8266, but you can use any other ESP8266 with a FDTI programmer. For the power supply, you can use 2 AA batteries or a 3.3V regulator.

You need one wire to connect the magnetic switch to GPIO5 and other to the ground, and that's it. 

ESP8266 door/window sensor


See more tutorials at http://iot-playground.com/build.

The IoT Zone is brought to you in partnership with GE Digital.  Discover how IoT developers are using Predix to disrupt traditional industrial development models.

Topics:
gpio ,switch ,sensor ,wifi ,arduino

Published at DZone with permission of Igor Jarc, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}