In this tutorial we will show how to build door/window sensor only with ESP8266 and NodeMCU(LUA) - without Arduino. We will use nice EasyIoT interface to display door/windows status on our phone, tablet or desktop computer.

ESP8266 NodeMcu

First step when building this sensor is to upload correct firmware to our ESP8266 module. We will use node NodeMCU firmware. This firmware is capable of executing LUA script. Latest NodeMCU firmware can be download here NodeMCU. Read ESP8266 firmware update tutorial for instructions how to update ESP8266 firmware.

Next we will add virtual door/window sensor to EasyIoT server. You can download EasyIoT server (Windows or Raspberry Pi version) on EasyIoT server download page.

Material

  • ESP8266 module
  • Reed relay
  • 100nF ceramic capacitor

See buying guide at the end of tutorial.

EasyIoT server configuration

Go to Configure->Drivers->Virtual driver and press button Add node. Virtual driver must be enabled.

Remember node address. In this case is N9S0.

Then go back in Groups and modules and newly added module (address N9S0).

Then name senosr and change type to Door/Window(Di) sensor.

 

After you add sensor it should be like this.

Upload LUA program

To upload LUA program to ESP8266 we need LUAloader. It can be found here. If you just updated firmware don't forget to disconnect GND wire connected to GPIO0 - we do not need it any more. 

LUA program can be found on GitHub. There are two files: init.lua and lua_easyiot_doorwindow.lua. Download both files and correct AP and AP password in init.lua. In file lua_easyiot_doorwindow.lua correct server IP address EasyIoT username and password and node address. In our case node address is N9S0, but in your case can be different. 

Change this line in init.lua:

wifi.sta.config("AP","password")

and this in lua_easyiot_doorwindow.lua:

easyiot_username = "admin"

easyiot_password = "test"

easyiot_server = "192.168.1.5"

easyiot_node_address = "N9S0"

 

Connecting

Now we just need to connect wires. Add magnetic reed relay switch and 100nF capacitor for debouncing and two AA batteries. See buying guide for details.

ESP8266 door/window sensor 

Check also EasyIoT Cloud ESP8266 door/window sensor.

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

Buying guide

To support this site and EasyIoT framework development please buy in our store.

10 pcs MC-38 Wired Door Window Sensor Magnetic Switch

100pcs Mini 50V Ceramic Disc Capacitors 100nF

2xAA 3V Battery Holder Box Case Wire

This 2xAA battery holder puts a nice finishing touch on your battery powered project.

10Pcs 2xAA Plastic Battery Storage Case Box Holder

This 2xAA battery holder puts a nice finishing touch on your battery powered project.

ESP8266 ESP-01 Serial WIFI Wireless Transceiver Module

ESP8266 Serial WIFI Wireless Transceiver Module

ESP8266 ESP-03 Serial WIFI Wireless Transceiver Module

ESP8266 serial WIFI model ESP-03

ESP8266 ESP-12 Serial WIFI Wireless Transceiver Module

ESP8266 serial Port WIFI wireless module ESP-12

ESP8266 ESP-05 Serial WIFI Wireless Transceiver Module

ESP8266 ESP-05 Serial WIFI Wireless Transceiver Module


Comments   

+1 #21 Kari Vainio 2016-02-18 10:09
Is it possible to add second sensor to node?
For example, I have a door closed/open sensor and I'd like to add another sensor for the lock of the same door. Then I would know that door is "closed and locked" or door is "closed and unlocked".
+1 #20 Bob 2015-11-30 17:39
Think you need to update the nodemcu link as Its showing 404 not found.
#19 DarioCdj 2015-06-09 13:40
Quoting Gilson:
How long should the battery last in this setup?
Thanks,
Gilson

Forget the battery... you must use a power supply and an esp different from ESP-01 or it doesn't work on reboot if reed is closed.
#18 Gilson 2015-06-07 19:01
How long should the battery last in this setup?
Thanks,
Gilson
#17 DarioCdj 2015-06-04 12:11
And finally it works !!!

Ok to connect gpio2 to reed and reed to +3.3v
Ok to modify code :
gpio.mode(4,gpio.INT,gpio.PULLDOWN)

BUT IS ALSO NECESSARY TO MODIFY :

command = 'ControlOff'
if (status == 0) then
command = 'ControlOn'
end

(and not status == 1 !!!)
#16 DarioCdj 2015-05-29 16:44
Again try to fix this, assuming that reed has two cable, 1 and 2:

1) connect reed1 to +3.3v
2) connect gpio2 to +3.3v
3 connect reed2 to gnd

this produce a loop of "1" status....

1) connect reed1 to +3.3v
2) connect gpio2 to reed2

this produce a NULL status... nothing appear when i close or open reed

gpio.mode(4,gpio.INT,gpio.PULLDOWN) line modified

obviously in pull-up mode with gpio2 to reed1 and gnd to reed2 works perfectly, but when reset with closed door it doesn't start correct as you discovered.

Please can you explain better how to fix it ?
Dario
#15 DarioCdj 2015-05-11 10:39
No Waitamin,
if i connect reed to 3.3v coming out from AMS1117 and to GPIO2 it doesn't work, obviously code modified.

Try to debug it in breadboard but when open and close reed do nothing in this way.....
#14 Super User 2015-05-10 23:00
Quoting DarioCdj:
Do you intend to connect GPIO2 to VCC and Reed to GPIO0 ? you've wrote GPIO :-)
Thanks in advance
Dario

Quoting Super User:
Quoting Allan Madsen:
Sorry to say, this dos not work at all :-(
How to fix it, i like the hole idea in this program :-)


Later I've discovered one little detail about ESP8266. While power up GPIO2 must be connected to VCC to start in correct mode. In my test case it happened that door was open and GPIO2 was on VCC. If you start this example with door closed it won't work because ESP8266 wont start int correct mode. There are two solutions:
-connect reed relay and capacitor to VCC and GPIO. That way GPIO2 will be always connected to VCC at startup. Also correct line gpio.mode(4,gpio.INT,gpio.PULLUP) to gpio.mode(4,gpio.INT,gpio.PULLDOWN)
-use different ESP8266 model and connect reed relay to different GPIO pin. Also correct program to work with different GPIO.


GPIO2, I will fix comment.
#13 DarioCdj 2015-05-10 16:01
Do you intend to connect GPIO2 to VCC and Reed to GPIO0 ? you've wrote GPIO :-)
Thanks in advance
Dario

Quoting Super User:
Quoting Allan Madsen:
Sorry to say, this dos not work at all :-(
How to fix it, i like the hole idea in this program :-)


Later I've discovered one little detail about ESP8266. While power up GPIO2 must be connected to VCC to start in correct mode. In my test case it happened that door was open and GPIO2 was on VCC. If you start this example with door closed it won't work because ESP8266 wont start int correct mode. There are two solutions:
-connect reed relay and capacitor to VCC and GPIO. That way GPIO2 will be always connected to VCC at startup. Also correct line gpio.mode(4,gpio.INT,gpio.PULLUP) to gpio.mode(4,gpio.INT,gpio.PULLDOWN)
-use different ESP8266 model and connect reed relay to different GPIO pin. Also correct program to work with different GPIO.
#12 Super User 2015-05-10 14:22
Quoting Allan Madsen:
Sorry to say, this dos not work at all :-(
How to fix it, i like the hole idea in this program :-)


Later I've discovered one little detail about ESP8266. While power up GPIO2 must be connected to VCC to start in correct mode. In my test case it happened that door was open and GPIO2 was on VCC. If you start this example with door closed it won't work because ESP8266 wont start int correct mode. There are two solutions:
-connect reed relay and capacitor to VCC and GPIO2. That way GPIO2 will be always connected to VCC at startup. Also correct line gpio.mode(4,gpio.INT,gpio.PULLUP) to gpio.mode(4,gpio.INT,gpio.PULLDOWN)
-use different ESP8266 model and connect reed relay to different GPIO pin. Also correct program to work with different GPIO.

You have no rights to post comments

Forum latest

  • No posts to display.