{"id":73,"date":"2016-08-15T00:52:12","date_gmt":"2016-08-15T05:52:12","guid":{"rendered":"https:\/\/blog.qianyurui.com\/?p=73"},"modified":"2016-08-15T01:24:31","modified_gmt":"2016-08-15T06:24:31","slug":"diy-locked-door-detector","status":"publish","type":"post","link":"https:\/\/blog.qianyurui.com\/index.php\/2016\/08\/15\/diy-locked-door-detector\/","title":{"rendered":"DIY Locked Door Detector"},"content":{"rendered":"<p>Did you lock the door today?<\/p>\n<p>Let me ask again: did you lock the door today?<\/p>\n<p>Are you sure you do not want to go back and check?<\/p>\n<p>&#8230;<\/p>\n<p>Welcome back. After few times waking up and finding the apartment door unlocked, I decided to do something to save myself from my carelessness. The idea is simple: building a locked door detection system that notifies me every time I forget to lock my door. A lovely schematics soon appeared on my napkin (or draw.io):<\/p>\n<p><a href=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/DoorDetection.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-79\" src=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/DoorDetection.png\" alt=\"DoorDetectionDiagram\" width=\"518\" height=\"424\" srcset=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/DoorDetection.png 518w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/DoorDetection-300x246.png 300w\" sizes=\"auto, (max-width: 518px) 100vw, 518px\" \/><\/a><br \/>\nA magnet is attached to the end of the deadbolt of my door lock. Inside the strike box is a hall effect sensor which detects the distance from the magnet. A micro-controller gathers the reading from the hall effect sensor, so now it knows whether the door is locked or not. If the door is not locked, the micro-controller would send\u00a0a notification to my phone. Since there is no power socket near the door, I am going to power the micro-controller by a chargeable battery.<\/p>\n<p>What a smart idea! As I submerged in my self-pride, a thread on a micro-controller community rescued me from drowning:<\/p>\n<p><a href=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/discussionFromParticle.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-76 size-full\" src=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/discussionFromParticle.png\" alt=\"discussionFromParticle\" width=\"776\" height=\"761\" srcset=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/discussionFromParticle.png 776w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/discussionFromParticle-300x294.png 300w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/discussionFromParticle-768x753.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Well, it seems someone brought the idea to table back in 2014&#8230;<\/p>\n<p>&nbsp;<\/p>\n<p>Anyway, I am going to build it, in a 2016 way.<\/p>\n<p>&nbsp;<\/p>\n<h3>Bill of Material<\/h3>\n<p>. Magnet tape<\/p>\n<p><a href=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/tape.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81\" src=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/tape.jpg\" alt=\"tape\" width=\"353\" height=\"450\" srcset=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/tape.jpg 353w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/tape-235x300.jpg 235w\" sizes=\"auto, (max-width: 353px) 100vw, 353px\" \/><\/a><\/p>\n<p>. Hall effect sensor &#8211; Notice that \u00a0as of today (08\/03\/2016) the only hall effect sensor on Sparkfun is a latching hall effect sensor (US1881) which is good for determining\u00a0the polarity of a magnet but not the\u00a0magnitude of magnetic field (which indicates the distance from magnet). I\u00a0bought\u00a0<strong>A1324LUA-T<\/strong> which is a linear hall effect sensor. As described by its datasheet, &#8220;the presence of a south-polarity magnetic field perpendicular to the branded surface of the package increases the output voltage from its quiescent value toward the supply voltage rail. The amount of the output voltage increase is proportional to the magnitude of the magnetic field applied. Conversely, the application of a north polarity field will decrease the output voltage from its quiescent value.&#8221; In short, if the south end of the magnet is always facing the sensor, as the distance between them decreases, the output voltage increases, and vice versa.<a href=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/A1324LUA-T.jpg\"><br \/>\n<\/a><\/p>\n<p><a href=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/hallEffectSensor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-107\" src=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/hallEffectSensor.png\" alt=\"hallEffectSensor\" width=\"629\" height=\"416\" srcset=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/hallEffectSensor.png 629w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/hallEffectSensor-300x198.png 300w\" sizes=\"auto, (max-width: 629px) 100vw, 629px\" \/><\/a><\/p>\n<p>. Micro-controller &#8211; Almost any major micro-controller can do the job, but for this project I am using Particle Photon because it has a built-in Wi-Fi module and easy-to-use cloud IDE, which is perfect to meet the design requirements.<\/p>\n<p><a href=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/photon.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-83\" src=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/photon.jpg\" alt=\"photon\" width=\"1920\" height=\"1080\" srcset=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/photon.jpg 1920w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/photon-300x169.jpg 300w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/photon-768x432.jpg 768w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/photon-1024x576.jpg 1024w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><\/p>\n<p>.\u00a0SparkFun Photon battery shield &#8211; Optional, as long as you know how to power Photon with a battery and how to charge the battery, you are good to go. I choose to use the battery shield just to make life easier.<\/p>\n<p><a href=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/batteryShield.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-88\" src=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/batteryShield.jpg\" alt=\"batteryShield\" width=\"600\" height=\"600\" srcset=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/batteryShield.jpg 600w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/batteryShield-150x150.jpg 150w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/batteryShield-300x300.jpg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>. Li-ion battery &#8211; I bought a 2000mAh battery with JST cable. When it comes to battery, bigger is better.<\/p>\n<p><a href=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/battery.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-89\" src=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/battery.jpg\" alt=\"battery\" width=\"600\" height=\"600\" srcset=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/battery.jpg 600w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/battery-150x150.jpg 150w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/battery-300x300.jpg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>. Fastener tape\/Double-sided tape<\/p>\n<p>. Wires<\/p>\n<p>The total cost is around $50, depending\u00a0on how many tech-savvy friends you have.<\/p>\n<p>&nbsp;<\/p>\n<h3>Wire them up<\/h3>\n<p>As shown in the schematic above, the wiring is very simple. Here is the step-by-step recipe:<\/p>\n<ol>\n<li>Set up the Photon. <a href=\"https:\/\/docs.particle.io\/guide\/getting-started\/start\/photon\/#connect-your-photon\">Here is the detailed instruction.<\/a><\/li>\n<li>Mount the Photon on the battery shield.<\/li>\n<li>Put a hall effect sensor on a table with the branded side\u2014the uneven side\u2014facing up. Connect the leftmost pin to the 3V3 pin on the Photon, the center\u00a0pin to the GND, the rightmost pin to the A0.<\/li>\n<li>Cut a small piece of magnet tape and paste it on the end side of the deadbolt.<br \/>\n<a href=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0816-e1471236996110.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-92\" src=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0816-e1471236996110.jpg\" alt=\"IMG_0816\" width=\"2448\" height=\"3263\" srcset=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0816-e1471236996110.jpg 2448w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0816-e1471236996110-225x300.jpg 225w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0816-e1471236996110-768x1024.jpg 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><\/li>\n<li>Stick one part of a fastener tape on the inside wall of the strike\u00a0box. Put the other part on the back of hall effect sensor. Press two parts of the fastener tape together. I bent the sensor legs to fit the wires in the strike box.<a href=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0815-e1471237501511.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-91\" src=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0815-e1471237501511.jpg\" alt=\"IMG_0815\" width=\"2448\" height=\"3263\" srcset=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0815-e1471237501511.jpg 2448w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0815-e1471237501511-225x300.jpg 225w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0815-e1471237501511-768x1024.jpg 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><a href=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0814-e1471237485567.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-90\" src=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0814-e1471237485567.jpg\" alt=\"IMG_0814\" width=\"2448\" height=\"3263\" srcset=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0814-e1471237485567.jpg 2448w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0814-e1471237485567-225x300.jpg 225w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0814-e1471237485567-768x1024.jpg 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><\/li>\n<li>Plug the battery in the battery shield. Done.<\/li>\n<\/ol>\n<p>I ended up getting this on my wall:<\/p>\n<p><a href=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0829-e1471237769987.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-93\" src=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0829-e1471237769987.jpg\" alt=\"IMG_0829\" width=\"2448\" height=\"3263\" srcset=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0829-e1471237769987.jpg 2448w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0829-e1471237769987-225x300.jpg 225w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0829-e1471237769987-768x1024.jpg 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><\/p>\n<p>I moved to a new apartment when I was building this, so the door in this picture is different from that in others.\u00a0One thing you can learn from this picture is that tape is real helpful \ud83d\ude1b . I am going to design a housing for this system and 3D print it out. Hopefully few weeks later I will not have this mess on \u00a0my wall.<\/p>\n<p>&nbsp;<\/p>\n<h3>A Little Test<\/h3>\n<p>To compare\u00a0the reading of the hall effect sensor when the door is locked to that\u00a0when the door\u00a0is unlocked, I flashed the Photon with Tinker and set pin A0 to &#8220;analogRead&#8221;. When the door is unlocked, the reading is around 2030, and when the door is locked, the reading is nearly 0. The difference in readings is significant enough, time to move on!<\/p>\n<p>&nbsp;<\/p>\n<h3>Coding Time<\/h3>\n<p>Not a big fan of coding? No problem. Feel free to copy and paste the code below.<\/p>\n<p>Start with Particle IDE. Create a new app and give it a cool name. The code below will do the magic.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">void setup() {\r\n pinMode(A0, INPUT); \/\/ set pin A0 as input\r\n}\r\n\r\nvoid loop() {\r\n int starttime = 0;\r\n int realtime = 0;\r\n int notification = 0;\r\n\r\n while (1) {\r\n int val = analogRead(A0); \/\/ get reading from A0, and store the value in val\r\n if (val &gt; 1900) { \/\/ when door is unlocked\r\n if (starttime == 0) { \/\/ if door is just unlocked\r\n starttime = Time.now(); \/\/ starttime is the time when door is unlocked\r\n } else {\r\n realtime = Time.now(); \/\/ realtime is the time now\r\n if ((realtime - starttime) &gt; 10 &amp;&amp; notification == 0) { \/\/ if door has been unlocked for more than 10 seconds\r\n Particle.publish( &quot;unlockedDoor&quot; ); \/\/ event &quot;unlockedDoor&quot; is published to Particle cloud\r\n notification = 1; \/\/ notification has been sent\r\n }\r\n }\r\n } else {\r\n starttime = 0;\r\n notification = 0;\r\n }\r\n delay(500); \/\/ loop every 0.5 seconds\r\n }\r\n}<\/pre>\n<p>What does it do? Every time when the door is unlocked, the Photon starts to count for 10 seconds. If the door is not locked within the 10 seconds, an event called &#8220;unlockedDoor&#8221; will be published to Particle cloud.<\/p>\n<p>I do not want to look at Particle console 24\/7 and wait for the event to appear. Instead, I want Particle to notify me when it sees the event. on my iPhone there is an app called Boxcar which is able to push notification. If somehow I can let Particle call Boxcar API&#8230; Introducing Webhook. Webhook can be created in Particle online console under &#8220;Integrations&#8221;. The setup should look like this:<\/p>\n<p><a href=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/webhook1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-99\" src=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/webhook1.png\" alt=\"webhook1\" width=\"795\" height=\"844\" srcset=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/webhook1.png 795w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/webhook1-283x300.png 283w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/webhook1-768x815.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><\/p>\n<p>Tada! Now if I forget to lock my door, this appears on my phone:<\/p>\n<p><a href=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0831.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100\" src=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0831.png\" alt=\"IMG_0831\" width=\"640\" height=\"1136\" srcset=\"https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0831.png 640w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0831-169x300.png 169w, https:\/\/blog.qianyurui.com\/wp-content\/uploads\/2016\/08\/IMG_0831-577x1024.png 577w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>[qrcode]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Did you lock the door today? Let me ask again: did you lock the door today? Are you sure you do not want to go back and check? &#8230; Welcome back. After few times waking up and finding the apartment door unlocked, I decided to do something to save myself from my carelessness. The idea &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.qianyurui.com\/index.php\/2016\/08\/15\/diy-locked-door-detector\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;DIY Locked Door Detector&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-73","post","type-post","status-publish","format-standard","hentry","category-tech"],"_links":{"self":[{"href":"https:\/\/blog.qianyurui.com\/index.php\/wp-json\/wp\/v2\/posts\/73","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.qianyurui.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.qianyurui.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.qianyurui.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qianyurui.com\/index.php\/wp-json\/wp\/v2\/comments?post=73"}],"version-history":[{"count":19,"href":"https:\/\/blog.qianyurui.com\/index.php\/wp-json\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":102,"href":"https:\/\/blog.qianyurui.com\/index.php\/wp-json\/wp\/v2\/posts\/73\/revisions\/102"}],"wp:attachment":[{"href":"https:\/\/blog.qianyurui.com\/index.php\/wp-json\/wp\/v2\/media?parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qianyurui.com\/index.php\/wp-json\/wp\/v2\/categories?post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qianyurui.com\/index.php\/wp-json\/wp\/v2\/tags?post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}