Arduino Android Based Bluetooth Control Password P
Arduino Android Based Bluetooth Control Password P
Arduino & Android Based Bluetooth Control Password Protected Door Lock
(Version 2)
by taifur
I published an instructable on Arduino & Android An android application is required to open and close
based DIY smart lock which can be open and closed the lock and I will explain the details of android app
using password sent from Android smartphone over development in the later part of the tutorial. If the
Bluetooth two years back. The instructable become password is matched to your preset-lock password
very popular and I got lots of email from the then the lock will be open and sent a feedback to your
community. Here I am going to publish an updated phone.
version of my door lock. Now, the lock is fully 3D
printed.
https://www.youtube.com/embed/pPrKEFigT28
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 1
Step 1: Shopping List
Tools Required
1. 3D Printer
2. Screw Driver
Arduino Uno: Arduino Uno is the most popular development board in Arduino family. It is not mandatory to use
arduino uno, you can use another member from the arduino family such as arduino nano or arduino micro. You can
also use standalone atmega328p without using arduino baord. A well explain tutorial "From Arduino to a
Microcontroller on a Breadboard" for standalone atmega microcontroller is here.
Bluetooth Module (HC-05): Bluetooth is a type of wireless communication used to transmit voice and data at high
speeds using radio waves. It’s widely used in mobile phones for making calls, headset and share data. This type of
communication is a cheap and easy way to control something remotely using arduino. For communication with
arduino using blutooth a bluetooth module need to be connected with arduino. Several model of bluetooth module
are available. I am using here HC-05 a very common and cheap one. HC-05 module has 6 pins. We have to
connect 4 pins to arduino, they are:
RXD
TXD
VCC
GND
RXD will receive data from arduino; TXD will send data to arduino; VCC is the power supply (3.3V to 6.6V) and
GND is the ground. For details click here .
Micro Servo SG90: Tiny and lightweight with high output power. Servo can rotate approximately 180 degrees (90
in each direction), and works just like the standard kinds but smaller. You can use any servo code, hardware or
library to control these servos.
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 2
Step 2: Door Lock 3D Design
At the very beginning of this step, I would like to thanks Thingiverse user ridercz for his nice design. He designed
and published this servo based door lock. All the files except Rack_Holder.stl are taken from his design. You can
download the files from below or from Thingiverse.com .
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 3
Download (https://cdn.instructables.com/ORIG/FVG/5WDX/J7ARF62U/FVG5WDXJ7ARF62U.stl)
http://www.instructables.com/ORIG/FVG/5WDX/J7ARF62U/FVG5WDXJ7ARF62U.stl
… View in 3D
(https://cdn.instructables.com/ORIG/FVG/5WDX/J7ARF62U/FVG5WDXJ7ARF62U.stl)
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 4
http://www.instructables.com/ORIG/FLU/3JQM/J7ARF634/FLU3JQMJ7ARF634.stl
… Download (https://cdn.instructables.com/ORIG/FLU/3JQM/J7ARF634/FLU3JQMJ7ARF634.stl)
View in 3D
(https://cdn.instructables.com/ORIG/FLU/3JQM/J7ARF634/FLU3JQMJ7ARF634.stl)
Download (https://cdn.instructables.com/ORIG/FJA/4OPQ/J7ARF6CC/FJA4OPQJ7ARF6CC.stl)
http://www.instructables.com/ORIG/FJA/4OPQ/J7ARF6CC/FJA4OPQJ7ARF6CC.stl
… View in 3D
(https://cdn.instructables.com/ORIG/FJA/4OPQ/J7ARF6CC/FJA4OPQJ7ARF6CC.stl)
Download (https://cdn.instructables.com/ORIG/FD5/EHM9/J7ARF6FG/FD5EHM9J7ARF6FG.stl)
http://www.instructables.com/ORIG/FD5/EHM9/J7ARF6FG/FD5EHM9J7ARF6FG.stl
… View in 3D
(https://cdn.instructables.com/ORIG/FD5/EHM9/J7ARF6FG/FD5EHM9J7ARF6FG.stl)
Download (https://cdn.instructables.com/ORIG/FA0/R8B7/J7ARF6GL/FA0R8B7J7ARF6GL.stl)
http://www.instructables.com/ORIG/FA0/R8B7/J7ARF6GL/FA0R8B7J7ARF6GL.stl
… View in 3D
(https://cdn.instructables.com/ORIG/FA0/R8B7/J7ARF6GL/FA0R8B7J7ARF6GL.stl)
Download (https://cdn.instructables.com/ORIG/FNG/LOY7/J7ARF6HR/FNGLOY7J7ARF6HR.stl)
http://www.instructables.com/ORIG/FNG/LOY7/J7ARF6HR/FNGLOY7J7ARF6HR.stl
… View in 3D
(https://cdn.instructables.com/ORIG/FNG/LOY7/J7ARF6HR/FNGLOY7J7ARF6HR.stl)
Download (https://cdn.instructables.com/ORIG/F0N/7O4Z/J7ARF6I4/F0N7O4ZJ7ARF6I4.stl)
http://www.instructables.com/ORIG/F0N/7O4Z/J7ARF6I4/F0N7O4ZJ7ARF6I4.stl
… View in 3D
(https://cdn.instructables.com/ORIG/F0N/7O4Z/J7ARF6I4/F0N7O4ZJ7ARF6I4.stl)
Download (https://cdn.instructables.com/ORIG/FQI/24ZL/J7ARF6JF/FQI24ZLJ7ARF6JF.stl)
http://www.instructables.com/ORIG/FQI/24ZL/J7ARF6JF/FQI24ZLJ7ARF6JF.stl
… View in 3D
(https://cdn.instructables.com/ORIG/FQI/24ZL/J7ARF6JF/FQI24ZLJ7ARF6JF.stl)
Download (https://cdn.instructables.com/ORIG/F2Q/XYFD/J7ARF6MG/F2QXYFDJ7ARF6MG.stl)
http://www.instructables.com/ORIG/F2Q/XYFD/J7ARF6MG/F2QXYFDJ7ARF6MG.stl
… View in 3D
(https://cdn.instructables.com/ORIG/F2Q/XYFD/J7ARF6MG/F2QXYFDJ7ARF6MG.stl)
Download (https://cdn.instructables.com/ORIG/FD0/NY9F/J7ARF6RJ/FD0NY9FJ7ARF6RJ.stl)
http://www.instructables.com/ORIG/FD0/NY9F/J7ARF6RJ/FD0NY9FJ7ARF6RJ.stl
… View in 3D
(https://cdn.instructables.com/ORIG/FD0/NY9F/J7ARF6RJ/FD0NY9FJ7ARF6RJ.stl)
Download (https://cdn.instructables.com/ORIG/FOF/Y7YB/J7ARF6SN/FOFY7YBJ7ARF6SN.stl)
http://www.instructables.com/ORIG/FOF/Y7YB/J7ARF6SN/FOFY7YBJ7ARF6SN.stl
… View in 3D
(https://cdn.instructables.com/ORIG/FOF/Y7YB/J7ARF6SN/FOFY7YBJ7ARF6SN.stl)
Download (https://cdn.instructables.com/ORIG/FKK/50CA/J7ARF78A/FKK50CAJ7ARF78A.stl)
http://www.instructables.com/ORIG/FKK/50CA/J7ARF78A/FKK50CAJ7ARF78A.stl
… View in 3D
(https://cdn.instructables.com/ORIG/FKK/50CA/J7ARF78A/FKK50CAJ7ARF78A.stl)
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 5
Step 3: Assembling of 3D Printed Parts
Assemble all the printed files one by one as shown in the photos. You may also follow the video instruction
attached in the first step.
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 6
Step 4: Arduino Program for Bluetooth Control Password Protected Door Lock
The code for bluetooth control smart door lock is very simple. I have designed android application in such that it
sent a command with the password. Actually I set two command, one is "OPEN=" and another is "CLOSE=" and
password may contain any digit, later or symbol or combination of them as you like. In the app I used two buttons
one for open the door and another for closing the door. When password is given to password box and open button
is clicked than app joint the "OPEN=" command with the password and sent it to arduino. I add '=' sign with
command for that I will separate command and password by using this '=' . After receiving the string from phone
arduino program separates command and password from the receive string and save them into two separate
variables. Then check the password first, if password matched with saved password then open the door for
"OPEN" command, and close the door for "CLOSE" command. "=" helps to separate command and
password.Complete arduino sketch is given below.
<code style="display:block;white-space:pre-wrap;color:green">
/* Athor: Md. Khairul Alam
Date: 1 September, 2015
This program is for password protected smart door lock */
#include
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 7
#include
void setup(){
//start serial connection
Serial.begin(9600); // baud rate is 9600 must match with bluetooth
//The String reserve() function allows you to allocate a buffer in memory for manipulating strings.
inputString.reserve(50); // reserve 50 bytes in memory to save for string manipulation
command.reserve(50);
value.reserve(50);
void loop(){
// if arduino receive a string termination character like \n stringComplete will set to true
if (stringComplete) {
//Serial.println(inputString);
delay(100);
// identified the posiion of '=' in string and set its index to pos variable
int pos = inputString.indexOf('=');
// value of pos variable > or = 0 means '=' present in received string.
if (pos > -1) {
// substring(start, stop) function cut a specific portion of string from start to stop
// here command will be the portion of received string till '='
// let received string is open=test123
// then command is 'open'
command = inputString.substring(0, pos);
// value will be from after = to newline command
// for the above example value is test123
// we just ignoreing the '=' taking first parameter of substring as 'pos+1'
// we are using '=' as a separator between command and vale
// without '=' any other character can be used
// we are using = menas our command or password must not contains any '=', otherwise it will cause error
value = inputString.substring(pos+1, inputString.length()-1); // extract command up to \n exluded
//Serial.println(command);
//Serial.println(value);
// password.compareTo(value) compare between password tring and value string, if match return 0
if(!password.compareTo(value) && (command == "OPEN")){
// if password matched and command is 'OPEN' than door should open
openDoor(); // call openDoor() function
Serial.println(" OPEN"); // sent open feedback to phone
delay(100);
}
else if(!password.compareTo(value) && (command == "CLOSE")){
// if password matched and command is 'CLOSE' than door should close
closeDoor();
Serial.println(" CLOSE"); // sent " CLOSE" string to the phone
delay(100);
}
else if(password.compareTo(value)){
// if password not matched than sent wrong feedback to phone
Serial.println(" WRONG");
delay(100);
}
}
// clear the string for next iteration
inputString = "";
stringComplete = false;
}
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 8
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
//Serial.write(inChar);
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline or a carriage return, set a flag
// so the main loop can do something about it:
if (inChar == '\n' || inChar == '\r') {
stringComplete = true;
}
}
}
void openDoor(){
myservo.write(0);
delay(100);
}
void closeDoor(){
myservo.write(65);
delay(100);
}
If arduino received wrong password then it sent a message to the android phone that the password is wrong.
Actually arduino sent " WRONG" string. Android program then shows wrong password message.
Download (https://cdn.instructables.com/ORIG/FTT/9GKX/J7ARC8L0/FTT9GKXJ7ARC8L0.ino)
http://www.instructables.com/ORIG/FTT/9GKX/J7ARC8L0/FTT9GKXJ7ARC8L0.ino
…
(https://cdn.instructables.com/ORIG/FTT/9GKX/J7ARC8L0/FTT9GKXJ7ARC8L0.ino)
I hope you programmed your Arduino board with the hot glue to tightly adjust the module with the lock.
sketch attached in the previous step. Now, place the After that, place the Arduino board to the right place.
lock in the appropriate place of the door. First, place Finally, connect the wires according to the schematic.
the lock with four screws. Then, fix the Bluetooth
module at the right place of the lock and add some
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 9
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 10
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 11
Step 6: Android App Development for Password Protected Door Lock
I will show you how to develop an Bluetooth android application using MIT App Inventor. I am using App Inventor
because it does't required any coding and no software installation. You only need a google account. Go to
http://ai2.appinventor.mit.edu/, you will ask to log in using google account.
Log in to App Inventor using gmail and follow the steps below.
You are now in the Designer, where you lay out the "user interface" of your app (image 6). The Designer Window
is where you lay out look and feel of your app, and specify what functionalities it should have. You choose things
for the user interface things like Buttons, Image, Label, Text boxes, and functionalities like Text-to-Speech,
Bluetooth, Sensors, and GPS.
Now, follow the figure 7 and add a List Picker to the viewer. Rename it to "Connect to Bluetooth Device" (image 8).
Add one Label, one Password Text Box, and two buttons shown in figure 10. Change the text properties of the
components as "Enter Password", "Open Door", and "Close Door" respectively. Final User Interface should look
like as figure 11. Now add a Bluetooth Client to the viewer. It is an invisible component and it has no UI. See
Image 12.
It's time to tell your app what to do! Click "Blocks" to move over to the Blocks Editor (image 13). Think of the
Designer and Blocks buttons like tabs- you use them to move back and forth between the two areas of App
Inventor. The Blocks Editor is where you program the behavior of your app. There are Built-in block that handle
things like math, logic, and text. Below that are the blocks that go with each of the components in your app. In
order to get the blocks for a certain component to show up in the Blocks Editor, you first have to add that
component to your app through the Designer.
Let us, design blocks for List Picker. List Picker is a UI element when clicked it shows a list of corresponding
elements here is the paired Bluetooth device. We have to add two blocks ListPicker.BeforePicking and
ListPicker.AfterPicking (image 14). Complete Blocks are shown in figure 18 & 19.
Our design is complete, now we need to build the apk file for our android phone. Click to build menu and select
"App(save .apk to my computer)". Wait for a minute. An apk file will download to your default download folder.
Install and use it.
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 12
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 13
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 14
Step 7: A More Feature Rich Android App
In previous step I have showed you how to develop a attached herewith. If you did not like to make your
basic bluetooth app in App Inventor. But that app is own app or modify it, just download the BTcontrol.apk
not so user friendly. It will not show you any message and install it to your android pone.
either your door is open or closed or bluetooth can
not connected for any error. I have attached here a You can also download it from Google Play Store
more advanced app for our project. using the link:
When you run you app this app check either your https://play.google.com/store/apps/details?
bluetooth radio is enable or not, if not enable a id=appinventor.ai_khairul_uapstu.BTcontrol&hl=en
bluetooth enabling dialog will appear. Click yes to
turn on your bluetooth radio. Then pair the device For modifying the source click the "Projects" menu
using pair device button. Then click to connect button, and select "Import project (.aia) from my computer"
if successfully connected to any device the shown in figure 24 and browse the BTcontrol.aia file
application shows the connected device's MAC from your computer. After importing you can modify it
address and name. Enter password to the password easily.
box then click the open door button, if you entered
correct password door will be open and application If you want to know more about android app
will show you a message that your door is now open. developing using App Inventor browse:
Then enter password again if you want to close the http://appinventor.mit.edu/explore/ai2/tutorials.html.
door. If you give the wrong password, then app will You can also try http://meta-
show wrong password message. guide.com/videography/100-best-appinventor-videos/
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 15
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 16
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 17
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 18
Download (https://cdn.instructables.com/ORIG/FTQ/D5CE/J7ARCY2P/FTQD5CEJ7ARCY2P.aia)
http://www.instructables.com/ORIG/FTQ/D5CE/J7ARCY2P/FTQD5CEJ7ARCY2P.aia
…
(https://cdn.instructables.com/ORIG/FTQ/D5CE/J7ARCY2P/FTQD5CEJ7ARCY2P.aia)
Download (https://cdn.instructables.com/ORIG/FBM/YWUU/J7ARCY31/FBMYWUUJ7ARCY31.apk)
http://www.instructables.com/ORIG/FBM/YWUU/J7ARCY31/FBMYWUUJ7ARCY31.apk
…
(https://cdn.instructables.com/ORIG/FBM/YWUU/J7ARCY31/FBMYWUUJ7ARCY31.apk)
Arduino & Android Based Bluetooth Control Password Protected Door Lock (Version 2): Page 19