Code Lock with ESP8266
Hardware needed:
Wemos D1 Mini (ESP8266)
Some kind of electric lock.
A connected matrix keypad with digits 0 through 9, size 3x4.
The matrix is connected to pins D1 through D7:
D1 to D4 are connected to the rows.
D5 to D7 are connected to the columns.
A relay board to control the lock connected to D8.
Optional reed switch to detect unlocked/locked/door opened/closed connected to D0.
Additionally, a 5V power supply is required, such as from a USB charger.
Features:
WiFi connectivity.
Admin web page.
MQTT connectivity for automations with Home Assistant or similar.
Supports 8 multiple individual codes, consisting of the digits 0 through 9, with a maximum length of 8 digits.
Different codes can be valid at specific hours of the day.
Unlock the lock using the keypad by entering a valid code, or
unlock from admin page, or unlock from MQTT.
If "AlwaysOpen" is set, the lock will be unlocked all the time.
Automatically connects to an MQTT broker if one is configured.
Unlock and set AlwaysOpen from MQTT.
The program updates an MQTT server/topic with activity, including timestamps, who unlocked the door, commands published from MQTT and admin page, and input from the keypad.
Optional detection when the lock is in the unlocked state using a reed switch connected to D8. (Switch is assumed closed when the door is locked.)
You can detect if the door is opened/closed instead, if there is trouble detecting the locked/unlocked state of your lock.
Serial interface enabled in USB, 9600 baud.
Wi-Fi Connectivity:
Configure initial Wi-Fi settings via a simple Access Point (AP) and a web page.
WiFi Settings are stored in non volatile memory.
Password-Protected Admin Web Page:
The admin page features:
A button to manually unlock the door.
A button to permanently put the lock in always-unlocked mode".
Managing codes, their validity periods, and comments. There is a button to delete configuration file if needed which at reboot will create the default config file.
Configuring the MQTT broker (address, username, password, topic).
If you have multiple doors, door name and MQTT topic can be set for ease of identification.
Changing the admin password for the admin page.
Configuration Storage:
Configuration is saved in a JSON file on a non-volatile partition, ensuring it persists across reboots or firmware uploads.
A default configuration is created if no valid configuration file exists or if it becomes corrupted.
Default Settings:
Default code #1: 12345678
Default admin web page password: adminpass
Default door/lock name: Door
Known issues:
Some unidentified bugs in admin page when adding or changing codes or settings;
Always refresh and only edit and save one code/section at a time, and if it doesn't work to save the first time, retry.)
No https encryption on web page.
No TLS encryption for MQTT connection.
TBD: Setting correct time zone from admin page.
#include <Arduino.h>
#include <LittleFS.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <PubSubClient.h>
#include <WiFiManager.h>
#include <NTPClient.h>
#include <Keypad.h>
#define JSON_FILENAME "/config.json"
/*
[env:esp12e]
...
Read more »
Integration with Home Assistant etc. is possible trough MQTT.
Since every keypad stroke is sent to MQTT, you can automate/compare codes to known numbers in a database or similar.
The CodeLock also subscribes and responds to valid MQTT commands like Open, AlwaysOpenOn, AlwaysOpenOff which makes it easy to have a door unlocked during business hours as an example.