DIY: Creating a SmartLock for Your Home That Only Opens With Your Smartphone

Nowadays, there are so many locks available in the market that need some sort of password to open it. These locks are very efficient but very costly. If we need to make a lock for a small scale purpose that is automated and is unlocked or locked without any password but with a smartphone, we can make it by using some components that are easily available in the market.

This lock will be very low in cost and will work perfectly on a small scale. An android application will be needed to operate this lock. Now, let’s move towards setting up the ESP32, installing necessary packages on it and making certain hardware changes!

Automatic DoorLock

Without wasting any time, let’s get started studying important concepts and working on this project.

How To Make Android Operated SmartLock?

Step 1: Gathering The Components

Before starting any project, if there is a fear that you will get stuck in the middle of the project and waste time, there is an excellent approach to avoid it. Make a complete list of all the components that you are going to need in the project and buy them in the first place. Following is the complete list of all the components that we are going to use in this project. All these components are easily available in the market.

Step 2: Making The App

As we are going to make a smart lock that will be operated by a mobile phone, we need to develop an android app that will contain a button. By pressing that button we will be able to open or close the smart lock. Previously, we have developed several android applications. We have already developed an application that consists of only one button in it. This button is used to send data into the database. if ‘1’ is pushed into the firebase database, the switch lock will open and if ‘0’ is pushed into that database, the lock will be closed.

Please refer to our article named Creating a Wireless On/OFF Switch Switch For Your PC to take help to develop your own android application that will be used to operate the smart lock.

Step 3: Assembling The Components

As we have a complete list of all the components that we need to complete this project, let us move one step ahead and assemble all the components together.

Take the servo motor and connect its Vcc and ground to the Vcc and ground of the ESP board respectively. Connect the PWM pin of your servo motor to the pin 34 of your ESP32 board. Now see that there is a gear-type knob on a servo motor. Take out the handle of the lock by rotating it and fix the knob of gear motor in the lock with the help of some adhesives.

Now, with the help of the drill machine, drill some holes on the door where you want to place this smart lock. Make sure that you drill the holes in such a way that the holes of the lock overlap the holes in the door, making way for the screw to fix in.

Step 4: Working

As we now know the main idea behind this project, let us understand how this project is going to work.

ESP32 is the heart of this project. A servo motor is connected to this board and this microcontroller has a connection to the firebase database. When the button in the app is pressed to open the lock, ‘1’ is pushed into the firebase database and when the button is pressed to close the lock, ‘0’ is pushed into the firebase database. The ESP board is reading this value in the firebase database continuously. As long as there is 0, the ESP32 will direct the servo motor to remain in its initial position. As soon as 1 comes into the firebase, the ESP board will read it and tell the servo motor to make a rotation that will open the lock.

Step 5: Getting Started With ESP32

If you haven’t worked on Arduino IDE before, don’t worry because a step by step to set up Arduino IDE is shown below.

  1. Download the latest version of Arduino IDE from Arduino.
  2. Connect your Arduino board to the PC and open Control Panel. Click on Hardware and Sound. Now open Devices and Printer and find the port to which your board is connected. In my case it is COM14 but it is different in different computers.
    Finding Port
  3. Click on File and then click on Preferences. Copy the following link in the Additional Board Manager’s URL. “https://dl.espressif.com/dl/package_esp32_index.json”
    Preferences
  4. Now, to use ESP32 with Arduino IDE, we need to import special libraries that will allow us to burn code on ESP32 and use it. these two libraries are attached in the link given below. To include the library, goto Sketch > Include Library > Add ZIP Library. A box will appear. Find the ZIP folder on your computer and click OK to include the folders.
    Include Library
  5. Now goto Sketch > Include Library > Manage Libraries.
    Manage Libraries
  6. A Menu will open. In the search bar, type Arduino JSON. A list will appear. Install Arduino JSON by Benoit Blanchon.
    Arduino JSON
  7. Now click on the Tools. A dropdown menu will appear. Set the board to ESP Dev Module.
    Setting Board 
  8. Click on the Tool menu again and set the port that you observed in the control panel before.
    Setting Port
  9. Now Upload the code that is attached in the link below and click on the upload button to burn the code on the ESP32 microcontroller.
    Upload

So now when you will upload the code, an error may occur. This is the most common error that may occur if you are using a new version of the Arduino IDE and the Arduino JSON. The following are the errors that you may see on the screen.

In file included from C:\Users\Pro\Documents\Arduino\libraries\IOXhop_FirebaseESP32-master/IOXhop_FirebaseESP32.h:8:0,

from C:\Users\Pro\Desktop\smartHome\code\code.ino:2:

C:\Users\Pro\Documents\Arduino\libraries\IOXhop_FirebaseESP32-master/IOXhop_FirebaseStream.h:14:11: error: StaticJsonBuffer is a class from ArduinoJson 5. Please see arduinojson.org/upgrade to learn how to upgrade your program to ArduinoJson version 6

StaticJsonBuffer<STREAM_JSON_BUFFER_SIZE> jsonBuffer;

^

In file included from C:\Users\Pro\Documents\Arduino\libraries\IOXhop_FirebaseESP32-master/IOXhop_FirebaseESP32.h:8:0,

from C:\Users\Pro\Desktop\smartHome\code\code.ino:2:

C:\Users\Pro\Documents\Arduino\libraries\IOXhop_FirebaseESP32-master/IOXhop_FirebaseStream.h:65:11: error: StaticJsonBuffer is a class from ArduinoJson 5. Please see arduinojson.org/upgrade to learn how to upgrade your program to ArduinoJson version 6

return StaticJsonBuffer<STREAM_JSON_DATA_BUFFER_SIZE>().parseObject(_data);

^

Multiple libraries were found for "WiFi.h"
Used: C:\Users\Pro\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.2\libraries\WiFi
Not used: C:\Program Files (x86)\Arduino\libraries\WiFi
Using library WiFi at version 1.0 in folder: C:\Users\Pro\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.2\libraries\WiFi 
Using library IOXhop_FirebaseESP32-master in folder: C:\Users\Pro\Documents\Arduino\libraries\IOXhop_FirebaseESP32-master (legacy)
Using library HTTPClient at version 1.2 in folder: C:\Users\Pro\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.2\libraries\HTTPClient 
Using library WiFiClientSecure at version 1.0 in folder: C:\Users\Pro\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.2\libraries\WiFiClientSecure 
Using library ArduinoJson at version 6.12.0 in folder: C:\Users\Pro\Documents\Arduino\libraries\ArduinoJson 
exit status 1
Error compiling for board ESP32 Dev Module.

There is nothing to worry about because we can eliminate these errors by following some simple steps. These errors are arising because the new version of Arduino JSON has another class instead of StaticJsonBuffer. This is the class of JSON 5. So we can simply eliminate this error by downgrading the version of Arduino JSON of our Arduino IDE. Simply go to Sketch > Include Library > Manage Libraries. Search for Arduino JSON by Benoit Blanchon that you have installed before. Uninstall it first and then set its version to 5.13.5. Now as we have set an old version of Arduino JSON, install it again and recompile the code. This time, your code will compile successfully.

To download the code, click here.

Step 6: Code

the code of this project is very simple but still, some chunks of it are explained below.

1. At the start of the code, we will include three libraries. The first is to enable Wifi on the ESP board, second is to enable ESP to use servo motor and the third is to connect the ESP board to the firebase database. After that, we will add the firebase host, authentication, name of our local internet connection and its password in the code. After doing that, create an object to use the servo motor.

#include<WiFi.h>                  // include library to use WiFi
#include <Servo.h>                // include library for servo motor
#include <IOXhop_FirebaseESP32.h> // include library to connect to Firebase

#define FIREBASE_HOST "xxxxxxxxxx" // replace xxxxxxxxxx by your firebase host here
#define FIREBASE_AUTH "xxxxxxxxxx" // replace xxxxxxxxxx by your firebase authentication here
#define WIFI_SSID "xx code,xxxxxxxx"     // replace xxxxxxxxxx by the name of our Wifi connection
#define WIFI_PASSWORD "xxxxxxxxxx" // replace xxxxxxxxxx by your wifi password

Servo myservo; // create object for servo motor
int pos = 0;   // creating variable
int state;     // creating variable

2. void setup() is a function that runs only once in a program when the microcontroller board is powered on or the enable button is pressed. The baud rate is set in this function. Baud rate is actually the communication speed in bits per second through which the microcontroller communicates with the external devices. The servo motor is connected to the pin34 of the ESP board. code is written in this function to connect the microcontroller to the local internet connection.

void setup() { 
Serial.begin(115200); // setting the baud rate
myservo.attach(34);   // connect PWM pin of the servo motor to pin34 of ESP32
myservo.write(60);
delay(1000); 

// connect to wifi.
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.println("connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}

3. void loop() is a function that runs again and again in a loop. In this function, we tell the microcontroller, what operations to carry out and how. Here, data is being read from the firebase and stored int the variable named state. If the state has value ‘0’ in it, the position of the servo motor is set to 8 degrees. If the value in the variable state is equal to ‘1’, the position of the servo motor will be set to 55 degrees.

void loop()
{
state = Serial.println(Firebase.getFloat("motor")); // read the data from the firebase

// if the state is '0' the DC motor will turn off
if (state == '0')
{
myservo.write(8);  // set position of the servo motor
delay(1000);       // wait for onw second
Serial.println("Door Locked");
} 
else if (state == '1')
{
myservo.write(55); // set new position of the servo motor
delay(1000);       // wait for one second
Serial.println("Door UnLocked");
}

// handle error
if (Firebase.failed()) {
Serial.print("setting /number failed:");
Serial.println(Firebase.error()); 
return;
}
delay(1000);

Appuals.com is a participant in the Amazon Services LLC Associates Program, and we get a commission on purchases made through our links.

Hamza Iqbal


Hey! I am Hamza. I am an Electrical Engineer who has a very innovative approach towards daily life stuff. I tend to make life easier by making circuits and designs to automate things around me. I mainly work with printed circuit boards on proteus to bring life to my inventions.
Close