Dharmveer Bharti
Published

Secure Lock

Monitor your locker in real time. Get an SMS alert whenever the locker is opened. An IoT based add-on to the security of your commodities.

IntermediateFull instructions provided4 hours3
Secure Lock

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
IR proximity sensor
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
3 wires are needed to connect IR sensor with Bolt WiFi module
×1
Battery bank 5V 10,000mAh
Power banks with higher capacity are preferred.
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
Bolt IoT Android App
Bolt IoT Android App
SMS Messaging API
Twilio SMS Messaging API

Hand tools and fabrication machines

Tape, Double Sided
Tape, Double Sided

Story

Read more

Schematics

Connections

Code

CSV file initializing program

Python
Run this first to create data.csv
import csv

with open('data.csv',mode='a',newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['Action','Time'])

Monitoring program

Python
Python script for monitoring door state, saving into file and sending SMS
#conf file stores the credentials of the bolt device and twilio account
#datetime is used to get current system time
#all the records are stored in a file data.csv

import time, json, csv, conf
from datetime import datetime
from boltiot import Bolt, Sms

my_device = Bolt(conf.API_KEY,conf.DEVICE_ID)   #object for bolt device
sms = Sms(conf.SID,conf.AUTH_TOKEN,conf.TO_NUM,conf.FROM_NUM)   #object for sending sms

#setting initial state to 0, i.e. closed
previousState = 0
#'0' means door is closed and '1' means it's open
#current state is obtained each time and matched with previous state
#if they are different, then either the door is opened or is closed

print("Press Ctrl+C to quit")

while True:
    try:
        file = open('data.csv','a',newline='')  #opening file to store door open and door close time
        writer = csv.writer(file)
        reading = my_device.digitalRead('1')    #reading from digital pin 1
        data = json.loads(reading)
        currentState = int(data['value'])   #surrent state of door
        current = datetime.now().strftime("%d-%b-%Y %H:%M") #current time of system (dd-Mmm-yyyy HH:MM)

        if previousState==0 and currentState==1:
            #door was closed and now opened
            writer.writerow(["opened",current]) #store door opened time in file
            try:
                sms.send_sms("Door recently opened at: " + current) #send sms to phone
            except Exception as e:
                print("Message sending error:",e)
            
        if previousState==1 and currentState==0:
            #door was open and now closed
            writer.writerow(["closed",current]) #store door closed time in file
            
        previousState = currentState    #storing the current state
                                        #for comparision in the next iteration of loop
            
    except Exception as e:
        print("Exception raised: ",e)
    
    finally:
        file.close()    #this is required to see update in the file 'data.csv'
    
    time.sleep(5)   #wait for 5 seconds before next iteration of loop

User Interface webpage

HTML
Bolt 'product' code
To access device data from anywhere without linking your device to a 'product', simply set api_key and d_name (lines 26,27) to your device api key and device ID
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Secure Lock Check</title>
    <style>
        body{
            font-family: sans-serif;
            margin: 0;
        }
        h3{
            margin-top: 0px;
            margin-left: 2%;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        button{
            margin: 10px;
        }
        #banner{
            background-color: #424242;
            color: white;
        }
    </style>
    <script type="text/javascript">
        var api_key = "{{ApiKey}}";
        var d_name = "{{Name}}";
        var base_url = "https://cloud.boltiot.com/remote/";
        var message = "";
        
        function digitalRead(pin) {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    var obj = JSON.parse(xmlhttp.responseText);
                    if(obj.success=="1"){
                        if(obj.value=="1")
                            message = "Door is open!";
                        else if(obj.value=="0")
                            message = "Door is closed.";
                    }
                    else{
                        message = "error in reading, try again";
                    }
                }
            };
            xmlhttp.open("GET",base_url+api_key+"/digitalRead?pin="+pin+"&deviceName="+d_name,true);
            xmlhttp.send();
        }
        
        function getState()
        {
            var result = document.getElementById("state");
            message = "undefined, try again";
            digitalRead('1');    //reading from pin 1
            setTimeout( () => {result.innerHTML = message;}, 2000);
        }
    </script>
</head>

<body>
    <div id="banner"><h3>Secure Lock</h3></div>
    <p>This page lets you know the current state of your locker's door.</p>
    <p>Click the button below to know the live status:</p>
    <center>
        <button onclick="getState();">Check State</button>
        <br>
        <div id="state"></div>
    </center>
    <p id='pin-state'></p>
</body>
</html>

Credits

Dharmveer Bharti

Dharmveer Bharti

0 projects • 0 followers

Comments