Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I created a remote control web server garage "man door" deadbolt lock/unlock system using an ESP8266 web server on an ESP8266 developer proto board, all powered by a 5V DC supply, via a 120V AC to 5V DC power brick. I first bought a 2 x 6 piece of wood from Home Depot to test this out before drilling into my actual garage man door. Once I drilled all the holes for the deadbolt on the 2 x 6 I then purchased a 4 x 4 inch clear acrylic box, with a removable top lid. This will house the servo and the ESP8266 board. I also added, to the removable lid, a flat-head green and red LED, and a 16mm momentary on push-button switch with a red LED indicator, all run on 5V DC. I also added a 5V passive buzzer to the removable lid. When the deadbolt locks you will hear 2 short beeps; when unlocked 1 short beep.
- The clear acrylic box straddles the deadbolt and the servo must be held stationary so that when it turns the deadbolt 90 degrees, the servo must stay stationary. I connected the servo directly to the shaft of the deadbolt, you have to remove the turn knob to do that. I then had to purchase a coupler to attach the servo to the exposed shaft that the turn knob was attached to, and I tightened the coupler down with the supplied allen wrench screw.
- On the ESP developer proto board, I ran a wire from VIN 5V to power 5V to a column of holes on the board to power the LED's and the buzzer. I added three 2N2222A transistors one for the green 5V LED one for the red 5V LED and one for the 5V buzzer.
- The emitter of the transistor connects to ground, and the collector of the transistor connects to the negative of the LED's and the buzzer. The positive side of all LED's and the buzzer connect to the 5V column.
- You can control the deadbolt wireless via the web server on your local LAN either by a PC or your mobile phone. I have an Android phone, and I run Linux Mint PC's.
- You can control the deadbolt via the web server or manually by pressing the 16mm momentary on button. Press once, it will lock the red LED indicator on the button lights up red, red LED on acrylic box lid "which faces out toward the user" lights up red and you will her 2 beeps. Press the button again, the red LED turns off, the green LED turns on, and you will hear 1 beep. The same actions apply with the web server via phone or PC.
- To make the servo stay stationary, I used mini tiger clamps drilling a hole on the sides of the box and securing the clamps with the supplied nuts.
- HTH
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #include <Servo.h>
- /*
- John's Garage Man Door Lock / Unlock
- -----------------------------------
- • Momentary pushbutton toggles lock
- • Servo-driven deadbolt
- • Web UI lock / unlock
- • Red / Green status LEDs
- • Buzzer chirps:
- - 1 beep = UNLOCKED
- - 2 beeps = LOCKED
- • millis()-based servo timing
- • Explicit state machines (lock + buzzer)
- */
- // ----------------------------------------------------
- // PIN SETUP
- // ----------------------------------------------------
- const int SERVO_PIN = 2; // D4 (GPIO2)
- const int BUTTON_PIN = 14; // D5 (GPIO14) momentary button → GND
- const int RED_LED_LOCKED = 5; // D1 (GPIO5) RED LED
- const int GREEN_LED_UNLOCKED = 13; // D7 (GPIO13) GREEN LED
- const int BUZZER_PIN = 15; // D8 (GPIO15)
- // ----------------------------------------------------
- // SERVO POSITIONS (PHYSICALLY VERIFIED)
- // ----------------------------------------------------
- const int LOCK_POS = 90; // Physically LOCKS deadbolt
- const int UNLOCK_POS = 0; // Physically UNLOCKS deadbolt
- const unsigned long MOVE_TIME = 1200; // ms servo travel time
- // ----------------------------------------------------
- // WIFI CONFIG
- // ----------------------------------------------------
- const char* ssid = "xxxxxxxx";
- const char* password = "xxxxxxxx";
- IPAddress staticIP(xxx,xxx,xxx,xxx);
- IPAddress gateway (xxx,xxx,xxx,xxx);
- IPAddress subnet (xxx,xxx,xxx,xxx);
- IPAddress dns1 (xxx,xxx,xxx,xxx);
- ESP8266WebServer server(80);
- Servo doorServo;
- // ----------------------------------------------------
- // LOCK STATE MACHINE
- // ----------------------------------------------------
- enum LockState {
- IDLE_LOCKED,
- IDLE_UNLOCKED,
- LOCKING,
- UNLOCKING
- };
- LockState lockState;
- unsigned long stateStartTime = 0;
- // ----------------------------------------------------
- // BUTTON DEBOUNCE
- // ----------------------------------------------------
- bool lastReading = HIGH; // remembers last raw button reading
- bool stableState = HIGH; // remembers debounced button state
- unsigned long lastDebounceTime = 0;
- const unsigned long debounceDelay = 35;
- // ----------------------------------------------------
- // BUZZER STATE MACHINE (NON-BLOCKING)
- // ----------------------------------------------------
- enum BuzzerState {
- BUZZER_IDLE,
- BUZZER_ON,
- BUZZER_OFF_PAUSE
- };
- BuzzerState buzzerState = BUZZER_IDLE;
- unsigned long buzzerTimer = 0; // remembers last buzzer state change
- int beepsRemaining = 0; // how many beeps still need to play
- const unsigned long BEEP_ON_TIME = 40; // ms buzzer ON time
- const unsigned long BEEP_OFF_TIME = 80; // ms pause between beeps
- // ----------------------------------------------------
- // STATE TRANSITION HELPER
- // ----------------------------------------------------
- void setState(LockState newState) {
- lockState = newState;
- stateStartTime = millis(); // remember when this state started
- }
- // ----------------------------------------------------
- // BUZZER REQUEST (NON-BLOCKING)
- // ----------------------------------------------------
- void requestBeeps(int count) {
- beepsRemaining = count; // remember how many beeps are requested
- buzzerState = BUZZER_ON; // start with buzzer ON
- buzzerTimer = millis(); // remember when buzzer turned ON
- digitalWrite(BUZZER_PIN, HIGH);
- }
- // ----------------------------------------------------
- // BUZZER STATE MACHINE UPDATE
- // ----------------------------------------------------
- void updateBuzzer() {
- unsigned long now = millis();
- switch (buzzerState) {
- case BUZZER_IDLE:
- // Nothing to do
- break;
- case BUZZER_ON:
- // Turn buzzer OFF after ON duration expires
- if (now - buzzerTimer >= BEEP_ON_TIME) {
- digitalWrite(BUZZER_PIN, LOW);
- buzzerTimer = now; // remember OFF time
- buzzerState = BUZZER_OFF_PAUSE;
- }
- break;
- case BUZZER_OFF_PAUSE:
- // Wait between beeps
- if (now - buzzerTimer >= BEEP_OFF_TIME) {
- beepsRemaining--; // one beep completed
- if (beepsRemaining > 0) {
- digitalWrite(BUZZER_PIN, HIGH); // start next beep
- buzzerTimer = now;
- buzzerState = BUZZER_ON;
- } else {
- buzzerState = BUZZER_IDLE; // all beeps done
- }
- }
- break;
- }
- }
- // ----------------------------------------------------
- // WEB PAGE (UNCHANGED)
- // ----------------------------------------------------
- void handleRoot() {
- bool isMoving = (lockState == LOCKING || lockState == UNLOCKING);
- String page =
- "<!DOCTYPE html><html><head>"
- "<meta name='viewport' content='width=device-width, initial-scale=1'>"
- "<title>John's Garage Man Door Lock/Unlock</title>";
- page += "<meta http-equiv='refresh' content='1'>";
- page +=
- "<style>"
- "body{font-family:Arial;background:#111;color:white;text-align:center;"
- "margin:0;padding-top:30px;}"
- "h1{font-size:28px;margin-bottom:10px;}"
- ".status{font-size:26px;margin-bottom:30px;}"
- ".btn{display:block;width:80%;max-width:320px;margin:20px auto;"
- "padding:25px;font-size:26px;border:none;border-radius:14px;color:white}"
- ".lock{background:#c62828}"
- ".unlock{background:#2e7d32}"
- ".disabled{background:#555!important}"
- "</style></head><body>";
- page += "<h1>John's Garage Man Door</h1>";
- page += "<div class='status'>Status: <b>";
- if (lockState == IDLE_LOCKED)
- page += "<span style='color:#ff5252'>LOCKED</span>";
- else if (lockState == IDLE_UNLOCKED)
- page += "<span style='color:#69f0ae'>UNLOCKED</span>";
- else if (lockState == LOCKING)
- page += "<span style='color:#ffb74d'>LOCKING</span>";
- else
- page += "<span style='color:#81d4fa'>UNLOCKING</span>";
- page += "</b></div>";
- page += "<form action='/lock'><button class='btn lock";
- if (isMoving) page += " disabled' disabled>LOCK</button></form>";
- else page += "'>LOCK</button></form>";
- page += "<form action='/unlock'><button class='btn unlock";
- if (isMoving) page += " disabled' disabled>UNLOCK</button></form>";
- else page += "'>UNLOCK</button></form>";
- page += "</body></html>";
- server.send(200, "text/html", page);
- }
- // ----------------------------------------------------
- // WEB HANDLERS
- // ----------------------------------------------------
- void handleLock() {
- if (lockState == IDLE_UNLOCKED) {
- setState(LOCKING);
- }
- server.sendHeader("Location", "/");
- server.send(303);
- }
- void handleUnlock() {
- if (lockState == IDLE_LOCKED) {
- setState(UNLOCKING);
- }
- server.sendHeader("Location", "/");
- server.send(303);
- }
- // ----------------------------------------------------
- // SETUP
- // ----------------------------------------------------
- void setup() {
- pinMode(BUTTON_PIN, INPUT_PULLUP);
- pinMode(RED_LED_LOCKED, OUTPUT);
- pinMode(GREEN_LED_UNLOCKED, OUTPUT);
- pinMode(BUZZER_PIN, OUTPUT);
- digitalWrite(RED_LED_LOCKED, LOW);
- digitalWrite(GREEN_LED_UNLOCKED, HIGH);
- digitalWrite(BUZZER_PIN, LOW);
- doorServo.attach(SERVO_PIN);
- setState(IDLE_UNLOCKED); // startup unlocked, silent
- WiFi.config(staticIP, gateway, subnet, dns1);
- WiFi.begin(ssid, password);
- server.on("/", handleRoot);
- server.on("/lock", handleLock);
- server.on("/unlock", handleUnlock);
- server.begin();
- }
- // ----------------------------------------------------
- // LOOP
- // ----------------------------------------------------
- void loop() {
- server.handleClient();
- updateBuzzer(); // service buzzer state machine (non-blocking)
- bool reading = digitalRead(BUTTON_PIN);
- unsigned long now = millis();
- // Button debounce logic
- if (reading != lastReading) {
- lastDebounceTime = now; // remember last time input changed
- }
- if ((now - lastDebounceTime) > debounceDelay) {
- if (reading != stableState) {
- stableState = reading;
- if (stableState == LOW) { // button pressed
- if (lockState == IDLE_UNLOCKED) setState(LOCKING);
- else if (lockState == IDLE_LOCKED) setState(UNLOCKING);
- }
- }
- }
- lastReading = reading;
- // Lock state machine
- switch (lockState) {
- case IDLE_UNLOCKED:
- digitalWrite(RED_LED_LOCKED, LOW);
- digitalWrite(GREEN_LED_UNLOCKED, HIGH);
- break;
- case IDLE_LOCKED:
- digitalWrite(RED_LED_LOCKED, HIGH);
- digitalWrite(GREEN_LED_UNLOCKED, LOW);
- break;
- case LOCKING:
- digitalWrite(RED_LED_LOCKED, HIGH);
- digitalWrite(GREEN_LED_UNLOCKED, LOW);
- doorServo.write(LOCK_POS);
- if (now - stateStartTime >= MOVE_TIME) {
- requestBeeps(2); // LOCKED = 2 beeps
- setState(IDLE_LOCKED);
- }
- break;
- case UNLOCKING:
- digitalWrite(RED_LED_LOCKED, LOW);
- digitalWrite(GREEN_LED_UNLOCKED, HIGH);
- doorServo.write(UNLOCK_POS);
- if (now - stateStartTime >= MOVE_TIME) {
- requestBeeps(1); // UNLOCKED = 1 beep
- setState(IDLE_UNLOCKED);
- }
- break;
- }
- }
Advertisement