Deadbolt Door Lock Monitor
Made by Stephen Nomura
Made by Stephen Nomura
Device that makes the current and historical locked/unlocked status of your front door available to view online in real-time.
Created: January 30th, 2016
My sister is forgetful, paranoid, and extremely busy. She is often rushed and scatterbrained when leaving for work in the morning. It is common for her to worry that she forgot to lock her front door. This will bug her throughout the day, reducing her ability to concentrate.
The primary aim of this project is to allay her fears: to provide her with a way to check if she locked the front door and make sure no one else has unlocked her door since her leaving. Furthermore, some mechanism to help prevent her from forgetting to lock the door is key.
Requirements are as follows:
- A subtle visual indication of whether the door is locked or not. This will help remind her to lock it and the change in indicator will help her remember that she did it.
- Real-time tracking of the state of the deadbolt from her phone.
- A log of every time the deadbolt is locked and unlocked, available from her phone.
I didn't have a hall effect sensor to work with at first, so I used a button to simulate the binary signal the sensor would provide.
I first worked on writing a decent function to communicate with the LED. I wanted something simple and reusable, so I modified an existing function to take a clean 6 digit hex value instead of a delimited string of binary values.
Next I wrote a separate function for monitoring the button. I set it up so when the button was pressed, the LED would change. I quickly noticed a problem -- the code was acting on the state of the button, not whether the button's state had changed. That is, holding the button down would send a flurry of continuous calls to the LED function. In response, I modified the function to only act on specific changes in state, rather than the state itself.
When a hall effect sensor was obtained, I replaced the button with the sensor and modified my code where relevant.
The website was written after the device was built.
Lastly, device code was optimized -- pulled it down from 100 lines to about 50!
The solution contains the following components:
- A Photon connected to a hall effect sensor and an RGB LED.
- A portable phone charger battery pack as a power source.
- A magnet attached to the manual deadbolt handle.
- A hosted website.
The Photon senses whether the lock is engaged or not by detecting the position of the lock handle using the hall effect sensor. When the lock is enabled, the LED glows red. When the lock is disabled, the LED glows green. In addition to being visible while inside the apartment, the LED is also visible from outside the door via the peephole. This provides visual feedback to help cue memory.
When a state change in the hall effect sensor is detected, a cloud variable is updated and a webhook calling a PHP script on my servers is called. This script records the lock's state change in a log file.
The website displays the current status of the lock as well as the log file of events. This uses a recurring AJAX call to keep the information updated in real time -- no browser refresh needed. The call to Particle's servers uses a PHP proxy so that tolkens are not exposed in client-side code.
This project was a fun introduction to the Arduino language and connecting physical sensors to web services. I gained experience enabling communication between a Photon, web server PHP, and client-side Javascript.
In future versions, I would work on the following:
- Improve the device to make it less intrusive and more durable: enclosure, mounting, sensors, status LED. Right now, it's fairly fragile and the door opening and closing carries enough force to knock the circuit loose. Additionally, it is an eyesore -- an enclosure would help with this.
- Improved battery life. The current setup is not optimized to reduce power consumption. Furthermore, a better battery pack could be installed.
- Add a sensor for detecting when the door itself is opened and closed, which would allow knowing if anyone has actually entered the apartment in the event that it was left unlocked.
- More advanced and meaningful data presentation online, rather than just a stream of events.
- Push notifications.
- Better security for the online information.
/*
Door Monitor
Uses a sensor to monitor the locked/unlocked status of
a door, lock, or other mechanism. An LED displays the
current status -- red for locked and green for unlocked.
When change is detected, it is recorded online.
* RGB LED on pins D0, D1, D2
* Hall Effect Sensor on pin D4
@author Stephen Nomura
@version 1.0 2016-01-29
*/
/* CONSTANTS & VARIABLES
------------------------------------------------------*/
int rgbLedPins[] = { D0, D1, D2 };
int sensorPin = D4;
int sensorState;
int sensorStatePrev = -1;
/* SETUP
------------------------------------------------------*/
void setup(){
pinMode(rgbLedPins[0], OUTPUT);
pinMode(rgbLedPins[1], OUTPUT);
pinMode(rgbLedPins[2], OUTPUT);
pinMode(sensorPin, INPUT);
Particle.variable("lockState",sensorState);
}
/* LOOP
------------------------------------------------------*/
void loop(){
monitorHallSensor();
delay(1000);
}
/* FUNCTIONS
------------------------------------------------------*/
void monitorHallSensor(){
sensorState = digitalRead(sensorPin);
if(sensorState != sensorStatePrev){ // only trigger on change
if(sensorState == LOW){ // locked
setRgbLedByHex("FF0000", rgbLedPins);
}
else { // unlocked
setRgbLedByHex("00FF00", rgbLedPins);
}
Particle.publish("lock_record_state");
}
delay(50);
sensorStatePrev = sensorState;
}
int setRgbLedByHex(String hex, int pins[]){
// error handling
if( hex.length() != 6 ) return -1;
// split hex strings, convert to decimal, invert, send to LED
analogWrite( pins[0], 255 - strtol(hex.substring(0,1),NULL,16) );
analogWrite( pins[1], 255 - strtol(hex.substring(2,3),NULL,16) );
analogWrite( pins[2], 255 - strtol(hex.substring(2,3),NULL,16) );
return 1;
}
Click to Expand
<!DOCTYPE html>
<html>
<head>
<script charset="utf-8" src=
"http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type=
"text/javascript">
</script>
<style>
body {
background-color:#fff;
margin:0;
padding:0;
}
/* CENTERED HEADER */
.dead-center {
position: fixed;
top: 50%;
left: 50%;
width: 300px;
height: 108px;
margin-left: -150px;
margin-top: -54px;
z-index:1;
}
h1 {
color:#fff;
text-align:center;
font-size:xx-large;
font-family:helvetica;
}
#log {
position:absolute;
margin: 20px;
}
</style>
<title></title>
</head>
<body>
<pre id="log">
</pre>
<div class="dead-center">
<h1 id="ledState"></h1>
</div>
<script type="text/javascript">
var CORE_ID = '1c0035001347343339383037';
$(document).ready(function() {
updateLedState();
});
setInterval(function(){
updateLedState();
updateLog();
}, 1000);
function updateLedState(){
$.getJSON('./proxy.php?'+CORE_ID+'/lockState', function(response) {
if(response.result == 1){
$("#ledState").html("UNLOCKED");
$("body").css("background-color","#50E3C2");
}
else if(response.result == 0){
$("#ledState").html("LOCKED");
$("body").cscs("background-color","#D0021B");
}
else{
$("#ledState").html("ERROR");
$("body").css("background-color","#F5A623");
}
});
}
function updateLog(){
$.get( './history.txt', function(response){
$("#log").html(response);
});
}
</script>
</body>
</html>
Click to Expand
This course charts the emergence of the now "connected world" to explore the possibilities for future products and connected spaces.
Device that makes the current and historical locked/unlocked status of your front door available to view online in real-time.