$5 DIY Wireless MQTT Smart Home Window Sensors (Updated)

Window Sensors:

Back in the 1990s and early 2000s every new house got a wired security system installed in it basically for free.  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.  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.

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’t have a whole lot of money to spare, so we passed.  I’d love to go back in time and tell my past self to install as much wiring in the walls as possible, but c’est la vie.

I’ve now got various wired microcontrollers around the house monitoring the outside doors, but I was struggling with a solution for the windows.  I don’t use a home automation hub for zigbee or zwave protocol (although I probably should), and I didn’t really want to open up the bag of worms that is the xiaomi system.  This left me with very few options but to build my own solution.  So I did!  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… I was too impatient to wait for the shipping.

Lets look at the design.

This reed switch here is a normally closed switch, meaning it only breaks the circuit when it’s close to the magnet.  This reed switch connects directly to the ground pin on the ESP-01 and the negative terminal of the CR2032.  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.  All this stuff sits in a small 3d printed package.  By the way, I also 3d printed the magnet enclosures, I’m using those small bucky ball magnets to trigger my switch, but almost any magnet will work.

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.  The ESP boots up, connects to your wifi, and sends an “open” MQTT message to a window status topic before putting itself into a deep sleep for an hour (which is actually forever, because I didn’t make the extra solder connection for it to be able to wake up from deep sleep.  I’ve also set the last will and testament in this program to publish a “closed” 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.

Here’s a quick overview of the build:

  1. 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’t forget what you call your topics, write them down.
  2. Flash the program to your ESP-01 using a serial to UART adapter, hook up the pins according to this diagram.
  3. 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
  4. 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.
  5. Place a high value resistor (I used a 10k) between the Vin pin and the enable pin
  6. 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.
  7. Put in the battery and install the top and you’re all set.

 

Lets talk about the disadvantages of this solution:

  1. 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.
  2. 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’ve been in this house we’ve 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.
  3. 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.

Okay, I just told you why you shouldn’t build these things, lets talk about why you should:

  1. They are dirt cheap, at $3-5 per sensor we’re talking about outfitting your whole house for the cost of a single zigbee or zwave window sensor.
  2. 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.
  3. The battery is disconnected when the window is closed, no deep sleep, no micro current draw, it’s 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’t have year long data, but the engineer in me says the battery should last as long as it would in an unopened package.

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.

Window Sensor Updates

I love building stuff, I love coding, and I love sharing my projects with other people.  I know I’m 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.  Today on the hookup we’re going to look at some of the feedback from my $5 MQTT window sensor video and how I addressed those issues.

The first suggestion was from youtube users SornDP and Marcin Domanski (sorry if I butchered your name).  They recommended using a static IP address instead of DHCP to knock a few seconds off the response time.  Here’s how I implemented it.  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.  I then go into my router and reserve the IP address for this device.  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.  Just as Sorn and Marcin suggested, this change reduces the time to send the MQTT message by about 50%.  Causing the “open” mqtt message to come through in an average of just under 2 seconds. Thanks guys.

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.  That was a valid concern, and one that was fairly easy to fix.  I changed the code so the very first thing the program does it measure the voltage on the VCC pin.  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.

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.  To help with largo_al_factotum’s 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 “Replace Battery” instead of just saying “closed”.

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.  This should be plenty of time for you to throw a new coin cell in.

Lets review the changes:

  1. Static IP Change lowers the response time from 4 seconds to 2 seconds.
  2. 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 “replace battery” message.  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.
  3. Battery voltage is no longer a mystery, you can be sure that if your MQTT message says “closed” that your battery will have enough juice to send another message.

These improvements were so significant that I actually went through and updated all 6 of my windows to the new software, it’s not super fun reflashing an ESP-01 that’s had the headers removed, but it’s actually not too bad once you’ve done it a few times.  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

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.  If you enjoyed this video, please consider subscribing, and as always, thanks for watching the hookup.

1 thought on “$5 DIY Wireless MQTT Smart Home Window Sensors (Updated)”

  1. I sure would love to see you adapt this to a battery powered motion sensor. I’ve tried but I just don’t have your knowledge or skill set. For all the places I want to use motion sensors, it’s really impractical to get AC to them. (For some reason, the missus doesn’t like having extension cords dangling from the ceiling!) I can’t be the only one with this predicament; seems like you’d be doing the community a solid…

Leave a Reply

Your email address will not be published. Required fields are marked *