hi

Dependencies:   4DGL-uLCD-SE Servo HC_SR04_Ultrasonic_Library mbed-rtos mbed

Created by David Sanchez, Jonathan Arthur, and Jong Pil Park

Smart Door Lock

Overview

The smart door lock system was designed with the purpose of adding a modular form of security and convenience to any door. A servo was connected to a door lock which locks and unlocks the door through three different methods: Using an app on a phone via Bluetooth , the use of pushbuttons, and a set of timers which lock the door after a certain time. Sonar was also implemented to determine whether the door is in an opened or closed state. An LCD was used to display the current state of the locking mechanism. Lastly, a speaker and an amplifier are included which are used as an alarm that goes off when the door is opened without authorization.

Bluetooth Thread

/media/uploads/Philipjp/ble.jpg

Adafruit BLEMbed
VinVu
gnd,CTSGnd
TXOp10
RXIp9

To be able to wirelessly unlock the door knob we used the adafruit BLE which can communicate with the Bluefruit app. We created a function which prints a line to the phone asking for a password. It waits until something is typed in and if the password is correct it unlocks/locks the door. If the password is not correct it just prints the same line again. This function runs on a separate thread so that other things can be done simultaneously.

void bluetooth(){
      
      while(1){

       blue.printf("enter password\n");   
             if (blue.getc()=='!')
              {
              if (blue.getc()=='!')
              {
               if (blue.getc()=='!')
              {
               if (blue.getc()=='!')
               {
                      i=!i;   
                     blue.printf("password is correct\n");  
               }
               }
               }
               }          
                                  }//end while loop
    }

LCD Thread

/media/uploads/Philipjp/8185.png

uLCD cableMbed
Vcc5V
GndGnd
TXp13
RXp14
RESETp15

Another thread was used for displaying the status of the door. If the door is closed a red message is displayed on the LCD and a green message is displayed if the door is opened. The LCD also tells if the door is locked or unlocked. The code for the LCD is provided below. This thread uses a mutex so that the program does not lock up.

void LCD()
{
    while(1)
    {
       mut.lock();
       if(i==0){
             
             uLCD.locate(7,9);
             uLCD.printf("Locked       ");
             }
             else 
             {
                 uLCD.locate(7,9);
             uLCD.printf("Unlocked");
             
     }
       mut.unlock();
       
       
       
       
       if (j==0)
       {
           mut.lock();
         
           uLCD.line(0, 0, 127, 0, RED);
            uLCD.line(127, 0, 127, 127, RED);
            uLCD.line(127, 127, 0, 127, RED);
             uLCD.line(0, 127, 0, 0, RED);
           uLCD.color(RED);
           uLCD.locate(7,8);
            uLCD.text_width(1);
           uLCD.text_height(1);
           uLCD.printf("CLOSED   ");
           mut.unlock();
       }//end if
       
       if(j==1)
        {
        
            mut.lock();
          
            uLCD.line(0, 0, 127, 0, GREEN);
            uLCD.line(127, 0, 127, 127, GREEN);
            uLCD.line(127, 127, 0, 127, GREEN);
             uLCD.line(0, 127, 0, 0, GREEN);
           uLCD.color(GREEN);
           uLCD.locate(7,8);
           uLCD.text_width(1);
           uLCD.text_height(1);
           uLCD.printf("OPEN    ");
          
         
            mut.unlock();
        }  
           
           
        Thread::wait(2);
                 
    }//end while
}//end LCD

Light Sensor Thread

/media/uploads/davidscque/lightsen.png

A light sensor is used to tell if it is night time. This thread checks if the amount of light is low and if it is it locks the door after 10 minutes.

void LEDT(){
     while(1){
           
           
           
            
              mut.lock(); 
              if(photocell<0.010)
              {
               mut.unlock(); 
               Thread::wait(100000);
               Thread::wait(100000);
               Thread::wait(100000);
                 mut.lock(); 
               if(photocell<0.010)
               i=0;
               } //end if
               
               
                 mut.unlock(); 
                 
                 
               //Thread::yield();  
                 
            }//end while
              

         
         }

Speaker Thread

/media/uploads/Philipjp/speaker.jpg /media/uploads/davidscque/amp.jpg

MbedTPA2005D1Speaker
gndpwr-,in-
Voutpwr+
p18in+
out++
out--

This thread checks if the door is opened while it is locked and makes the alarm go off.

void Pwspeaker()
    {
        
        while(1)
        {
            
         while(i==0&&j==1)
         {
           speaker.PlayNote(969.0, 0.5, 1.0);
        speaker.PlayNote(800.0, 0.5, 1.0);

          }   
            
            
            
        }
            
    }

Main Thread

/media/uploads/Philipjp/servoproduct.jpg.200x200_q85.jpg /media/uploads/Philipjp/servocable.png

Servo MotorMbed
BlackGnd
RED5V
Yellowp21

/media/uploads/Philipjp/hcsr04.jpg

Sonar SenorMbed
Vcc5V
GndGnd
trigp6
echop7

/media/uploads/Philipjp/bottom.jpg

Pb1Pb2Pb3Mbed
pin1p19
pin1p 23
pin1p24
pin2pin2pin2gnd

/media/uploads/Philipjp/rgbled.jpg

RGBLED lightMbed
REDp26
GndGnd
GREENp29
BLUEp30

The main thread initializes all the other threads, checks if the pushbuttons are pressed and checks if the door is closed or open. The pushbuttons simply unlock and lock the door. There is a third pushbutton that locks the door but 10 seconds after being pressed. This gives the person time to open and close the door before it locks. Sonar is used to check if the door is closed or open. The main thread initializes the sonar so that the distance from the sensor to an object is constantly measured. If this distance is greater than a certain value then it means that the door is open. This is used for the alarm which is turned on if the door is open while it is locked. An RGB LED used to indicate the status of the door. If the door is locked the green LED turns on and if the door is unlocked the red LED turns on. The blue LED turns on when the door is open.

int main() {    
uLCD.baudrate(3000000);
 pb1.mode(PullUp);
  pb2.mode(PullUp);
  pb3.mode(PullUp);
   mu.startUpdates();//start measuring the distance

thread3.start(Pwspeaker);       
         thread.start(bluetooth);
  thread2.start(LCD);
  thread4.start(LEDT);
 
   while(1){
       
       
        LEDG =!i;
        LEDB = 0;
        LEDR= i;   
            
    if(pb3==0){
     
     mut.lock();
     uLCD.cls();
     uLCD.color(RED);
     uLCD.locate(3,3);
     uLCD.text_width(3);
     uLCD.text_height(3);
     uLCD.printf("10");
     
          wait(1);
          uLCD.cls();
           uLCD.color(RED);
     uLCD.locate(3,3);
     uLCD.text_width(3);
     uLCD.text_height(3);
          uLCD.printf("9");
          wait(1);
          uLCD.cls();
           uLCD.color(RED);
     uLCD.locate(3,3);
     uLCD.text_width(3);
     uLCD.text_height(3);
          uLCD.printf("8");
          wait(1);
          uLCD.cls();
           uLCD.color(RED);
     uLCD.locate(3,3);
     uLCD.text_width(3);
     uLCD.text_height(3);
          uLCD.printf("7");
          wait(1);
          uLCD.cls();
           uLCD.color(RED);
     uLCD.locate(3,3);
     uLCD.text_width(3);
     uLCD.text_height(3);
          uLCD.printf("6");
          wait(1);
          uLCD.cls();
           uLCD.color(RED);
     uLCD.locate(3,3);
     uLCD.text_width(3);
     uLCD.text_height(3);
          uLCD.printf("5");
          wait(1);
          uLCD.cls();
           uLCD.color(RED);
     uLCD.locate(3,3);
     uLCD.text_width(3);
     uLCD.text_height(3);
          uLCD.printf("4");
          wait(1);
          uLCD.cls();
           uLCD.color(RED);
     uLCD.locate(3,3);
     uLCD.text_width(3);
     uLCD.text_height(3);
          uLCD.printf("3");
          wait(1);
          uLCD.cls();
           uLCD.color(RED);
     uLCD.locate(3,3);
     uLCD.text_width(3);
     uLCD.text_height(3);
          uLCD.printf("2");
          wait(1);
          uLCD.cls();
           uLCD.color(RED);
     uLCD.locate(3,3);
     uLCD.text_width(3);
     uLCD.text_height(3);
          uLCD.printf("1");
          wait(1);
          uLCD.cls();
           
     i=0;
     mut.unlock();
    
     } 
     
     
        
        
        
            
   if (pb1==0){//Locked
        
        i = 0;
      
        }
       
    if (pb2==0){//Unlocked
        i = 1;
        
        }
         if (j==1){//close
            LEDR =0;
           LEDG = 0;
           LEDB =1;
        }
       
  
 
   
        myservo=i;
      
      mut.lock();
        mu.checkDistance();
        mut.unlock();
        printf("%f \n",dist2);
        if (dist2>0.5f)
        {
            led=1;
            j=1;
           
        }
        else 
        {
            led=0;
            j=0;
           
            }
                
            
    }//end while
}//end main
Committer:
davidscque
Date:
Tue May 01 04:54:34 2018 +0000
Revision:
1:21d56f84fe51
final edit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
davidscque 1:21d56f84fe51 1 #include "ultrasonic.h"
davidscque 1:21d56f84fe51 2
davidscque 1:21d56f84fe51 3 ultrasonic::ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout):_trig(trigPin), _echo(echoPin)
davidscque 1:21d56f84fe51 4 {
davidscque 1:21d56f84fe51 5 _updateSpeed = updateSpeed;
davidscque 1:21d56f84fe51 6 _timeout = timeout;
davidscque 1:21d56f84fe51 7 }
davidscque 1:21d56f84fe51 8
davidscque 1:21d56f84fe51 9 ultrasonic::ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout, void onUpdate(int))
davidscque 1:21d56f84fe51 10 :_trig(trigPin), _echo(echoPin)
davidscque 1:21d56f84fe51 11 {
davidscque 1:21d56f84fe51 12 _onUpdateMethod=onUpdate;
davidscque 1:21d56f84fe51 13 _updateSpeed = updateSpeed;
davidscque 1:21d56f84fe51 14 _timeout = timeout;
davidscque 1:21d56f84fe51 15 _t.start ();
davidscque 1:21d56f84fe51 16 }
davidscque 1:21d56f84fe51 17 void ultrasonic::_startT()
davidscque 1:21d56f84fe51 18 {
davidscque 1:21d56f84fe51 19 if(_t.read()>600)
davidscque 1:21d56f84fe51 20 {
davidscque 1:21d56f84fe51 21 _t.reset ();
davidscque 1:21d56f84fe51 22 }
davidscque 1:21d56f84fe51 23 start = _t.read_us ();
davidscque 1:21d56f84fe51 24 }
davidscque 1:21d56f84fe51 25
davidscque 1:21d56f84fe51 26 void ultrasonic::_updateDist()
davidscque 1:21d56f84fe51 27 {
davidscque 1:21d56f84fe51 28 end = _t.read_us ();
davidscque 1:21d56f84fe51 29 done = 1;
davidscque 1:21d56f84fe51 30 _distance = (end - start)/6;
davidscque 1:21d56f84fe51 31 _tout.detach();
davidscque 1:21d56f84fe51 32 _tout.attach(this,&ultrasonic::_startTrig, _updateSpeed);
davidscque 1:21d56f84fe51 33 }
davidscque 1:21d56f84fe51 34 void ultrasonic::_startTrig(void)
davidscque 1:21d56f84fe51 35 {
davidscque 1:21d56f84fe51 36 _tout.detach();
davidscque 1:21d56f84fe51 37 _trig=1;
davidscque 1:21d56f84fe51 38 wait_us(10);
davidscque 1:21d56f84fe51 39 done = 0;
davidscque 1:21d56f84fe51 40 _echo.rise(this,&ultrasonic::_startT);
davidscque 1:21d56f84fe51 41 _echo.fall(this,&ultrasonic::_updateDist);
davidscque 1:21d56f84fe51 42 _echo.enable_irq ();
davidscque 1:21d56f84fe51 43 _tout.attach(this,&ultrasonic::_startTrig,_timeout);
davidscque 1:21d56f84fe51 44 _trig=0;
davidscque 1:21d56f84fe51 45 }
davidscque 1:21d56f84fe51 46
davidscque 1:21d56f84fe51 47 int ultrasonic::getCurrentDistance(void)
davidscque 1:21d56f84fe51 48 {
davidscque 1:21d56f84fe51 49 return _distance;
davidscque 1:21d56f84fe51 50 }
davidscque 1:21d56f84fe51 51 void ultrasonic::pauseUpdates(void)
davidscque 1:21d56f84fe51 52 {
davidscque 1:21d56f84fe51 53 _tout.detach();
davidscque 1:21d56f84fe51 54 _echo.rise(NULL);
davidscque 1:21d56f84fe51 55 _echo.fall(NULL);
davidscque 1:21d56f84fe51 56 }
davidscque 1:21d56f84fe51 57 void ultrasonic::startUpdates(void)
davidscque 1:21d56f84fe51 58 {
davidscque 1:21d56f84fe51 59 _startTrig();
davidscque 1:21d56f84fe51 60 }
davidscque 1:21d56f84fe51 61 void ultrasonic::attachOnUpdate(void method(int))
davidscque 1:21d56f84fe51 62 {
davidscque 1:21d56f84fe51 63 _onUpdateMethod = method;
davidscque 1:21d56f84fe51 64 }
davidscque 1:21d56f84fe51 65 void ultrasonic::changeUpdateSpeed(float updateSpeed)
davidscque 1:21d56f84fe51 66 {
davidscque 1:21d56f84fe51 67 _updateSpeed = updateSpeed;
davidscque 1:21d56f84fe51 68 }
davidscque 1:21d56f84fe51 69 float ultrasonic::getUpdateSpeed()
davidscque 1:21d56f84fe51 70 {
davidscque 1:21d56f84fe51 71 return _updateSpeed;
davidscque 1:21d56f84fe51 72 }
davidscque 1:21d56f84fe51 73 int ultrasonic::isUpdated(void)
davidscque 1:21d56f84fe51 74 {
davidscque 1:21d56f84fe51 75 //printf("%d", done);
davidscque 1:21d56f84fe51 76 d=done;
davidscque 1:21d56f84fe51 77 done = 0;
davidscque 1:21d56f84fe51 78 return d;
davidscque 1:21d56f84fe51 79 }
davidscque 1:21d56f84fe51 80 void ultrasonic::checkDistance(void)
davidscque 1:21d56f84fe51 81 {
davidscque 1:21d56f84fe51 82 if(isUpdated())
davidscque 1:21d56f84fe51 83 {
davidscque 1:21d56f84fe51 84 (*_onUpdateMethod)(_distance);
davidscque 1:21d56f84fe51 85 }
davidscque 1:21d56f84fe51 86 }