Introduction: Wi-Fi Magnetic Door Spy
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!
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.
- 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.
- 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:
- 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).
- 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.
- 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.
- Connect your USB to TTL Serial Cable into the HUZZAH ESP8266. Reference screenshot above for appropriate pinouts.
- Download the door_spy.ino file from my github, and open it up with the previous board settings from the last step.
At this point, make sure the device is in bootloader mode (dim red led). If not, ensure that it is before the next step.
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.
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!
- 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.
- 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).
- 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!