loading

Scared you've left the garage door open when you leave the house? Want to be notified of intruders when you're at work? How about a simple text or e-mail notification when someone enters a room, opens a safe full of valuables, or raids your private beer fridge?

Look no further! The Magnetic Door Spy system is exactly what you're looking for!

The Wi-Fi Magnetic Door Spy system is a an ESP8266 (arduino) board connected to a magnetic door sensor that constantly checks to see if the circuit (magnets) are closed. If they are, all is well. If not, it will fire off an alert to a cloud dashboard over your wireless network - which can be configured to send you a text, email, or a slew of other actions using the IFTTT platform.

This tutorial will walk you through the process of purchasing the parts you need, putting them together, uploading code to the board, then creating the alerts using Adafruit's IO Dashboard connected to IFTTT recipes.

Read on for details!

Credit

Original sketch code by Todd Treece @ Adafruit.com

Tweaked code & design by Josh King

Step 1: Materials Required

You will need the following to build a Magnetic Door Spy system:

  • Adafruit Huzzah ESP8266 Breakout Board - The brains of our project. (Link)
  • USB to TTL Serial Cable - Needed to communicate to the board. (Link)
  • Tiny Breadboard - What we'll plug everything into. (Link)
  • Magnetic Contact Switch - Sensor used to determine if something is open or closed. (Link)
  • 3.7V Lithium Ion Battery - I recommend a slightly bigger battery here. (Link)
  • Micro Lipo USB Charger - Used to charge/recharge our battery. (Link) (Or more expensive fancy one)
  • Some wire - Here's a good bundle if you don't have any. (Link)
  • 1M Ohm Resistors - needed for our design. (Link)
  • 220k Ohm Resistors - needed for our design. (Link)
  • Hammond Black Box - needed to enclose our design. (Link)
  • A soldering iron - One suited for electronics with an ST tip works best. (I use this one)
  • Rosin Core Solder - Just make sure it has rosin core/flux to prevent needless corrosion/oxidation. (Link)

While you technically don't NEED the following, I HIGHLY suggest you get these to make your life easier for this project (and most electronics projects in general):

  • Helping Hands - This will make your life a lot easier when soldering and connecting things (Cheap one) (Super fancy one)
  • Solder tip cleaning wire and holder - Makes it easy to brush off extra solder after every joint. Pretty cheap too. (Link)
  • Circuit Board Vise - I almost consider this a need, but not quite. Use this to make your life easier when soldering, wiring, or doing any kind of activity on your boards. (Link)
  • Solder Sucker - this makes it easy to remove excess solder or solder mistakes from boards. Super useful. (Link)

Step 2: Setting Up Arduino IDE Boards/Custom Libraries/USB Cable

This is not a how to use Arduino IDE guide. For that, I recommend you watch some YouTube tutorials on Arduino basics. However, I will walk you through downloading the necessary board code for the Adafruit Huzzah ESP8266, the libraries you need, and the actual sketch.

1. First, let's get the board code we need to work with the Adafruit Huzzah ESP8266 board. Go to File --> Preferences, and type "http://arduino.esp8266.com/stable/package_esp8266com_index.json" in the Additional Boards Manager URLs box.

2. Now, you should have the Adafruit HUZZAH ESP8266 board available to you in the Tools menu. Select this, and be sure to select USBTinyISP from Programmer, as this is usually NOT the default option. Take all of the options in the screenshot above.

3. Next up, go to Sketch --> Include Libary --> Manage Libraries. From here, search for "adafruit io arduino". DO NOT install the latest version, as it doesn't contain the needed functions for our sketch. Instead, install version 1.0.1 from the dropdown box (refer to screenshot)

4. Install the latest driver for the USB to TTL serial cable. You can find the drivers and information about the cable here. Once installed, plug the cable into your computer and select the appropriate port in the Arduino IDE (Tools --> Port). Check device manager (PC users) if you're unsure what port it's plugged into.


You are now ready to connect your battery and upload your code in the next step!

Step 3: Setting Up Your Adafruit Account + AIO Key

Our little device will be sending door and battery data constantly, so we'll need a way to monitor it. One of the best ways to do this, is to use Adafruit's IO dashboards/feeds. We'll need our own unique key to send from our device to Adafruit's cloud, so let's sign up for an account and set up our monitor feeds.

  1. Sign up for an account with Adafruit IO here, and grab your AIO key. Store this in a safe place, as we'll need it later for our sketch.
  2. Since you're here, go ahead and set up your feeds. Click on 'Your Feeds' to the left, then create a 'door' and 'battery' feed. Reference the screenshots above.

Now that we have our AIO key and our feeds set up, it's time to prep our board for programming!

Step 4: Connect Battery + Bootloader Mode + Upload Sketch

Now that we have our needed board code and library, it's time to connect our battery and upload our sketch to the ESP8266.

Unfortunately, the ESP8266 is a delicate board. It's amazing and powerful, but not as easy to work with as a traditional Arduino board, like the Uno. In order to connect to it properly, we will need to set ourselves up for success. Here are the steps to do that:

  1. We must provide stable external power while uploading our code. To do this, go ahead and solder wire to the GND and VBat pins on the ESP8266. Connect those to your breadboard. (refer to the wiring diagram, but don't worry about the other stuff just yet).
  2. Next, go ahead and plug wire from your lithium ion battery into the board as well. This should provide stable external power, which will be the first step in ensuring our sketch is uploading properly from our computer. You will see the ESP8266 board blink only briefly if you are successful, then go dim. This is completely normal.
  3. Next, you will need to put your ESP8266 into Bootloader mode - which will allow it to accept a sketch from the Arduino IDE. To do this, hold down GPIO 0 button on the board. With the button still held down, push the reset button once, and release it. Then, quickly release the GPIO 0 button. You should now see the light go dim on the red LED and stay lit. At this point, don't worry about rushing - it will NOT leave bootloader mode until powered off or reset.
  4. Connect your USB to TTL Serial Cable into the HUZZAH ESP8266. Reference screenshot above for appropriate pinouts.
  5. Download the door_spy.ino file from my github, and open it up with the previous board settings from the last step.
  6. At this point, make sure the device is in bootloader mode (dim red led). If not, ensure that it is before the next step.

  7. You will want to change the appropriate values in the sketch at this time. Change WLAN_SSID and WLAN_PASS, as well as AIO_KEY to match your appropriate network and adafruit account settings. These are case sensitive, so be sure to enter them correctly - with no extra spaces! Please note that the default battery checking interval is 5 minutes, and the default door check is 20 seconds. Feel free to change these or tweak them to your needs later on. Reference the screenshot above for fields you should change.

  8. Go ahead and upload the sketch. If you did everything correctly, you shouldn't encounter any errors.

You are now ready to wire everything up in the next step!

Step 5: Wiring + Enclosing

It's time to wire it up! TIP - drill a hole in your box, and move the door sensor wires inside the box before attaching to the breadboard. Follow the wiring diagram above.

  • Pin 16 to RST (this lets us use the low power mode)
  • Pin 13 to one side of door sensor
  • GND to opposite side of door sensor
  • VBat to the battery +
  • GND to battery -
  • GND to one side of the 220kΩ resistor
  • VBat to one side of the 1MΩ resistor
  • Pin A to the opposite side of the 1MΩ resistor and 220kΩ resistor

Step 6: Verify + Install

Now that we've got everything wired up, we'll want to verify our sexy new device is functioning. After that, we'll install our device and prepare to create trigger notifications!

  1. Connect your USB cable into the Huzzah ESP8266. In the Arduino IDE, connect to serial monitor. (Tools --> Serial Monitor). You should see the an output similar to the screenshot above. Take not that unless both sides of the door sensor magnets have made contact, it will sense as open and send the data to adafruit (value of 1). This will be used later to create our trigger for notifications. After the unit does a test, it will go to sleep until it's next test. This allows it to save a tremendous amount of battery, and prolong the time until our battery needs recharged.
  2. If everything is functioning appropriately, then go ahead and close up your box with the components secured inside (I used velcro, but it shouldn't move much regardless).
  3. Mount your device at the desired locations, and position the door sensors accordingly. I am monitoring my garage door - which you can see in the screenshot above.

Step 7: Set Up IFTTT Trigger + Alerts - YOU'RE DONE!

The final step is upon us!

Now that our device is in place and is sending data to our Adafruit dashboard feeds over our wireless network, we'll want to create a notification when certain requirements have been met.

  • Sign into IFTTT. Create an account if you don't have one (it's free).
  • Connect to the Adafruit channel with your newly created account credentials.
  • Connect to whatever you want to notify you - I chose SMS.
  • Create a recipe. For If, choose your Adafruit channel. Check the images above for door and battery details. For then, choose the details of how you want to receive your message. I've atttached an image of how I receive text message alerts.

THAT'S IT! Congratulations on your new completely wireless wi-fi using Magnetic Sensor HUZZAH ESP8266 device (mouthful isn't it!?). Err.....grats on your Magnetic Door Spy!

<p>Hi, great Instructable.</p><p>I am trying to have this for my fridge door. But i want the ESP to wake up from deepsleep only when the door is opened. (and ideally when it is closed also). </p><p>Would you have any suggestions/advice for that? also can you kindly share what the purpose of resistors is please?</p>
<p>I love it! Straight forward and simple!</p><p>I would love to have a notification when the door is closed again, can you help me with the code?</p><p>So basically what I would love to do is:</p><p>Keep it in deepsleep until the switch is interrupted.</p><p>As soon as the interruption is made, send the value to adafruit and ramp up the checking interval</p><p>As soon as the connection is closed again, send the changed value to adafruit and ramp down the checking interval to the initial one.</p><p>Can you help me with that? Thanks!</p>
<p>Hey there! To do what you're asking, you'll need to keep track of the state of the door. A de-bounce to drive that action would probably work. Here is a write-up on how something like that would work! CHEERS - and I'm so glad you like the project!<br>https://www.baldengineer.com/arduino-de-bounce-a-button-with-micros.html</p>
<p>Thanks!</p><p>I got it to work. Instead of De-bouncing, I am actually using the eeprom to write the last state (if the state has changed) and then compare the last state with the actual state. </p><p>This way I receive now a 0 if closed and a 1 if opened - but only, if it the value has changed.</p><p>Again, great and easy project, a perfect start to get into the ESP8266 board!</p>
<p>excellent! Wood you mind sharing your sketch or a copy paste of the code you used? I'm sure it would help others who want to accomplish this!</p>
<p>Of course!</p><p>Basically what it does is comparing the current door state with a previously stored one in the EEPROM.</p><p>To make sure the EEPROM is not written each and every time, it will only be written upon a change of the door state. </p><p>This is quick and dirty, but it works smooth. </p><p>I am actually using this to check if my vacuum cleaning robot has come back to his charging station. I do receive a text as soon as he starts his cleaning process and another one when he is back - sometimes he gets stuck under some furniture and that way I know if I need to look for him ;)</p><p>Thanks again for this!</p><p>Here ist the part of the code:</p><p>int door_status = EEPROM.read(5); // read out the saved value out of the EEPROM</p><p> if(digitalRead(PIN13) == LOW &amp;&amp; door_status == 1) </p><p> {</p><p> Serial.println(&quot;Door is closed.&quot;);</p><p> door_closed();</p><p> EEPROM.write(5, 0);</p><p> EEPROM.commit();</p><p> } </p><p> else if(digitalRead(PIN13) == HIGH &amp;&amp; door_status == 0) </p><p> {</p><p> // door is open so let's send something to Adafruit IO.</p><p> Serial.println(&quot;Door is open&quot;);</p><p> door_open();</p><p> EEPROM.write(5, 1);</p><p> EEPROM.commit();</p><p> } </p><p> else</p><p> // if nothing has changed, we do not do anything.</p><p> {</p><p> Serial.println(&quot;Nothing Changed.&quot;);</p><p> }</p><p> // we are done here. go back to sleep.</p><p> Serial.println(&quot;zzzz&quot;);</p><p> ESP.deepSleep(SLEEP_LENGTH * 1000000);</p>
<p>Thank you so much for sharing! :)</p>
A very nice instructable.
<p>Thank you sir! Glad you enjoyed :)</p>
<p>Hello, nice project. Do you know if any esp8266 compatible will work?</p><p>Any of these </p><p>http://www.banggood.com/search/esp8266/0-0-0-1-3-45-0-price-0-0_p-1.html?sortType=asc</p>
<p>Yes, any of them will work. Try and get one of the newer stand-alone boards though, not a shield (otherwise you'll need something like an arduino uno etc as well). <br><br>Also, make sure to adjust the pinouts appropriately. They are different on different models of boards. You will also need to possibly adjust the board type when using a non huzzah board.</p>
<p>WOW.</p><p>First, thank you for this quick reply.Is this one </p><p><a href="http://www.banggood.com/Geekcreit-Doit-NodeMcu-Lua-ESP8266-ESP-12E-WIFI-Development-Board-p-985891.html?rmmds=cart" rel="nofollow">http://www.banggood.com/Geekcreit-Doit-NodeMcu-Lua...</a> </p><p>a standalone? I think it is. I am newbie!</p><p>Second, I voted this project :) hope you win!! </p>
<p>Yup, that one will work! Thank you for voting that means a LOT to me! :)</p>
<p>Thank you! I really like this project!</p>
<p>Hey guys - if you're reading this tutorial and like it, please feel free to click the vote buttons! I'm trying really hard to win the IoT Builder competition in particular. I will use the prizes to continue making quality Instuctables like this one :) Thank you!</p>
<p>Thank you very much for the tip.</p>
<p>You're welcome! enjoy!</p>
<p>How long will it work on battery?</p>
<p>Depends on the battery size you use. Unfortunately it still uses about 20mA on transmit when waking from deep sleep, so it could be many months to a year or more if you use a battery big enough.</p>
<p>I'm concerned that the SSID, Username, and Password are not encrypted. </p>
<p>Hi there @RobertG179 - the traffic is firstly encrypted over your wifi - hopefully you're using WPA2. It is encrypted secondly by the IFTTT and Adafruit servers. Rest easy.</p>
<p>Good to know. I was a bit concerned after all the IoT vulnerabilities. </p>
<p>No problem - cheers!</p>
<p>Nice little tools and pics on how to do this. </p>
<p>Thanks glad you like it :)</p>
Very nice. Could be just what I need for the basis of a door/window monitoring system to protect my parrot from escapes. Do you think IFTTT could be used to send an alert only if a master door is opened? I'd like it to warn me when I open the birdcage door that e.g. a window is open that he could escape from. It doesn't need to alert me that a window is open when the birdcage is securely closed!
<p>You absolutely can use this for exactly that purpose. Good luck!</p>

About This Instructable

31,474views

329favorites

License:

Bio: It's dark. You are likely to be eaten by a grue.
More by esper2142:PiE-Ink Name Badge The Super Easy Micropython ESP8266 Windows Guide. No Guesswork Required! Illuminated Arrow Pen 
Add instructable to: