jegatheesan
Published © GPL3+

Smart BT Lock with Custom Number

High security cheap number lock. No one can able to see where the lock from outside the door. Use change password when ever they want.

AdvancedFull instructions provided7,361
Smart BT Lock with Custom Number

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Android device
Android device
×1
servo motor
×1
Door Lock
×1

Software apps and online services

Bluetooth Controller App

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Diagram

Code

BTlocker.ino

Arduino
#include <LBT.h>
#include <LBTServer.h>
#include <LEEPROM.h>
#include <Servo.h> 

int readval;
Servo myservo;

int resultvalue;
int checktime=0;

String readpassword;
String Password;
String msg;

const int buttonPin = 12;
const int ledPin =  13;

boolean firstconn;
boolean changepass;
int Doorstatus;
int Dooraddress=100;

void setup() {
 if(!LBTServer.begin((uint8_t*)"Sivam_LIO"))
 {
 return;
 }  
//  writepass("123456");
  Doorstatus=EEPROM.read(Dooraddress);
//  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  firstconn=false;
  changepass=false;
  readpassword="";
  Password=readpass();
  myservo.attach(9);
  delay(100);
}

void servocommand()
{
  if (Doorstatus==0)
  {
    myservo.write(0);
    delay(100);
  }
  else
  {
    myservo.write(180);    
    delay(100);
  }
}

void loop() {
 uint8_t buf[1];
 int bytesRead;
if(LBTServer.connected())
 {
   if (firstconn==false)
   {
     firstconn=true;
     delay(200);     
     LBTServer.write("Hello Welcome");     
     delay(100);
     if (Doorstatus==0)
     {
       LBTServer.write("Door is close");            
       delay(100);       
       LBTServer.write("Type Password to open");
       delay(100);       
     }
     else
     {
       LBTServer.write("Door is open");            
       delay(100);       
       LBTServer.write("Type Password to close");
       delay(100);              
     }
   }
   if (digitalRead(buttonPin) == HIGH && Doorstatus==0 && changepass==false)
   {     
     changepass=true;
     LBTServer.write("Type new password");
     readpassword="";
     delay(100);
   }
   else if (digitalRead(buttonPin) == LOW && changepass==true)
   {
     changepass=false;
     LBTServer.write("Exit from password change");
     readpassword="";
     delay(100);     
   }
   while(true)
   {
     bytesRead = LBTServer.readBytes(buf, 1);
     if(!bytesRead)
     break;
     readval = buf[0];
     readpassword = readpassword + char(readval);
     checktime=0;
//     msg = "U type -" + readpassword;
//     LBTServer.write(msg);   
//      Serial.println(readpassword);
     if (readpassword.length()==6)
     {
       if (changepass==false)
       {
         if (readpassword.equals(Password))
         {
           if (Doorstatus==0)
           {
             Doorstatus=1;
             servocommand();
             EEPROM.write(Dooraddress,1);
             LBTServer.write("Door Open");
           }
           else
           {
             Doorstatus=0;
             servocommand();
             EEPROM.write(Dooraddress,0);           
             LBTServer.write("Door Close");           
           }
         }
         else
         {
           LBTServer.write("Invalid Password");              
         }  
       }       
      else
      {
        writepass(readpassword);
        Password=readpassword;
        LBTServer.write("Password Changed");
        delay(100);
        LBTServer.write("Remove Your hand");
        delay(100);
      }
       readpassword="";
     }     
   }
   delay(100);
 
  if (readpassword!="")
  {
   checktime=checktime+1;
   if (checktime>5)
   {
     readpassword="";
     changepass=false;
     checktime=0;
      LBTServer.write("Time out. Try again");
      delay(100);     
   }
  }
  
  }
  else
  {
     readpassword="";
     firstconn=false;
     changepass=false;
     LBTServer.accept(5);
  }
  
//  if (digitalRead(buttonPin) == HIGH) {
//    Serial.println(2);
//    digitalWrite(ledPin, HIGH);
//  }
//  else {
//    Serial.println(3);
//    digitalWrite(ledPin, LOW);
//  }  
  
  delay(100);
}


String readpass()
{
  String mempass;
  int memval;
  int memadd;
  mempass="";
  for(int i=0;i<6;i++)
  {
    memadd=200+i;
    memval=EEPROM.read(memadd);
    if (memval>9)
    {
      memval=0;
    }
    mempass=mempass + memval;
  }
  return mempass;
}
void writepass(String passw)
{
  String mempass;
  int memval;
  int memadd;
  for(int i=0;i<6;i++)
  {
    mempass=passw.substring(i,i+1);
    memval=mempass.toInt();
    memadd=200+i;
    EEPROM.write(memadd,memval);
  }
}

BTlockerWA.ino

Arduino
#include <LBT.h>
#include <LBTServer.h>
#include <LEEPROM.h>
//#include <Servo.h> 

int readval;
//Servo myservo;

int resultvalue;
int checktime=0;

String readpassword;
String Password;
String msg;

const int buttonPin = 12;x
//const int ledPin =  13;
const int ctrlpin =  10;

boolean firstconn;
boolean changepass;
int Doorstatus;
int Dooraddress=100;

void setup() {
 if(!LBTServer.begin((uint8_t*)"Sivam_LIO"))
 {
 return;
 }  
//  writepass("123456");
  Doorstatus=EEPROM.read(Dooraddress);
//  Serial.begin(9600);
  pinMode(ctrlpin, OUTPUT);
  pinMode(buttonPin, INPUT);
  firstconn=false;
  changepass=false;
  readpassword="";
  Password=readpass();
//  myservo.attach(9);
  delay(100);
}

void servocommand()
{
  if (Doorstatus==0)
  {
    digitalWrite(ctrlpin, LOW);
//    myservo.write(0);
//    delay(100);
  }
  else
  {
    digitalWrite(ctrlpin, HIGH);
//    myservo.write(180);    
//    delay(100);
  }
}

void loop() {
 uint8_t buf[1];
 int bytesRead;
if(LBTServer.connected())
 {
   if (firstconn==false)
   {
     firstconn=true;
     delay(200);     
     LBTServer.write("Hello Welcome");     
     delay(100);
     if (Doorstatus==0)
     {
       LBTServer.write("Door is close");            
       delay(100);       
       LBTServer.write("Type Password to open");
       delay(100);       
     }
     else
     {
       LBTServer.write("Door is open");            
       delay(100);       
       LBTServer.write("Type Password to close");
       delay(100);              
     }
   }
   if (digitalRead(buttonPin) == HIGH && Doorstatus==0 && changepass==false)
   {     
     changepass=true;
     LBTServer.write("Type new password");
     readpassword="";
     delay(100);
   }
   else if (digitalRead(buttonPin) == LOW && changepass==true)
   {
     changepass=false;
     LBTServer.write("Exit from password change");
     readpassword="";
     delay(100);     
   }
   while(true)
   {
     bytesRead = LBTServer.readBytes(buf, 1);
     if(!bytesRead)
     break;
     readval = buf[0];
     readpassword = readpassword + char(readval);
     checktime=0;
//     msg = "U type -" + readpassword;
//     LBTServer.write(msg);   
//      Serial.println(readpassword);
     if (readpassword.length()==6)
     {
       if (changepass==false)
       {
         if (readpassword.equals(Password))
         {
           if (Doorstatus==0)
           {
             Doorstatus=1;
             servocommand();
             EEPROM.write(Dooraddress,1);
             LBTServer.write("Door Open");
           }
           else
           {
             Doorstatus=0;
             servocommand();
             EEPROM.write(Dooraddress,0);           
             LBTServer.write("Door Close");           
           }
         }
         else
         {
           LBTServer.write("Invalid Password");              
         }  
       }       
      else
      {
        writepass(readpassword);
        Password=readpassword;
        LBTServer.write("Password Changed");
        delay(100);
        LBTServer.write("Remove Your hand");
        delay(100);
      }
       readpassword="";
     }     
   }
   delay(100);
 
  if (readpassword!="")
  {
   checktime=checktime+1;
   if (checktime>5)
   {
     readpassword="";
     changepass=false;
     checktime=0;
      LBTServer.write("Time out. Try again");
      delay(100);     
   }
  }
  
  }
  else
  {
     readpassword="";
     firstconn=false;
     changepass=false;
     LBTServer.accept(5);
  }
  
//  if (digitalRead(buttonPin) == HIGH) {
//    Serial.println(2);
//    digitalWrite(ledPin, HIGH);
//  }
//  else {
//    Serial.println(3);
//    digitalWrite(ledPin, LOW);
//  }  
  
  delay(100);
}


String readpass()
{
  String mempass;
  int memval;
  int memadd;
  mempass="";
  for(int i=0;i<6;i++)
  {
    memadd=200+i;
    memval=EEPROM.read(memadd);
    if (memval>9)
    {
      memval=0;
    }
    mempass=mempass + memval;
  }
  return mempass;
}
void writepass(String passw)
{
  String mempass;
  int memval;
  int memadd;
  for(int i=0;i<6;i++)
  {
    mempass=passw.substring(i,i+1);
    memval=mempass.toInt();
    memadd=200+i;
    EEPROM.write(memadd,memval);
  }
}

servocontrol.ino

Arduino
#include <Servo.h> 
const int buttonPin = 12;
int buttonstate = 0;
Servo myservo;


void setup() {
  myservo.attach(9);
  pinMode(buttonPin, INPUT);
}

void loop() {
  if (digitalRead(buttonPin) == HIGH && buttonstate==0)
  {
    buttonstate=1;
    myservo.write(0);
    delay(15);          
  }
  else if (digitalRead(buttonPin) == LOW && buttonstate==1)
  {
    buttonstate=0;
    myservo.write(180);
    delay(15);          
  }
}

Credits

jegatheesan

jegatheesan

18 projects • 67 followers
Simply A Mechatronics Lover.

Comments