{"id":386,"date":"2018-04-03T13:31:15","date_gmt":"2018-04-03T17:31:15","guid":{"rendered":"http:\/\/www.thesmarthomehookup.com\/test_install\/?p=386"},"modified":"2023-02-08T17:22:32","modified_gmt":"2023-02-08T22:22:32","slug":"5-diy-wireless-mqtt-smart-home-window-sensors-updated","status":"publish","type":"post","link":"http:\/\/www.thesmarthomehookup.com\/test_install\/5-diy-wireless-mqtt-smart-home-window-sensors-updated\/","title":{"rendered":"$5 DIY Wireless MQTT Smart Home Window Sensors (Updated)"},"content":{"rendered":"<p><iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/BoYVr2UwWWg\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen=\"\"><\/iframe><br \/>\n<iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/2kLZ7DlP9KU\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen=\"\"><\/iframe><\/p>\n<p><strong>Window Sensors:<\/strong><\/p>\n<p>Back in the 1990s and early 2000s every new house got a wired security system installed in it basically for free.&nbsp; The idea was that the security companies would easily make their money back with the monthly monitoring fee so they could take a bit of upfront cost.&nbsp; Fast forward to 2018 and most monitoring companies are utilizing wireless sensors instead of installing wired systems in new houses and the days of free installation are gone.<\/p>\n<p>My house was built in 2012 and adding a security system was going to add upwards of $1500 to the cost of the build. At the time we didn\u2019t have a whole lot of money to spare, so we passed.&nbsp; I\u2019d love to go back in time and tell my past self to install as much wiring in the walls as possible, but c\u2019est la vie.<\/p>\n<p>I\u2019ve now got various wired microcontrollers around the house monitoring the outside doors, but I was struggling with a solution for the windows.&nbsp; I don\u2019t use a home automation hub for zigbee or zwave protocol (although I probably should), and I didn\u2019t really want to open up the bag of worms that is the xiaomi system.&nbsp; This left me with very few options but to build my own solution.&nbsp; So I did!&nbsp; Total cost for each sensor is just over $5 if you buy the parts from amazon, you can build them for under $3 each if you buy your stuff from aliexpress\u2026 I was too impatient to wait for the shipping.<\/p>\n<p>Lets look at the design.<\/p>\n<p>This reed switch here is a normally closed switch, meaning it only breaks the circuit when it\u2019s close to the magnet.&nbsp; This reed switch connects directly to the ground pin on the ESP-01 and the negative terminal of the CR2032.&nbsp; The positive terminal of the CR2032 is connected to the 3.3V pin on the ESP-01, which is jumped with a 10k pullup resistor to the enable pin on the ESP-01 pulling it high, which is required for normal operation.&nbsp; All this stuff sits in a small 3d printed package.&nbsp; By the way, I also 3d printed the magnet enclosures, I\u2019m using those small bucky ball magnets to trigger my switch, but almost any magnet will work.<\/p>\n<p>When a bad guy opens the window they move the reed switch away from the magnet which closes the switch and provides a ground to the ESP-01, turning it on.&nbsp; The ESP boots up, connects to your wifi, and sends an \u201copen\u201d MQTT message to a window status topic before putting itself into a deep sleep for an hour (which is actually forever, because I didn\u2019t make the extra solder connection for it to be able to wake up from deep sleep.&nbsp; I\u2019ve also set the last will and testament in this program to publish a \u201cclosed\u201d mqtt message with a retain flag after the ESP-01 disconnects from your MQTT server, which it will about 5 seconds after it sends the original open message.<\/p>\n<p>Here\u2019s a quick overview of the build:<\/p>\n<ol>\n<li>Load up my Arduino sketch, put in your wifi information and mqtt information. Remember to set a unique identifier for the MQTT client and MQTT topic, don\u2019t forget what you call your topics, write them down.<\/li>\n<li>Flash the program to your ESP-01 using a serial to UART adapter, hook up the pins according to this diagram.<\/li>\n<li>Desolder the pins from the ESP-01 by pushing them through from the top using a hot soldering iron. Once they are pushed through you can pull them out individually with a pair of nippers<\/li>\n<li>Cut off the normally open leg of your reed switch and bend the normally closed leg so it will sit under the battery, bend the other lead so it can contact the ground pin.<\/li>\n<li>Place a high value resistor (I used a 10k) between the Vin pin and the enable pin<\/li>\n<li>Solder a thin wire onto the Vin pin and place it so it will make contact with the positive side of the CR2032. I put a dab of solder on the end of my wire to give it a slightly larger point of contact.<\/li>\n<li>Put in the battery and install the top and you\u2019re all set.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>Lets talk about the disadvantages of this solution:<\/p>\n<ol>\n<li>The time from power on until the sensor message hits home assistant is just under 4 seconds, which Is a little slow, but given their placement it would take a very skilled thief to be able to disable the chip before the message was sent.<\/li>\n<li>The CR2032 battery starts out at around 3.2V fresh out of the package. The ESP-01 requires at least 3V to boot and establish a wifi connection. This translates to around 20 open messages before the battery is drained beyond the point where the wifi radio can operate. For me this is no big deal, because in the 6 years we\u2019ve been in this house we\u2019ve opened each window significantly less than 20 times, if you open and close your windows a lot, or you leave your windows open then these sensors will probably not work out for you.<\/li>\n<li>Out of the 10 ESP-01s that I bought 1 of them was unusually power hungry and refused to boot up on the CR2032, it worked no problem with my 3.3V power supply. Even after removing the onboard LED it still was too current hungry to connect to wifi, probably just a manufacturing defect.<\/li>\n<\/ol>\n<p>Okay, I just told you why you shouldn\u2019t build these things, lets talk about why you should:<\/p>\n<ol>\n<li>They are dirt cheap, at $3-5 per sensor we\u2019re talking about outfitting your whole house for the cost of a single zigbee or zwave window sensor.<\/li>\n<li>They are wifi, so no hub is required. My biggest problem with wifi is wifi congestion, but these devices are going to be powered off 99.9% of the time, so they will not contribute to the congestion in my house.<\/li>\n<li>The battery is disconnected when the window is closed, no deep sleep, no micro current draw, it\u2019s an open circuit. This means your CR2032 batteries should last years and years as long as the window is not opened. I made my first one of these less than 2 months ago so I don\u2019t have year long data, but the engineer in me says the battery should last as long as it would in an unopened package.<\/li>\n<\/ol>\n<p>Links to everything needed for this project are in this description below, that includes amazon links, the Arduino sketch, and the STL file for the 3d printed enclosure.<\/p>\n<p><strong>Window Sensor Updates<\/strong><\/p>\n<p>I love building stuff, I love coding, and I love sharing my projects with other people.&nbsp; I know I\u2019m not going to get it 100% perfect every time on the first time, so I also really like hearing feedback about the faults in my projects.&nbsp; Today on the hookup we\u2019re going to look at some of the feedback from my $5 MQTT window sensor video and how I addressed those issues.<\/p>\n<p>The first suggestion was from youtube users SornDP and Marcin Domanski (sorry if I butchered your name).&nbsp; They recommended using a static IP address instead of DHCP to knock a few seconds off the response time.&nbsp; Here\u2019s how I implemented it.&nbsp; When I flash my program for the first time I comment out these 5 lines of code, this makes the ESP connect to wifi with DHCP and stay connected.&nbsp; I then go into my router and reserve the IP address for this device.&nbsp; After that IP is reserved, I go back and input the reserved IP into the user configuration section and uncomment the other lines of code.&nbsp; Just as Sorn and Marcin suggested, this change reduces the time to send the MQTT message by about 50%.&nbsp; Causing the \u201copen\u201d mqtt message to come through in an average of just under 2 seconds. Thanks guys.<\/p>\n<p>The next criticism came from reddit user largo_al_factotum who noted that there was no good way to tell the status of the batteries.&nbsp; That was a valid concern, and one that was fairly easy to fix.&nbsp; I changed the code so the very first thing the program does it measure the voltage on the VCC pin.&nbsp; I compared the numbers I was getting with the zero load battery readings from my multimeter and determined that the VCC voltage function returns a voltage about 600 millivolts below the actual value, this is most likely due to the fact that the wifi radio puts a decent load on the battery while the VCC voltage is being measured. Anyways, I went ahead and just added 600 to the value of the VCC millivoltage to make the numbers easier to understand.<\/p>\n<p>In my tests I determined that using static IP and replacing the enable pin resistor with a straight wire allowed the window to send MQTT messages all the way down to an unloaded 2700 millivolts.&nbsp; To help with largo_al_factotum\u2019s issue I changed the code so if the VCC voltage is under 2900 millivolts the last will and testament message changes to include the current mV of the battery and the message \u201cReplace Battery\u201d instead of just saying \u201cclosed\u201d.<\/p>\n<p>With these new changes to the code and the wiring you can get about 15 open\/closed messages between the time your window starts telling you to replace the battery and when it stops working completely.&nbsp; This should be plenty of time for you to throw a new coin cell in.<\/p>\n<p>Lets review the changes:<\/p>\n<ol>\n<li>Static IP Change lowers the response time from 4 seconds to 2 seconds.<\/li>\n<li>Switching the IP to static and changing the enable resistor to a straight wire significantly decreases the minimum voltage required in the battery to operate. My tests using a brand new battery yielded approximately 110 open\/closed messages until a message was no longer sent, with the last 15 messages sending the \u201creplace battery\u201d message.&nbsp; Also note that these were consecutive boots, and after letting the battery sit unused for 15 minutes the voltage rebounded enough to send another message after the initial failure.<\/li>\n<li>Battery voltage is no longer a mystery, you can be sure that if your MQTT message says \u201cclosed\u201d that your battery will have enough juice to send another message.<\/li>\n<\/ol>\n<p>These improvements were so significant that I actually went through and updated all 6 of my windows to the new software, it\u2019s not super fun reflashing an ESP-01 that\u2019s had the headers removed, but it\u2019s actually not too bad once you\u2019ve done it a few times.&nbsp; I just tin some jumper cables and tack them on quickly with a soldering iron, this makes them really easy to remove once the new program is added<\/p>\n<p>Thanks to everyone who commented and gave suggestions and I hope these changes are enough to calm some of the reservations that were expressed about these DIY sensors.&nbsp; If you enjoyed this video, please consider subscribing, and as always, thanks for watching the hookup.<\/p>\n<h3>Links:<\/h3>\n<p>Bare Reed Switches:<a href=\"https:\/\/amzn.to\/2qJ7Wcg\"> https:\/\/amzn.to\/2qJ7Wcg<\/a><br \/>\nESP-01 Microcontrollers: <a href=\"https:\/\/amzn.to\/2HJ8xTo\">https:\/\/amzn.to\/2HJ8xTo<\/a><br \/>\nCR2032 Batteries: <a href=\"https:\/\/amzn.to\/2JWVgY1\">https:\/\/amzn.to\/2JWVgY1<\/a><br \/>\nResistors: <a href=\"https:\/\/amzn.to\/2J67k8c\">https:\/\/amzn.to\/2J67k8c<\/a><br \/>\nSoldering Iron: <a href=\"https:\/\/amzn.to\/2JUP3Mm\">https:\/\/amzn.to\/2JUP3Mm<\/a><br \/>\nUSB to TTL Adapter: <a href=\"https:\/\/amzn.to\/2Hak9BW\">https:\/\/amzn.to\/2Hak9BW<\/a><br \/>\n3d Printer: <a href=\"https:\/\/amzn.to\/2vqMpuz\">https:\/\/amzn.to\/2vqMpuz<\/a><br \/>\nBucky Ball Magnets: <a href=\"https:\/\/amzn.to\/2vqMBdh\">https:\/\/amzn.to\/2vqMBdh<\/a><\/p>\n<p>Arduino code on github: <a href=\"https:\/\/github.com\/thehookup\/MQTT_Window_Sensors\/blob\/master\/Window_CONFIGURE.ino\">https:\/\/github.com\/thehookup\/MQTT_Window_Sensors\/blob\/master\/Window_CONFIGURE.ino<\/a><\/p>\n<p>STL Files:<br \/>\nMain Case: <a href=\"https:\/\/github.com\/thehookup\/MQTT_Window_Sensors\/blob\/master\/window_back.stl\">https:\/\/github.com\/thehookup\/MQTT_Window_Sensors\/blob\/master\/window_back.stl<\/a><br \/>\nCase Cover: <a href=\"https:\/\/github.com\/thehookup\/MQTT_Window_Sensors\/blob\/master\/window_front.stl\">https:\/\/github.com\/thehookup\/MQTT_Window_Sensors\/blob\/master\/window_front.stl<\/a><br \/>\nMagnet Holder: <a href=\"https:\/\/github.com\/thehookup\/MQTT_Window_Sensors\/blob\/master\/window_magnet.stl\">https:\/\/github.com\/thehookup\/MQTT_Window_Sensors\/blob\/master\/window_magnet.stl<\/a><\/p>\n<p>Support my channel:<br \/>\nPatreon:&nbsp;<a href=\"https:\/\/www.patreon.com\/thehookup\">https:\/\/www.patreon.com\/thehookup<\/a><\/p>\n<p>Music by BenSound.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Window Sensors: Back in the 1990s and early 2000s every new house got a wired security system installed in it basically for free.&nbsp; The idea was that the security companies would easily make their money back with the monthly monitoring fee so they could take a bit of upfront cost.&nbsp; Fast forward to 2018 and [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2617,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-386","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-projects"],"acf":[],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/comments?post=386"}],"version-history":[{"count":4,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/386\/revisions"}],"predecessor-version":[{"id":2372,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/386\/revisions\/2372"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media\/2617"}],"wp:attachment":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media?parent=386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/categories?post=386"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/tags?post=386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}