Jayakarthigeyan Prabakar
Published © GPL3+

Smart Lock - Internet connected door lock with door bell

It's a Wi-Fi video door lock with doorbell which lets you control your door lock from anywhere around the world

AdvancedFull instructions provided20 hours7,634

Things used in this project

Hardware components

OLED Display
×1
Adafruit 16mm Illuminated Pushbutton - Green Momentary
×1
Adafruit 16mm Illuminated Pushbutton - White Momentary
×1
JQC-3F(T73) DC 12V 5PIN Power Relay
×1
Logitech C270 Webcam
×1
https://www.adafruit.com/products/419
×1
Buzzer
Buzzer
×1
LM2596 power Supply Buck Converter step-down module
×1
Photo Coupler PC817
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Resistor 1k ohm
Resistor 1k ohm
×3
Resistor 100 ohm
Resistor 100 ohm
×2
General Purpose Transistor NPN
General Purpose Transistor NPN
×2
Magnetic lock with read switch
×1
LED (generic)
LED (generic)
×2
1N4007 – High Voltage, High Current Rated Diode
1N4007 – High Voltage, High Current Rated Diode
×4
1N4742
×1
Wall AC to 12V, 2A DC adapter
×1
SparkFun DC Barrel Jack Adapter
×1

Software apps and online services

Arduino IDE
Arduino IDE
MobaXterm
PuTTY
Phonegap

Hand tools and fabrication machines

Laser cutter (generic)
Laser cutter (generic)
Other stuff like drilling machine, drill bits, screw driver, screws, clamps etc

Story

Read more

Custom parts and enclosures

Outdoor body part 1

Outdoor body part 2

Outdoor body part 3

Outdoor body part 4

Outdoor body part 5

Indoor body part 1

Indoor body part 2

Indoor body part 3

Indoor body part 4

Schematics

Schematic

All in one schematic of the smart lock excluding the components AC to DC power adapter and camera

Code

app.js

JavaScript
app.js file which goes inside app folder on home directory of LinkIt Smart 7688 duo
var http = require('http');
var fs = require('fs');
var index = fs.readFileSync('/root/app/index.html');
//serialport
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var serialPort = new SerialPort("/dev/ttyS0", {
    baudrate: 9600,
    parser: serialport.parsers.readline("\n")
});
var app = http.createServer(function(req, res) {
    res.writeHead(200, {
        'Content-Type': 'text/html'
    });
    res.end(index);
});
var io = require('socket.io')(app);
app.listen(4000);
console.log("4000");
serialPort.on("open", function() {
    console.log('Console open');
    serialPort.write('Begin.');
    serialPort.on('data', function(data) {
        console.log(data);
        io.emit('data', data);
    });
});
io.on('connection', function(socket) {
    console.log('connected');
    socket.on('lock', function(msg) {
        console.log(msg);
        serialPort.write('LOCKIT.');
    });
    socket.on('pin', function(msg) {
        serialPort.write(msg + ".");
        console.log(msg);
    });
    socket.on('unlock', function(msg) {
        console.log(msg);
        serialPort.write('UNLOCKIT.');
    });
});

index.html

HTML
index.html file goes inside app folder on home directory of LinkIt Smart 7688 duo
<html>

<head>
    <style>
        .rotate180 {
            -webkit-transform: rotate(180deg);
            -moz-transform: rotate(180deg);
            -o-transform: rotate(180deg);
            -ms-transform: rotate(180deg);
            transform: rotate(180deg);
        }
        
        .button {
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 15px 82px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 4px 2px;
            cursor: pointer;
        }
        
        .button1 {
            background-color: #477fd8;
            border: none;
            color: white;
            padding: 15px 142px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 4px 2px;
            cursor: pointer;
        }
        
        .button2 {
            background-color: #ea430b;
            border: none;
            color: white;
            padding: 15px 150px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 4px 2px;
            cursor: pointer;
        }
    </style>
    <title>
        Smart Lock
    </title>
    <script src="http://192.168.4.3:4000/socket.io/socket.io.js"></script>
    <script>
        var socket = io('http://192.168.4.3:4000');
        socket.on('connect', function() {
            alert('connected');
        });

        socket.on('data', function(msg) {
            document.getElementById('ptag').innerHTML = msg;
        });

        function lock() {
            socket.emit('lock', "lock");
        }

        function unlock() {
            socket.emit('unlock', "unlock");
        }

        function pin() {
            socket.emit('pin', "S" + (Math.floor(Math.random() * 90000000) + 10000000).toString());
        }
    </script>
</head>

<body>
    <h1>Smart Lock App</h1>
    <label>Status: </label>
    <label id="ptag"> --- </label>
    <br>
    <img src="http://192.168.4.3:8080/?action=stream" height="300" width="340" class="rotate180" />
            <br><br>
            <button onclick="lock()" class="button2">Lock</button>
            <br>
            <button onclick="unlock()" class="button1">Unlock</button>
            <br>
            <button onclick="pin()" class="button">Generate temporary pin</button>
            <br>
</body>

</html>

ArduinoMain.ino

Arduino
Arduino Code that has to be uploaded on ATmega 32U4 on LinkIt
#include <Bridge.h>
///////////////////////////////////
#include "U8glib.h"
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NO_ACK);
///////////////////////////////////
#include <HCMatrixKeypad.h>
#define DEBOUNCE 10
#define C1 9  /* DIO for keypad column 1 */
#define C2 10  /* DIO for keypad column 2 */
#define C3 11  /* DIO for keypad column 3 */
#define R1 5 /* DIO for keypad row 1 */
#define R2 6 /* DIO for keypad row 2. Delete if there is no row 2! */
#define R3 7 /* DIO for keypad row 3. Delete if there is no row 3! */
#define R4 8  /* DIO for keypad row 4. Delete if there is no row 4! */
HCMatrixKeypad Keypad(DEBOUNCE, C1, C2, C3, ROWMARKER, R1, R2, R3, R4); /* Uncomment for 3x4 keypad */
///////////////////////////////////
String KeyVal = "";
///////////////////////////////////
String C = "";
///////////////////////////////////
String Case = "Lock";
String StatusofLock = "LOCKEDX";
int Begin = 1;
///////////////////////////////////
String Pin = "";
String PinTemp = "*&()(&^";
///////////////////////////////////
String DoorState = "X";
int c1 = 0;
int pc1 = 2;
int c2 = 0;
int pc2 = 2;
int c3 = 0;
int pc3 = 2;
///////////////////////////////////
#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};
void setup()
{
  Serial1.begin(9600);
  //Bridge.begin();
  u8g.setColorIndex(1);
  u8g.firstPage();
  do {
    u8g.setPrintPos(18, 44);
    u8g.setFont(u8g_font_fub14);
    u8g.print("Booting");
  } while ( u8g.nextPage() );
  //Serial1.println("Starting");
  pinMode(12, INPUT);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  StatusofLock = "UNLOCKED";
}
void loop()
{
  c1 = !digitalRead(12);
  c2 = digitalRead(A0);
  c3 = digitalRead(A1);
  if (pc1 != c1 && Begin == 1) {
    if (c1 == HIGH) {
      Serial1.println("Open");
      u8g.firstPage();
      do {
        u8g.setPrintPos(18, 44);
        u8g.setFont(u8g_font_fub14);
        u8g.print("Open");
      } while ( u8g.nextPage() );
      StatusofLock = "Unlocked";
    }
    else if (c1 == LOW) {
      Serial1.println("Closed");
      delay(1000);
      u8g.firstPage();
      do {
        u8g.setPrintPos(18, 20);
        u8g.setFont(u8g_font_fub14);
        u8g.print("Enter pin");
        u8g.setPrintPos(18, 40);
        u8g.setFont(u8g_font_fub14);
        u8g.print("to");
        u8g.setPrintPos(18, 60);
        u8g.setFont(u8g_font_fub14);
        u8g.print("unlock");
      } while ( u8g.nextPage() );
      digitalWrite(13, HIGH);
      Serial1.println("Locked");
      StatusofLock = "Locked";
      Case = "Lock";
    }
    delay(500);
    pc1 = c1;
  }
  if (pc2 != c2 && Begin == 1) {
    if (c2 == HIGH) {
      u8g.firstPage();
      do {
        u8g.setPrintPos(18, 44);
        u8g.setFont(u8g_font_fub14);
        u8g.print("Unlocked");
      } while ( u8g.nextPage() );
      digitalWrite(13, LOW);
      Serial1.println("Unlocked");
      StatusofLock = "Unlocked";
    }
    else if (c2 == LOW) {
    }
    delay(500);
    pc2 = c2;
  }
  if (pc3 != c3 && Begin == 1) {
    if (c3 == HIGH) {
      Serial1.println("Bell");
      for (int thisNote = 0; thisNote < 8; thisNote++) {

        // to calculate the note duration, take one second
        // divided by the note type.
        //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
        int noteDuration = 1000 / noteDurations[thisNote];
        tone(4, melody[thisNote], noteDuration);

        // to distinguish the notes, set a minimum time between them.
        // the note's duration + 30% seems to work well:
        int pauseBetweenNotes = noteDuration * 1.30;
        delay(pauseBetweenNotes);
        // stop the tone playing:
        noTone(4);
      }
    }
    else if (c3 == LOW) {
    }
    delay(500);
    pc3 = c3;
  }
  while (Serial1.available() > 0) {
    char c = Serial1.read();
    if (c == '.') {
      String hum = C;
      hum.trim();
      //Serial1.println(hum);
      if (hum == "Begin") {
        delay(1000);
        Serial1.println("Begun");
        Begin = 1;
        delay(1000);
      }
      else if (hum == "Lock") {
        Case = "Lock";
        u8g.firstPage();
        do {
          u8g.setPrintPos(18, 20);
          u8g.setFont(u8g_font_fub14);
          u8g.print("Enter pin");
          u8g.setPrintPos(18, 40);
          u8g.setFont(u8g_font_fub14);
          u8g.print("to");
          u8g.setPrintPos(18, 60);
          u8g.setFont(u8g_font_fub14);
          u8g.print("unlock");
        } while ( u8g.nextPage() );
      }
      else if (hum.substring(0, 1) == "S") {
        Case = "Lock";
        PinTemp = hum.substring(1);
        u8g.firstPage();
        do {
          u8g.setPrintPos(18, 20);
          u8g.setFont(u8g_font_fub14);
          u8g.print("Enter temp");
          u8g.setPrintPos(18, 40);
          u8g.setFont(u8g_font_fub14);
          u8g.print("pin to");
          u8g.setPrintPos(18, 60);
          u8g.setFont(u8g_font_fub14);
          u8g.print("unlock");
        } while ( u8g.nextPage() );
        Serial1.println("Temp Pin set to " + String(PinTemp));
      }
      else if (hum == "LOCKIT") {
        Case = "Lock";
        u8g.firstPage();
        do {
          u8g.setPrintPos(18, 20);
          u8g.setFont(u8g_font_fub14);
          u8g.print("Enter");
          u8g.setPrintPos(18, 40);
          u8g.setFont(u8g_font_fub14);
          u8g.print("pin to");
          u8g.setPrintPos(18, 60);
          u8g.setFont(u8g_font_fub14);
          u8g.print("unlock");
        } while ( u8g.nextPage() );
        digitalWrite(13, HIGH);
        Serial1.println("Locked");
        StatusofLock = "Locked";
      }
      else if (hum == "UNLOCKIT") {
        u8g.firstPage();
        do {
          u8g.setPrintPos(18, 44);
          u8g.setFont(u8g_font_fub14);
          u8g.print("Unlocked");
        } while ( u8g.nextPage() );
        digitalWrite(13, LOW);
        Serial1.println("Unlocked");
        StatusofLock = "Unlocked";
        Case = "";
      }
      C = "";
    }
    else if (c != '\n' && c != '\r') {
      C += c;
    }
  }
  Keypad.Scan();
  if (Keypad.New_Key() && Begin == 1)
  {
    if (((Keypad.Read() / 10) == 1) && ((Keypad.Read() % 10) == 1)) {
      KeyVal = "1";
    }
    else if (((Keypad.Read() / 10) == 2) && ((Keypad.Read() % 10) == 1)) {
      KeyVal = "4";
    }
    else if (((Keypad.Read() / 10) == 3) && ((Keypad.Read() % 10) == 1)) {
      KeyVal = "7";
    }
    else if (((Keypad.Read() / 10) == 4) && ((Keypad.Read() % 10) == 1)) {
      KeyVal = "Backward";
    }
    else if (((Keypad.Read() / 10) == 1) && ((Keypad.Read() % 10) == 2)) {
      KeyVal = "2";
    }
    else if (((Keypad.Read() / 10) == 2) && ((Keypad.Read() % 10) == 2)) {
      KeyVal = "5";
    }
    else if (((Keypad.Read() / 10) == 3) && ((Keypad.Read() % 10) == 2)) {
      KeyVal = "8";
    }
    else if (((Keypad.Read() / 10) == 4) && ((Keypad.Read() % 10) == 2)) {
      KeyVal = "0";
    }
    else if (((Keypad.Read() / 10) == 1) && ((Keypad.Read() % 10) == 3)) {
      KeyVal = "3";
    }
    else if (((Keypad.Read() / 10) == 2) && ((Keypad.Read() % 10) == 3)) {
      KeyVal = "6";
    }
    else if (((Keypad.Read() / 10) == 3) && ((Keypad.Read() % 10) == 3)) {
      KeyVal = "9";
    }
    else if (((Keypad.Read() / 10) == 4) && ((Keypad.Read() % 10) == 3)) {
      KeyVal = "Ent";
    }
    //Serial1.println(KeyVal);
    if (Case == "Lock" && KeyVal != "Ent" && KeyVal != "Backward" && Pin.length() < 8) {
      Pin += KeyVal;
      u8g.firstPage();
      do {
        u8g.setPrintPos(18, 44);
        u8g.setFont(u8g_font_fub14);
        u8g.print(Pin);
      } while ( u8g.nextPage() );
      //Serial1.println(Pin);
    }
    else if (Case == "Lock" && KeyVal == "Ent" && Pin.length() < 9) {
      if (Pin == "12345678" || Pin == PinTemp) {
        u8g.firstPage();
        do {
          u8g.setPrintPos(18, 44);
          u8g.setFont(u8g_font_fub14);
          u8g.print("Unlocked");
        } while ( u8g.nextPage() );
        digitalWrite(13, LOW);
        Serial1.println("Unlocked");
        StatusofLock = "Unlocked";
        Case = "";
      }
      else if (Pin != "12345678" && Pin != PinTemp) {
        u8g.firstPage();
        do {
          u8g.setPrintPos(18, 44);
          u8g.setFont(u8g_font_fub14);
          u8g.print("Wrong Pin");
        } while ( u8g.nextPage() );
        Case = "Lock";
        Serial1.println("Wrong Pin");
        delay(3000);
        u8g.firstPage();
        do {
          u8g.setPrintPos(18, 20);
          u8g.setFont(u8g_font_fub14);
          u8g.print("Enter pin");
          u8g.setPrintPos(18, 40);
          u8g.setFont(u8g_font_fub14);
          u8g.print("to");
          u8g.setPrintPos(18, 60);
          u8g.setFont(u8g_font_fub14);
          u8g.print("unlock");
        } while ( u8g.nextPage() );
      }
      //Serial1.println(Pin);
      Pin = "";
    }
    else if (Case == "Lock" && KeyVal == "Backward" && Pin.length() < 9) {
      int I = Pin.length();
      Pin.remove(I - 1);
      u8g.firstPage();
      do {
        u8g.setPrintPos(18, 44);
        u8g.setFont(u8g_font_fub14);
        u8g.print(Pin);
      } while ( u8g.nextPage() );
      //Serial1.println(Pin);
    }
  }
}

Credits

Jayakarthigeyan Prabakar

Jayakarthigeyan Prabakar

3 projects • 16 followers
Rapid Prototyper | Product Developer | Systems Designer | IoT Specialist | Hardware Design Team Leader | Author | Web and App Developer
Thanks to Bhanu Kishore, Chaitanya, and Hariharan Prabhakar.

Comments