Contents

Smart Door Lock w/ WiFi Login Page by Arduino & ESP8266

Overview

In this tutorial, you will learn how to create an Arduino based smart door lock, with the ability to be unlocked by your cell phone.

What you will learn

Secure Doors by Smart Lock

These days, You can improve your home or office security by adding technology. Using smartphones and devices become very popular for people and it’s a good option for us to make our stuffs smarter. Every day, Engineers and technicians produce new systems and tools that you can make smarter home automation by using them and create new ways to make your life safer and easier. Probably you have seen smart locks for doors that have a keypad or use a fingerprint to set the door lock or unlock.

In this project, We want to make an Arduino based system that helps you to make a login section for a door by wifi and solenoid lock. We don’t use keypad or complicated mechanical elements and these are benefits of this system. We only stick a QR code on the door and the allowed people can scan it to see the login page and enter their password. After typing the password, the solenoid lock will be activated. We only use an Arduino board and a driver for the solenoid and an ESP8266 to make a connection to local wifi. Let’s do it.

Required Materials

Hardware Components

Arduino UNO R3 × 1
ESP8266 WiFi Module with PCB Antenna × 1
TIP120 Power Darlington Transistors × 1
Lock-style Solenoid × 1

Software Apps

Arduino IDE

Circuit

Code

First we write a code for ESP-01 to make a log-in page and receive password from user then send it to Arduino by the Serial port. Then we write another code for Arduino to get data from ESP-01 and control the servo motor. You can use Arduino IDE to compile both codes and Upload them to boards.

You must add the library and then upload the code. If it is the first time you run an Arduino board, don’t worry. Just follow these steps:

  • Go to www.arduino.cc/en/Main/Software and download the software of your OS. Install the IDE software as instructed.
  • Run the Arduino IDE and clear the text editor and copy the following code in the text editor.
  • Choose the board in tools and boards, select your Arduino Board.
  • Connect the Arduino to your PC and set the COM port in tools and port.
  • Press the Upload (Arrow sign) button.
  • You are all set!
String inputString = "";         // a String to hold incoming data
bool stringComplete = false;  // whether the string is complete

void setup() {
  // initialize serial:
  Serial.begin(115200);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);

  pinMode(9,OUTPUT);
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    if (inputString=="your_password")
   {
       digitalWrite(9,HIGH);
       delay(300);
       digitalWrite(9,LOW);

       Serial.println(inputString);
       // clear the string:
       inputString = "";
      stringComplete = false;
    }
  }
}


void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

Now its time to upload the ESP-01 code. You should use the Arduino IDE to upload the sketch to ESP. Before uploading the code, you should select ESP board for IDE.

Go to File>Preferences and add http://arduino.esp8266.com/stable/package_esp8266com_index.json in the additional boards. Then download and install it. Now you can see the ESP boards in Tools>Board. Select “Generic ESP8266 Module” and copy the code in a new sketch. Then you should set USB to TTL Converter as Uploader hardware. Just plug the converter in and set the right port in Tools>Port. It’s ready to Upload.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
// WiFi network
const char* ssid     = "YourSSID";
const char* password = "YourPASSWORD";
ESP8266WebServer server ( 80 );
char htmlResponse[3000];
void handleRoot() {
 snprintf ( htmlResponse, 3000,
"<!DOCTYPE html>\
<html lang=\"en\">\
 <head>\
 <style>\
body {background-color: rgb(160, 0, 53);}\
h3   {color: white;text-align:center;}\
p    {color: white; text-align:center;}\
div  {color: white; text-align:center;}\
ID {text-align:center;}\
input {text-align:center;}\
</style>\
   <meta charset=\"utf-8\">\
   <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\
 </head>\
 <body>\
         <h3>\<canter>Electropeak Smart Security Door</canter>\</h3>\
         <p>\<canter>Please type your ID</canter>\</p>\
         <div>ID: <input type='text' name='pass_word' id='pass_word' align='center' size=10 autofocus></div> \
         <div>\
         <br><button id=\"save_button\">Log In</button>\
         </div>\
   <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js\"></script>\    
   <script>\
     var pass;\
     $('#save_button').click(function(e){\
       e.preventDefault();\
       pass = $('#pass_word').val();\        
       $.get('/save?pass=' + pass, function(data){\
         console.log(data);\
       });\
     });\      
   </script>\
 </body>\
</html>"); 
  server.send ( 200, "text/html", htmlResponse );  
}
void handleSave() {
 if (server.arg("pass")!= ""){
   Serial.println(server.arg("pass"));
 }
}
void setup() {
 // Start serial
 Serial.begin(115200);
 delay(10);
 // Connecting to a WiFi network
 Serial.println();
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   Serial.print(".");
 }
 Serial.println("");
 Serial.println("WiFi connected");  
 Serial.println("IP address: ");
 Serial.println(WiFi.localIP());
 server.on ( "/", handleRoot );
 server.on ("/save", handleSave);
 server.begin();
 Serial.println ( "HTTP server started" );
}
void loop() {
 server.handleClient();
}

After uploading the code, Open Serial monitor of Arduino IDE and get your IP. Now if you type the IP in a browser URL bar, You can see UI of the web server. Type your ID in the text box and if you do it correctly, Arduino will activate the lock. Notice that you have to connect to a common wifi router with ESP8266.

Now it’s time to make a QR code and make a simple way for users to reach this webpage. There are a lot of online tools to generate a specific QR code for you. We suggest this one.

Just copy your IP in the bar and click on “Create QR Code”.

Print it and Stick it near the door. To unlock the door, you have to connect to the wifi, scan the QR code and type your ID.

What’s Next?

Here are a few suggestions:

  • Try to make a professional UI for the login page.
  • Try to add more options for users such as different permission levels or time-based permission.
Liked What You See?​
Get Updates And Learn From The Best​

Comments (50)

  • Nish. A. Reply

    Hello there, thank you for the great explanation.
    I have 1 question; once we have successfully unlocked the door, does the door will be locked automatically once it is closed?
    Thank you.

    April 16, 2019 at 11:58 pm
    • Saeed Hosseini Reply

      Hi, yes it does.

      April 18, 2019 at 8:51 am
  • Roni Frans Reply

    Hi there, I’m a student from Indonesia. I’m interesting to modify your project.
    In my modification, I want to use NodeMCU ESP8266 only. And without barcode.
    So solenoid is controled by apps in smartphone if only connect to internet wherever it is, without password.
    It’s like on/off LED. So it’s just simple than yours. But I don’t know how to set the schematic on NodeMCU ESP 8266.
    So i’m very glad if you can help me.

    May 11, 2019 at 7:43 pm
    • Saeed Hosseini Reply

      Hi
      just connect the solenoid to one of the NodeMCU GPIO(for example D2).
      That’s it.

      May 12, 2019 at 11:15 am
      • Roni Frans Reply

        but i use NodeMCU ESP8266 not ESP-1 like yours? is it working the same?
        And how to connect it to the app?

        May 13, 2019 at 9:24 am
        • Saeed Hosseini Reply

          Yes, it is.
          Do you use the Blynk app?

          May 13, 2019 at 11:16 am
  • Denis Reply

    Hello there I really like this project and it will really fit my needs perfectly.

    However i am not able to replicate it fully …. i got your code uploaded into the Arduino Uno R3 … in the field where we have to enter our individual password i did that (as a matter of fact i tried it all letters only and than numbers only or a combination because of the inputString variable)
    I got the code uploaded on the ESP-01 ….i entered the log in and the password for my wifi ….the ESP signs right on onto my network and i am able to access the webpage just fine where we enter the password. However after entering the password and clicking Log In there is no relay clicking!!!

    I have connected a simple 5v relay to pin 9 of the Arduino and connected the Tx of the Uno to Rx of the ESP and the Tx of the ESP to Rx of the Uno ( i do not understand why you have the GPIO2 of the ESP connected to the Rx of the Arduino on your little pic/ wiring diagram i tried that also to no avail…is that just a drawing mistake??).

    All… the little 5v relay, the ESP and the Uno itself are powered from a single 12v source through the Uno (you have forgotten to mention on the write up that the CHPD pin of the ESP has to be connected to the 3v3 line also for the ESP to boot correctly if you are using Dupont jumper wires as the majority of us are)

    On the Arduino sketch you say Serial.begin 9600 while on the ESP sketch you say Serial.begin 115200 …. aren’t they supposed to be the same so the 2 boards can communicate on the same baud rate?? (i tried both by the way and still it does not work)

    Isn’t there supposed to be a Serial.write statement on the ESP sketch ?? (i am sorry i am no Arduino expert i just have seen this before on other sketches)

    Can i please have a copy of your own configuration it will really mean a lot to me if i can get this to work in my home automation setup because we have a lot of friends and family that come and go in our house. please write me directly at…. grabocka (at) sbcglobal.net

    thank you in advance
    Denis

    ps I have tried 2 differnt Arduino Uno’s 2 different ESP-01 and 2 different relays still no success

    May 28, 2019 at 4:15 am
    • Saeed Hosseini Reply

      Hi
      Thanks for your comment, we updated this tutorial. please check it again

      May 29, 2019 at 8:57 am
  • Denis Reply

    Hey Saeed,
    Thank you for your prompt reply ….it shows that you care and i really appreciate that.
    However it still does not work i see that you have corrected the diagram for the tx/rx connections and changed the baud rate on both boards to 115200.
    Again the Esp connect to my network i am able to put in the password however that argument does not seem to be transmitted to the Arduino Uno
    I know i have the connections right and i have uploaded the appropriate sketches with the appropriate changes but it is not working!
    What am i doing wrong??

    Is this the right order of things ( and i am sorry i may be tottally off i am not an expert so forgive me please) but it seems like we are passing the argument without starting the serial port!!! or am i wrong
    void handleSave() {
    if (server.arg(“pass”)!= “”){
    Serial.println(server.arg(“pass”));
    }
    }
    void setup() {
    // Start serial
    Serial.begin(115200);
    delay(10);
    // Connecting to a WiFi network
    Serial.println();
    Serial.println();
    Serial.print(“Connecting to “);
    Serial.println(ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(“.”);
    }
    Serial.println(“”);
    Serial.println(“WiFi connected”);
    Serial.println(“IP address: “);
    Serial.println(WiFi.localIP());
    server.on ( “/”, handleRoot );
    server.on (“/save”, handleSave);
    server.begin();
    Serial.println ( “HTTP server started” );

    Please help me solve this
    Again thank you
    Denis

    May 30, 2019 at 5:54 am
    • Saeed Olfat Reply

      Hi Denis,I’m sorry for delay in answering . I checked all parts of code and run whole project again. The code is correct and I can get password in Arduino serial terminal.
      I have some suggestions for you. ESP-01 module has a very weak circuit and sometimes its hard to making run without problem. Voltage and current of this module are very important and make sure your wires are thick enough to give needed ampere to module. If you can’t replace wires, try to connect it to a higher voltage power supply like 4 volt. Another way is replacing ESP-01 with a higher version of ESP modules like NodeMCU and try this code on that. I guess your main problem is ESP and for debugging step by step, Only test the ESP and see result in serial monitor without arduino as Step 1.

      June 4, 2019 at 2:48 pm
  • Denis Reply

    Any new word on this??? Please read the above comment although i did the above changes and changed the baud rate to 115200 on both boards there is still no communication.
    The ESP-01 web-server works fine but it does not seem to send any data to the Arduino UNO or Ardunio Uno is not processing anything.
    There has got to be something wrong with the sketch codes cause there is nothing wrong with the hardware and the connections please confirm that the sketch works just he way it is on your end (obviously with your passwords etc)
    I really want to get this to work
    Any help to troubleshoot will be greatly appreciated
    thank you in advance
    Denis

    June 4, 2019 at 3:58 am
    • Saeed Hosseini Reply

      Hi again.
      in ESP code use Serial.print(password) instead of Serial.println(password), or in Arduino code add “\n” after the password (password\n)

      June 8, 2019 at 9:50 am
      • Denis Reply

        hi there and thank you for replying
        which line is the one you are referring to on the ESP code??
        I do not see any Serial.println(password)

        Thank you again
        Denis

        June 16, 2019 at 2:35 am
        • Saeed Hosseini Reply

          Dear Denis
          Check line 48. Change ” Serial.println(server.arg(“pass”));” to ” Serial.print(server.arg(“pass”));”
          Kind regards

          June 16, 2019 at 9:11 am
      • Denis Reply

        When i compile the code for the ESP without changing anything just as you have posted i get the following errors.

        :\Users\Denis Grabocka\Documents\Arduino\Sketches\esp01DoorLock\esp01DoorLock.ino:31:95: warning: backslash and newline separated by space [enabled by default]

        \
        ^

        C:\Users\Denis Grabocka\Documents\Arduino\Sketches\esp01DoorLock\esp01DoorLock.ino:36:37: warning: backslash and newline separated by space [enabled by default]

        pass = $(‘#pass_word’).val();\
        ^

        C:\Users\Denis Grabocka\Documents\Arduino\Sketches\esp01DoorLock\esp01DoorLock.ino:40:9: warning: backslash and newline separated by space [enabled by default]

        });\
        ^

        C:\Users\Denis Grabocka\Documents\Arduino\Sketches\esp01DoorLock\esp01DoorLock.ino: In function ‘void handleRoot()’:

        C:\Users\Denis Grabocka\Documents\Arduino\Sketches\esp01DoorLock\esp01DoorLock.ino:10:1: warning: unknown escape sequence: ‘<' [enabled by default]

        "\

        ^

        C:\Users\Denis Grabocka\Documents\Arduino\Sketches\esp01DoorLock\esp01DoorLock.ino:10:1: warning: unknown escape sequence: ‘<' [enabled by default]

        C:\Users\Denis Grabocka\Documents\Arduino\Sketches\esp01DoorLock\esp01DoorLock.ino:10:1: warning: unknown escape sequence: '<' [enabled by default]

        C:\Users\Denis Grabocka\Documents\Arduino\Sketches\esp01DoorLock\esp01DoorLock.ino:10:1: warning: unknown escape sequence: '<' [enabled by default]`

        Do they affect the behavior of the communication or not really
        Again i am sorry i do not know much about coding i jam good at following along but i am willing to learn and i greatly appreciate your help

        Denis

        June 16, 2019 at 2:49 am
  • Denis Reply

    It is still not working
    I can hear the relay clicking now during boot up as soon as i plug it in but not though entering the correct password (before there was absolutely nothing)
    Is there a way we can get this to work without the Arduino Uno just the ESP board ….maybe i can put it in a NodeMCU and a relay

    Denis

    June 18, 2019 at 7:15 pm
    • Saeed Hosseini Reply

      Yes, you can use Nodemcu or Wemos D1 and remove Arduino UNO. Nodemcu has enough GPIO pins for this project.

      June 19, 2019 at 4:26 am
  • leonardo keithy nakagawa Reply

    Hey , I’m doing this project but when i put the password on html, it doesn’t send the command for digital write, how is the connection for esp8266 because im wiring vcc gnd tx rx and ch-pd is it correct?

    October 13, 2020 at 6:57 pm
  • dating swim Reply

    Thanks very nice blog!

    March 17, 2021 at 11:54 am
    • Mehran Maleki Reply

      So glad you enjoyed it.

      March 22, 2021 at 3:50 pm
  • Ala'a Saqa Reply

    thanks for your work , this really helpful for me , but I have a question , how can we set USB to TTL Converter as Uploader hardware because I don’t understand this part

    July 31, 2021 at 11:23 pm
    • Mehran Maleki Reply

      You’re welcome!
      Well, as you can see, the ESP-01 board has 8 pins and no miniUSB or microUSB port. So, you can’t just connect it to a PC and program it. To do that, you need to use a USB to TTL converter. For example, this converter can be helpful: “https://electropeak.com/usb-ttl-pl2303”
      Also, you can check the following link to see how you can connect the USB to TTL converter to the ESP-01: “https://behind-the-scenes.net/connecting-the-esp8266-to-a-breadboard-and-ftdi-programmer/”

      August 1, 2021 at 6:12 am
  • Ala'a Saqa Reply

    this is really helpful thank you , can I use this USB to TTL converter with ESP8266 ?

    August 2, 2021 at 7:15 pm
    • Mehran Maleki Reply

      Yes, it’s pretty much straight-forward. The link I recommended at the end of my previous comment fully explains how you can do that. Also, this link can be so helpful: “https://www.diyhobi.com/flash-program-esp-01-using-usb-serial-adapter/”

      August 3, 2021 at 5:53 am
  • EMS Reply

    hello i have an idea what if this project can record everyone who scanned and open the door and it will tell the time and date when their unlock the door? i think it would be great!

    October 26, 2021 at 9:04 am
  • sywl Reply

    hello, I can’t get the IP address for the esp01. I have tried every solution on the internet but to no avail. can you help me with this?

    October 29, 2021 at 8:10 am
    • Mehran Maleki Reply

      Hi,
      Are you sure you have selected the right Baud rate for your Serial Monitor? What exactly do you get in the Serial Monitor?

      October 31, 2021 at 6:31 am
  • sywl Reply

    i get ……….. in the serial monitor
    I use 115200 baud rate

    December 3, 2021 at 2:41 am
    • Mehran Maleki Reply

      Well, make sure you have opened the serial monitor of the right PORT. You need to open the serial monitor of ESP32 to get the local IP.

      December 4, 2021 at 7:23 am
      • EMS Reply

        yes it works thank you. but i cant enter the ID now for some reason what do you think?

        December 20, 2021 at 4:59 pm
        • Mehran Maleki Reply

          So, you mean you can’t get the IP in the Serial Monitor? Or you get it but nothing happens when you type in the browser?

          December 21, 2021 at 6:33 am
  • EMS Reply

    i finally get the ip address but now I’m stuck on entering the ID to unlock the lock. when I try to enter the ID or password nothing happens

    December 21, 2021 at 8:08 am
    • Mehran Maleki Reply

      Well, it’s weird. You’ve already passed the complicated steps. Do you have a multimeter? You may need it to troubleshoot the issue. First, you need to make sure that the Arduino is performing well. Use a multimeter to measure the voltage of digital pin 9 of the Arduino board when you enter your ID and password. If it becomes HIGH when you enter your info, there is nothing wrong with the microcontrollers, and they’re both properly programmed. Then, the problem might be related to the transistor or resistor in your circuit.

      December 25, 2021 at 6:00 am
  • SunnyDeva Reply

    i am unable to convert usb to ttl and unable to dumb the code in esp8266
    can anyone help me

    January 6, 2022 at 7:52 pm
    • Mehran Maleki Reply

      Hi.
      Could you explain your problem more exactly? What do you mean you can’t convert USB and TTL? And are there any errors you get when you upload the code to your ESP8266 board?

      January 8, 2022 at 7:18 am
  • Naz Reply

    Hi,

    Can i know the connection of circuit before uploading the code? It is same as shown in the picture or the picture is the final result?

    January 10, 2022 at 12:47 pm
    • Mehran Maleki Reply

      Hi.
      The connection of the circuit is the same as shown in the picture.

      January 11, 2022 at 6:10 am
  • surendra Reply

    Hi, Nice project thank you! Can you please show how to change the password, because I cant find where to change the password and how to change the password in the code, can you please guide me.

    January 5, 2023 at 5:19 pm
    • Ali Abdolmaleki Reply

      Hi.
      Thank you for your kindness and also your good opinion.
      in top of header in c code you can find :

      const char* ssid = “YourSSID”;
      const char* password = “YourPASSWORD”;

      as you see you can set any password instead of YourPASSWORD

      March 1, 2023 at 7:01 am
  • dzul ilham Reply

    hello, i have an error when uploading second coding which is lead to error: espcomm_upload_mem failed
    error: espcomm_upload_mem failed. how to fix this error

    February 9, 2023 at 8:05 am
    • Ali Abdolmaleki Reply

      Hi.
      dear please be sure that you choose right board from Tools in Arduino IDE
      and try it with deferent Upload Speed.
      it should be work.

      February 19, 2023 at 5:40 am
  • khalis Reply

    my lock solenoid not open or close,please help

    February 12, 2023 at 12:24 pm
    • Ali Abdolmaleki Reply

      Hi.
      whats your exact problem?
      I mean did you check your voltage supply?
      could you please connect solenoid to DC Voltage Directly?

      February 19, 2023 at 5:36 am
  • sunny chawan Reply

    can we add rfid with it ? so that it will be two way verification type
    please reply….

    February 15, 2023 at 11:48 pm
    • Ali Abdolmaleki Reply

      Hi.
      yes you can use RFID reader without problem.
      just note that it need Seloniod with support RFID reader.

      February 19, 2023 at 6:02 am

Leave a Reply

Your email address will not be published. Required fields are marked *