{"id":145783,"date":"2024-08-09T14:57:25","date_gmt":"2024-08-09T09:27:25","guid":{"rendered":"https:\/\/www.electronicsforu.com\/?p=145783"},"modified":"2025-03-18T12:51:43","modified_gmt":"2025-03-18T07:21:43","slug":"digital-lock-using-web-serial","status":"publish","type":"post","link":"https:\/\/www.electronicsforu.com\/electronics-projects\/digital-lock-using-web-serial","title":{"rendered":"Digital Lock Using Web Serial"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"alignright size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"152\" src=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2016\/12\/Screen-Shot-2016-12-24-at-15.19.33-e1644389186872.png\" alt=\"\" class=\"wp-image-28226\"\/><figcaption class=\"wp-element-caption\">Comment errors or corrections found for this circuit, and get the chance to win big!<\/figcaption><\/figure><\/div>\n\n\n<p>A digital lock using WebSerial leverages modern microcontroller technology to create a secure and convenient access control system. <\/p>\n\n\n\n<p>This setup typically involves an <a href=\"https:\/\/www.electronicsforu.com\/technology-trends\/esp32\">ESP32 microcontroller<\/a>, which hosts a web interface accessible via a local Wi-Fi network. Users can connect to this network and enter a pre-set password through the WebSerial interface. <\/p>\n\n\n\n<p>The microcontroller processes the input and controls a relay that activates the lock mechanism. If the correct password is entered, the relay is triggered, unlocking the door for a specified period before automatically re-locking. <\/p>\n\n\n\n<p>This system enhances security by allowing real-time, remote access control and monitoring through a simple web interface, making it an innovative solution for modern access control needs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Applications of the Digital Lock using Webserial<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Enhances the security of residential properties by providing remote access control and real-time monitoring of entry points.<\/li>\n\n\n\n<li>Used in office buildings to manage employee access, ensuring that only authorized personnel can enter certain areas.<\/li>\n\n\n\n<li>Used in smart locker systems for parcel delivery and storage, enabling users to securely access their parcels using a web interface.<\/li>\n\n\n\n<li>Allows hotel staff to remotely control and monitor room access, improving guest security and operational efficiency and many more.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Bill of Materials (BoM)<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Components<\/strong><\/td><td><strong>Description<\/strong><\/td><td><strong>Quantity<\/strong><\/td><\/tr><tr><td><a href=\"https:\/\/shop.electronicsforu.com\/product\/indusboard-iot-development-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">IndusBoard<\/a><\/td><td>3cm sized dev board<\/td><td>1<\/td><\/tr><tr><td>LED<\/td><td>5mm LED<\/td><td>1<\/td><\/tr><tr><td>Breadboard<\/td><td>3.5 cm x 4.5 cm breadboard<\/td><td>1<\/td><\/tr><tr><td>Wires<\/td><td>Jumper wires<\/td><td>4<\/td><\/tr><tr><td>Battery<\/td><td>3V Battery<\/td><td>1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Code for Digital Lock Using Web Serial<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;Arduino.h&gt;\n#if defined(ESP8266)\n  #include &lt;ESP8266WiFi.h&gt;\n  #include &lt;ESPAsyncTCP.h&gt;\n#elif defined(ESP32)\n  #include &lt;WiFi.h&gt;\n  #include &lt;AsyncTCP.h&gt;\n#endif\n#include &lt;ESPAsyncWebServer.h&gt;\n#include &lt;WebSerial.h&gt;\n\nAsyncWebServer server(80);\n\nconst char* ssid = \"ESP Wifi\"; \/\/ Your WiFi AP SSID\nconst char* password = \"12345678\"; \/\/ Your WiFi Password\n\n\/\/ Password\nString presetPassword = \"1234\"; \/\/ Pre-set password\nString inputPassword = \"\"; \/\/ User input password\n\nbool accessGranted = false;\nunsigned long unlockTime = 0;\nconst unsigned long unlockDuration = 5000; \/\/ 5 seconds\n\n\/* Message callback of WebSerial *\/\nvoid recvMsg(uint8_t *data, size_t len){\n  WebSerial.println(\"Received Data...\");\n  String d = \"\";\n  for(int i=0; i &lt; len; i++){\n    d += char(data&#91;i]);\n  }\n  WebSerial.println(d);\n\n  \/\/ Append the received data to the input password\n  inputPassword += d;\n\n  \/\/ If 4 digits have been entered\n  if (inputPassword.length() == 4) {\n    \/\/ Check if the input password matches the pre-set password\n    if (inputPassword == presetPassword) {\n      WebSerial.println(\"Access Granted\");\n      \/\/ Unlock the door (activate the relay)\n      digitalWrite(5, HIGH);\n      accessGranted = true;\n      unlockTime = millis();\n    } else {\n      WebSerial.println(\"Access Denied\");\n    }\n\n    \/\/ Clear the input password for the next attempt\n    inputPassword = \"\";\n\n    \/\/ digitalWrite(5, LOW);\n\n    \/\/ Prompt the user to enter the password again\n    WebSerial.println(\"Enter the 4-digit password:\");\n  }\n}\n\nvoid setup() {\n  Serial.begin(115200);\n\n\n  \/\/ Set relay pin as output\n  pinMode(5, OUTPUT);  \/\/ Use GPIO 5 for example, change if needed\n \n  \/\/ Initially turn off the relay (lock the door)\n  digitalWrite(5, LOW);\n\n  WiFi.softAP(ssid, password);\n\n  IPAddress IP = WiFi.softAPIP();\n  Serial.print(\"AP IP address: \");\n  Serial.println(IP);\n\n  \/\/ WebSerial is accessible at \"&lt;IP Address&gt;\/webserial\" in browser\n  WebSerial.begin(&amp;server);\n  \/* Attach Message Callback *\/\n  WebSerial.msgCallback(recvMsg);\n  server.begin();\n\n  \/\/ Prompt the user to enter the password\n  WebSerial.println(\"Enter the 4-digit password using the web interface:\");\n}\n\nvoid loop() {\n   if (accessGranted &amp;&amp; (millis() - unlockTime &gt;= unlockDuration)) {\n    \/\/ Turn off the relay (lock the door) after 5 seconds\n    digitalWrite(5, LOW);\n    accessGranted = false;\n   }\n  \/\/ Do nothing here as WebSerial handles the incoming data\n  WebSerial.print(F(\"IP address: \"));\n    WebSerial.println(WiFi.localIP());\n    WebSerial.printf(\"Millis=%lu\\n\", millis());\n    WebSerial.printf(\"Free heap=&#91;%u]\\n\", ESP.getFreeHeap());\n    delay(1000);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Connection<\/strong><\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"691\" height=\"544\" src=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/u1-LED-connection.png\" alt=\"Digital Lock Circuit\" class=\"wp-image-145789\" style=\"width:438px;height:auto\" srcset=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/u1-LED-connection.png 691w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/u1-LED-connection-500x394.png 500w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/u1-LED-connection-533x420.png 533w\" sizes=\"auto, (max-width: 691px) 100vw, 691px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Testing<\/strong><\/h2>\n\n\n\n<p>Now we connect the board with the USB and upload the code in the indus board and check output after giving command on web serial. To connect indus board with web serial type (192.168.4.1\/webserial) in web browser. Then we can give command 1234 which is password for the access. If the password is correct, then it shows \u201cAccess Granted\u201d and open the door also LED starts glowing. Then if the password is incorrect, it shows \u201cAccess Denied\u201d and the door will not open.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"775\" src=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-1-1-1024x775.jpg\" alt=\"Indusboard Project\" class=\"wp-image-145790\" style=\"width:335px;height:auto\" srcset=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-1-1-1024x775.jpg 1024w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-1-1-500x379.jpg 500w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-1-1-555x420.jpg 555w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-1-1-80x60.jpg 80w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-1-1-696x527.jpg 696w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-1-1.jpg 1280w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"754\" height=\"1024\" src=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-2-1-754x1024.jpg\" alt=\"Digital Lock Using Web Serial\" class=\"wp-image-145791\" style=\"width:346px;height:auto\" srcset=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-2-1-754x1024.jpg 754w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-2-1-368x500.jpg 368w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-2-1-309x420.jpg 309w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-2-1-696x946.jpg 696w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-2-1.jpg 942w\" sizes=\"auto, (max-width: 754px) 100vw, 754px\" \/><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"866\" src=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-3-1-1024x866.jpg\" alt=\"Digital Lock Project\" class=\"wp-image-145792\" style=\"width:359px;height:auto\" srcset=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-3-1-1024x866.jpg 1024w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-3-1-500x423.jpg 500w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-3-1-496x420.jpg 496w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-3-1-696x589.jpg 696w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-3-1.jpg 1280w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"740\" height=\"1024\" src=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-4-1-740x1024.jpg\" alt=\"Web Serial based Digital Lock\" class=\"wp-image-145793\" style=\"width:366px;height:auto\" srcset=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-4-1-740x1024.jpg 740w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-4-1-361x500.jpg 361w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-4-1-304x420.jpg 304w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-4-1-696x963.jpg 696w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/08\/img-4-1.jpg 925w\" sizes=\"auto, (max-width: 740px) 100vw, 740px\" \/><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>Author(s): Manjeet Vishwakarma, &nbsp;Abhay Verma and Satywanti Kundu are B.Tech ECE students at GJUS&amp;T HISAR<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A digital lock using WebSerial leverages modern microcontroller technology to create a secure and convenient access control system. This setup typically involves an ESP32 microcontroller, which hosts a web interface accessible via a local Wi-Fi network. Users can connect to this network and enter a pre-set password through the WebSerial interface. The microcontroller processes the [&hellip;]<\/p>\n","protected":false},"author":55758,"featured_media":146219,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,23,776,11851],"tags":[1985,1911,110],"class_list":{"0":"post-145783","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-electronics-projects","8":"category-hardware-diy","9":"category-prototypes","10":"category-super-innovative-projects","11":"tag-diy-projects","12":"tag-do-it-yourself","13":"tag-electronics-projects"},"_links":{"self":[{"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/posts\/145783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/users\/55758"}],"replies":[{"embeddable":true,"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/comments?post=145783"}],"version-history":[{"count":2,"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/posts\/145783\/revisions"}],"predecessor-version":[{"id":159651,"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/posts\/145783\/revisions\/159651"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/media\/146219"}],"wp:attachment":[{"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/media?parent=145783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/categories?post=145783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/tags?post=145783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}