Battery Powered Shed Door & Lock Sensor, Solar, ESP8266, ESP-Now, MQTT
Introduction: Battery Powered Shed Door & Lock Sensor, Solar, ESP8266, ESP-Now, MQTT
In this Instructable I show you how I made a battery powered sensor to monitor the door and lock status of my remote bike shed. I have nog mains power, therefor I have it battery powered. The battery is charged by a small solar panel.
The module is designed for low power operation and runs on an ESP-07S in deep sleep which wakes up and checks the door and lock position every minute. However, when the door is opened, the module is woken by a simple hardware circuit to immediately send the 'door open' information. The module communicates via ESP-Now, in which the transmission time is very short, requiring only a small amount of energy.
My home automation running on Openhab and Mosquitto handles the messages and sends my an alarming message via Telegram if the alarm is switched on.
Supplies
All components are bought from Aliexpress.
* ESP-07S module is chosen for the easy connection of an external antenna to increase the ESP-Now range.
* TP4056 charger board with battery protection
* 18650 LiPo battery
* Reed switch (NO to monitor door position)
* Contact switch (monitor lock position)
* Solar panel (6V, 0.6W)
* Transistors, resistors, diode, connectors (see schematic)
Step 1: Hardware
The as-built schematic is included as a picture. I firstly prototyped the circuit on a breadboard. Then I soldered all components on a perf board.
I use a ESP-07S ESP8266 module since it has a connection for an external antenna. Since my bike shed is outside, the WiFi signal needs to go through a concrete wall. I found out that an external antenna strongly increases the range of the ESP-Now. Quite logical, since it is a WiFi signal.
For the door sensor I used a reed switch with botn NO and NC connections. When the door is closed, a magnet attached to the opens the switch. The module checks the door and lock state every 60 seconds, however, when the door is opened, I want to be informed immediately, therefor I implemented a reset circuit, see below.
For the lock sensor I used a contact switch with botn NO and NC connections. When the lock is closed, the lock pin opens the switch. So, both the door sensor and the lock sensor are normally opened (NO).
The battery is charged via a TP4056 charger board with battery protection attached to a small 6V solar panel.
I will explain some parts of the circuit below.
Reset circuit
The reset circuit with the 2N7000 Mosfet is connected to the reset-pin of the ESP8266. 382
If the door is closed, the contact is open, both the gate and the source of the transistor are high and the mosfet is off. The capacitor connected to the gate has a positive charge. The ESP8266 reeds GPIO12 as HIGH = closed.
When the door is opened, the source of the mosfet is connected to ground. Since the gate is high, the mosfet is switched on and pulls the reset pin to ground, with results in a reset of the ESP8266. The capacitor is discharged via R7 and then switches the mosfet off. See the screenshot of my oscilloscope for the low pulse of 50 ms. After the pulse, the ESP8266 boots up. The ESP8266 reeds GPIO12 as LOW = open.
When the door is closed again, resistor R6 pulls the source and GPIO12 up.
Battery monitoring
The battery voltage is read via a voltage divider between VBat and GND. However, I do not want a permanent connection between VBat and GND, because it drains the battery. Therefor I put a P-channel mosfet at the high side of the voltage divider and the gate of the mosfet is pulled-up, so the mosfet is off. Only when GPIO14 is low, the mosfet is switched on and the ESP8266 can reed the voltage with the ADC.
Step 2: Software
The ESP8266 module mostly is in deep sleep mode to save power.
Every 60 seconds, the module boots up with WiFi disabled and measures the lock and door position and checks whether these positions have changed compared to the values stored in the RTC memory. If a position has changed, the module sleeps for a minimum time and wakes up with WiFi enabled to send the new position via ESP-Now. And of course the new positions are stored in the RTC-memory. If nothing was changed, the module just sleeps again and wakes up with WiFi off. 382