Md. Khairul Alam
Published © GPL3+

Arduino and Android Based Password Protected Door Lock

A lock that can be controlled from a smartphone using an app.

AdvancedFull instructions provided3 hours33,353
Arduino and Android Based Password Protected Door Lock

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
HC-05 Bluetooth Module
HC-05 Bluetooth Module
×1
Servos (Tower Pro MG996R)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

Thingiverse

https://www.thingiverse.com/thing:2350856

Rack Holder

All

Schematics

Schematic

AIA Source file

Android app

Code

Arduino Sketch

Arduino
#include <Servo.h>

Servo myservo;  // create servo object to control a servo

String inputString = "";
String command = "";
String value = "";
String password = "arduPi"; // this is the password for opening and closing your door
                            // you can set any pasword you like using digit and symbols
boolean stringComplete = false; 

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);
  
  boolean stringOK = false;
  
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  
}

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;
    }
   
}


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); 
}

Credits

Md. Khairul Alam

Md. Khairul Alam

68 projects • 584 followers
Developer, Maker & Hardware Hacker. Currently working as a faculty at the University of Asia Pacific, Dhaka, Bangladesh.

Comments