stspringer

ESP8266 Update Remote Garage Man Door Lock/Unlock

Jan 11th, 2026 (edited)
170
0
Never
9
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.51 KB | Source Code | 0 0
  1. 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.
  2.  
  3. 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.
  4.  
  5. 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.
  6.  
  7. 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.
  8.  
  9. 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.
  10.  
  11. 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.
  12.  
  13. 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.
  14.  
  15. HTH
  16.  
  17. #include <ESP8266WiFi.h>
  18. #include <ESP8266WebServer.h>
  19. #include <Servo.h>
  20.  
  21. /*
  22. John's Garage Man Door Lock / Unlock
  23. -----------------------------------
  24. • Momentary pushbutton toggles lock
  25. • Servo-driven deadbolt
  26. • Web UI lock / unlock
  27. • Red / Green status LEDs
  28. • Buzzer chirps:
  29. - 1 beep = UNLOCKED
  30. - 2 beeps = LOCKED
  31. • millis()-based servo timing
  32. • Explicit state machines (lock + buzzer)
  33. */
  34.  
  35. // ----------------------------------------------------
  36. // PIN SETUP
  37. // ----------------------------------------------------
  38. const int SERVO_PIN = 2; // D4 (GPIO2)
  39. const int BUTTON_PIN = 14; // D5 (GPIO14) momentary button → GND
  40. const int RED_LED_LOCKED = 5; // D1 (GPIO5) RED LED
  41. const int GREEN_LED_UNLOCKED = 13; // D7 (GPIO13) GREEN LED
  42. const int BUZZER_PIN = 15; // D8 (GPIO15)
  43.  
  44. // ----------------------------------------------------
  45. // SERVO POSITIONS (PHYSICALLY VERIFIED)
  46. // ----------------------------------------------------
  47. const int LOCK_POS = 90; // Physically LOCKS deadbolt
  48. const int UNLOCK_POS = 0; // Physically UNLOCKS deadbolt
  49. const unsigned long MOVE_TIME = 1200; // ms servo travel time
  50.  
  51. // ----------------------------------------------------
  52. // WIFI CONFIG
  53. // ----------------------------------------------------
  54. const char* ssid = "xxxxxxxx";
  55. const char* password = "xxxxxxxx";
  56.  
  57. IPAddress staticIP(xxx,xxx,xxx,xxx);
  58. IPAddress gateway (xxx,xxx,xxx,xxx);
  59. IPAddress subnet (xxx,xxx,xxx,xxx);
  60. IPAddress dns1 (xxx,xxx,xxx,xxx);
  61.  
  62. ESP8266WebServer server(80);
  63. Servo doorServo;
  64.  
  65. // ----------------------------------------------------
  66. // LOCK STATE MACHINE
  67. // ----------------------------------------------------
  68. enum LockState {
  69. IDLE_LOCKED,
  70. IDLE_UNLOCKED,
  71. LOCKING,
  72. UNLOCKING
  73. };
  74.  
  75. LockState lockState;
  76. unsigned long stateStartTime = 0;
  77.  
  78. // ----------------------------------------------------
  79. // BUTTON DEBOUNCE
  80. // ----------------------------------------------------
  81. bool lastReading = HIGH; // remembers last raw button reading
  82. bool stableState = HIGH; // remembers debounced button state
  83. unsigned long lastDebounceTime = 0;
  84. const unsigned long debounceDelay = 35;
  85.  
  86. // ----------------------------------------------------
  87. // BUZZER STATE MACHINE (NON-BLOCKING)
  88. // ----------------------------------------------------
  89. enum BuzzerState {
  90. BUZZER_IDLE,
  91. BUZZER_ON,
  92. BUZZER_OFF_PAUSE
  93. };
  94.  
  95. BuzzerState buzzerState = BUZZER_IDLE;
  96.  
  97. unsigned long buzzerTimer = 0; // remembers last buzzer state change
  98. int beepsRemaining = 0; // how many beeps still need to play
  99.  
  100. const unsigned long BEEP_ON_TIME = 40; // ms buzzer ON time
  101. const unsigned long BEEP_OFF_TIME = 80; // ms pause between beeps
  102.  
  103. // ----------------------------------------------------
  104. // STATE TRANSITION HELPER
  105. // ----------------------------------------------------
  106. void setState(LockState newState) {
  107. lockState = newState;
  108. stateStartTime = millis(); // remember when this state started
  109. }
  110.  
  111. // ----------------------------------------------------
  112. // BUZZER REQUEST (NON-BLOCKING)
  113. // ----------------------------------------------------
  114. void requestBeeps(int count) {
  115. beepsRemaining = count; // remember how many beeps are requested
  116. buzzerState = BUZZER_ON; // start with buzzer ON
  117. buzzerTimer = millis(); // remember when buzzer turned ON
  118. digitalWrite(BUZZER_PIN, HIGH);
  119. }
  120.  
  121. // ----------------------------------------------------
  122. // BUZZER STATE MACHINE UPDATE
  123. // ----------------------------------------------------
  124. void updateBuzzer() {
  125. unsigned long now = millis();
  126.  
  127. switch (buzzerState) {
  128.  
  129. case BUZZER_IDLE:
  130. // Nothing to do
  131. break;
  132.  
  133. case BUZZER_ON:
  134. // Turn buzzer OFF after ON duration expires
  135. if (now - buzzerTimer >= BEEP_ON_TIME) {
  136. digitalWrite(BUZZER_PIN, LOW);
  137. buzzerTimer = now; // remember OFF time
  138. buzzerState = BUZZER_OFF_PAUSE;
  139. }
  140. break;
  141.  
  142. case BUZZER_OFF_PAUSE:
  143. // Wait between beeps
  144. if (now - buzzerTimer >= BEEP_OFF_TIME) {
  145. beepsRemaining--; // one beep completed
  146.  
  147. if (beepsRemaining > 0) {
  148. digitalWrite(BUZZER_PIN, HIGH); // start next beep
  149. buzzerTimer = now;
  150. buzzerState = BUZZER_ON;
  151. } else {
  152. buzzerState = BUZZER_IDLE; // all beeps done
  153. }
  154. }
  155. break;
  156. }
  157. }
  158.  
  159. // ----------------------------------------------------
  160. // WEB PAGE (UNCHANGED)
  161. // ----------------------------------------------------
  162. void handleRoot() {
  163.  
  164. bool isMoving = (lockState == LOCKING || lockState == UNLOCKING);
  165.  
  166. String page =
  167. "<!DOCTYPE html><html><head>"
  168. "<meta name='viewport' content='width=device-width, initial-scale=1'>"
  169. "<title>John's Garage Man Door Lock/Unlock</title>";
  170.  
  171. page += "<meta http-equiv='refresh' content='1'>";
  172.  
  173. page +=
  174. "<style>"
  175. "body{font-family:Arial;background:#111;color:white;text-align:center;"
  176. "margin:0;padding-top:30px;}"
  177. "h1{font-size:28px;margin-bottom:10px;}"
  178. ".status{font-size:26px;margin-bottom:30px;}"
  179. ".btn{display:block;width:80%;max-width:320px;margin:20px auto;"
  180. "padding:25px;font-size:26px;border:none;border-radius:14px;color:white}"
  181. ".lock{background:#c62828}"
  182. ".unlock{background:#2e7d32}"
  183. ".disabled{background:#555!important}"
  184. "</style></head><body>";
  185.  
  186. page += "<h1>John's Garage Man Door</h1>";
  187. page += "<div class='status'>Status: <b>";
  188.  
  189. if (lockState == IDLE_LOCKED)
  190. page += "<span style='color:#ff5252'>LOCKED</span>";
  191. else if (lockState == IDLE_UNLOCKED)
  192. page += "<span style='color:#69f0ae'>UNLOCKED</span>";
  193. else if (lockState == LOCKING)
  194. page += "<span style='color:#ffb74d'>LOCKING</span>";
  195. else
  196. page += "<span style='color:#81d4fa'>UNLOCKING</span>";
  197.  
  198. page += "</b></div>";
  199.  
  200. page += "<form action='/lock'><button class='btn lock";
  201. if (isMoving) page += " disabled' disabled>LOCK</button></form>";
  202. else page += "'>LOCK</button></form>";
  203.  
  204. page += "<form action='/unlock'><button class='btn unlock";
  205. if (isMoving) page += " disabled' disabled>UNLOCK</button></form>";
  206. else page += "'>UNLOCK</button></form>";
  207.  
  208. page += "</body></html>";
  209.  
  210. server.send(200, "text/html", page);
  211. }
  212.  
  213. // ----------------------------------------------------
  214. // WEB HANDLERS
  215. // ----------------------------------------------------
  216. void handleLock() {
  217. if (lockState == IDLE_UNLOCKED) {
  218. setState(LOCKING);
  219. }
  220. server.sendHeader("Location", "/");
  221. server.send(303);
  222. }
  223.  
  224. void handleUnlock() {
  225. if (lockState == IDLE_LOCKED) {
  226. setState(UNLOCKING);
  227. }
  228. server.sendHeader("Location", "/");
  229. server.send(303);
  230. }
  231.  
  232. // ----------------------------------------------------
  233. // SETUP
  234. // ----------------------------------------------------
  235. void setup() {
  236.  
  237. pinMode(BUTTON_PIN, INPUT_PULLUP);
  238. pinMode(RED_LED_LOCKED, OUTPUT);
  239. pinMode(GREEN_LED_UNLOCKED, OUTPUT);
  240. pinMode(BUZZER_PIN, OUTPUT);
  241.  
  242. digitalWrite(RED_LED_LOCKED, LOW);
  243. digitalWrite(GREEN_LED_UNLOCKED, HIGH);
  244. digitalWrite(BUZZER_PIN, LOW);
  245.  
  246. doorServo.attach(SERVO_PIN);
  247.  
  248. setState(IDLE_UNLOCKED); // startup unlocked, silent
  249.  
  250. WiFi.config(staticIP, gateway, subnet, dns1);
  251. WiFi.begin(ssid, password);
  252.  
  253. server.on("/", handleRoot);
  254. server.on("/lock", handleLock);
  255. server.on("/unlock", handleUnlock);
  256. server.begin();
  257. }
  258.  
  259. // ----------------------------------------------------
  260. // LOOP
  261. // ----------------------------------------------------
  262. void loop() {
  263.  
  264. server.handleClient();
  265. updateBuzzer(); // service buzzer state machine (non-blocking)
  266.  
  267. bool reading = digitalRead(BUTTON_PIN);
  268. unsigned long now = millis();
  269.  
  270. // Button debounce logic
  271. if (reading != lastReading) {
  272. lastDebounceTime = now; // remember last time input changed
  273. }
  274.  
  275. if ((now - lastDebounceTime) > debounceDelay) {
  276. if (reading != stableState) {
  277. stableState = reading;
  278.  
  279. if (stableState == LOW) { // button pressed
  280. if (lockState == IDLE_UNLOCKED) setState(LOCKING);
  281. else if (lockState == IDLE_LOCKED) setState(UNLOCKING);
  282. }
  283. }
  284. }
  285.  
  286. lastReading = reading;
  287.  
  288. // Lock state machine
  289. switch (lockState) {
  290.  
  291. case IDLE_UNLOCKED:
  292. digitalWrite(RED_LED_LOCKED, LOW);
  293. digitalWrite(GREEN_LED_UNLOCKED, HIGH);
  294. break;
  295.  
  296. case IDLE_LOCKED:
  297. digitalWrite(RED_LED_LOCKED, HIGH);
  298. digitalWrite(GREEN_LED_UNLOCKED, LOW);
  299. break;
  300.  
  301. case LOCKING:
  302. digitalWrite(RED_LED_LOCKED, HIGH);
  303. digitalWrite(GREEN_LED_UNLOCKED, LOW);
  304. doorServo.write(LOCK_POS);
  305.  
  306. if (now - stateStartTime >= MOVE_TIME) {
  307. requestBeeps(2); // LOCKED = 2 beeps
  308. setState(IDLE_LOCKED);
  309. }
  310. break;
  311.  
  312. case UNLOCKING:
  313. digitalWrite(RED_LED_LOCKED, LOW);
  314. digitalWrite(GREEN_LED_UNLOCKED, HIGH);
  315. doorServo.write(UNLOCK_POS);
  316.  
  317. if (now - stateStartTime >= MOVE_TIME) {
  318. requestBeeps(1); // UNLOCKED = 1 beep
  319. setState(IDLE_UNLOCKED);
  320. }
  321. break;
  322. }
  323. }
  324.  
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment