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.
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.
The reset circuit with the 2N7000 Mosfet is connected to the reset-pin of the ESP8266. 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.
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.
See my other Instructable in which I explain how I use ESP-Now to transmit message and transform them to MQTT messages.
If the 'OTA-circuit' is manually closed via a jumper, the module wakes up and connects to my WiFi network to wait for an OTA update via ESP8266HTTPUpdateServer.
Every 30 minutes the voltage of the battery is measured and published.
It works as a state machine. The states are defined in the program which is published on my Github.
STATE_CHECK: wake up with Radio off (WiFi off), just check if something has changed
STATE_INIT: wake up with Radio on (WiFi on) and transmit door and lock states
STATE_DOOR: wake up with Radio on, publish doorstate next time it boots up
STATE_LOCK: wake up with Radio on, publish lockstate next time it boots up
STATE_VOLTAGE: wake up with Radio on, publish voltage next time it boots up
STATE_OTA 5: wake up with Radio on, go to OTA modus
Step 3: Assemble
I use screw terminals and DC male/female connectors to be able to assemble and disassemble my project. I put all parts in a small ABS box, see the pictures. I encapsulated the parts in Kapton tape for electric isolation
I connect the solar panel via a male DC-plug (5.5 x 2.1) with a 1N5817 diode which has a low forward voltage.
The reed switch is glued in the box and a magnet is glued on the door at the right position.
The lock contact is entered from the side, see the picture.
Step 4: Working Module
The received data is read by my Openhab home automation. I you like, I can post the Openhab files.
- The battery voltage (with persistence so I see the voltage over time in a graph).
- The door and lock positions.
- The times the position has changed.
In this way, when I go to bed, I can easily see whether all sheds are locked.
I the beginning of the use, the battery was charged on a bright day, and after a week or so tge battery was fully charged. Now in autumn, the battery remains charged. Apparently the module is very economic and uses much less energy then a small solar panel generates. The beefy battery probably has power for a few months of darkness. Lets see how the module performs this winter, when the temperature in the shed is much lower.
Participated in the
Battery Powered Contest