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. At the end of this project:

  • You will learn how to generate a specific QR code.
  • You will learn how to make a log in page by ESP8266.
  • You will be able to unlock a door via your cell phone.

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!

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.


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.

Share this post

Comments (17)

  • 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

Leave a Reply

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