$4 WiFi Door Alarm Using a ESP8266 #IoT

64,048

630

140

Posted

Introduction: $4 WiFi Door Alarm Using a ESP8266 #IoT

Sensors Contest 2016

Runner Up in the
Sensors Contest 2016

Internet of Things Contest 2016

Second Prize in the
Internet of Things Contest 2016

In this IoT project I will show you how to make a WiFi door alarm that alerts you wherever you are!

I had his idea of designing a wifi enabled door alarm after a friend's house was broken into.

Here is a quick overview of how it works: we reuse the reed sensor of a door alarm and hook it to the reset pin of a wifi module. When the door opens the magnet enter in the detection zone of the reed sensor, it wakes up the wifi module which runs a script that sends an event to your IFTTT.com account, and goes back to sleep. IFTTT do its magic to send you an email, a SMS or any alert you previously configured.

This alarm can potentially be placed on any kind of door.

This Instructables is quite long because I tried to detail the steps the most I could. Don't be afraid, it's actually simpler than it looks! :)

A software update is also available for this Instructable: IoT Door Alarm UPGRADED.

Step 1: Spend a Bit Less Than 4 Bucks

Material list

  • A wifi module: ESP8266 (I recommend using the ESP-07 but it can be any version) < $2.50 on ebay.
  • A cheap door alarm that works with 2 AAA batteries < $1.50 on ebay.

The ESP8266 is a very cheap'n'small wifi module. It has awesome capabilities, you can use the onboard microprocessor to run your own program so you don't need an additional one like an Arduino or a RaspPi! Also its low power consumption allows it to run on 2 AAA batteries.

We are using a cheap door alarm because it provides the adequate enclosure with batteries holder, on/off switch and of course a reed sensor.

You will also need the folowing tools:

  • A computer
  • Regular soldering tools
  • Screwdrivers
  • Wires
  • An Arduino Uno* or a USB-FTDI module for flashing the ESP8266 firmware

* with a DIP chip so it can be removed

Step 2: Connect the ESP8266

You will need a 3.3V FTDI or an Arduino (removing the microprocessor from its socket) for flashing the ESP8266 firmware.

Connect the ESP8266 to your Arduino as described in the diagram and in this table:

ESP8266 pinFTDI pinArduino pin
RxTxRx
TxRxTx
GNDGNDGND
GPIO15*GNDGND
GPIO0**GNDGND
GPIO2*3.3V3.3V
VCC3.3V3.3V
EN (CH_PD)3.3V3.3V

* sometimes those pins are not available depending on you ESP8266 version, if so ignore them.
** this pin is connected to ground only for flashing purpose. Disconnect it once done with flashing.

Step 3: Flash the Basic ESP8266 Firmware

Here is the tricky part!

There are multiple, sometimes tedious, steps to follow but don't worry, you have to do this only once and this is because I detailed a lot!

Here are the steps to flash the Basic firmware into your ESP8266, using Linux, Mac or Windows:

  • Linux and Mac:
    1. Install EspTool following "Installation / dependencies" section here.
    2. Download the awesome ESP8266 Basic firmware here and unzip the file.
    3. Plug the FTDI or the Arduino to your computer and test the connection:
      • In a terminal run:
        ls /dev/tty*usb*

        and look for the name of your USB port.

      • Replace tty.usbserial-A50285BI with your USB port name and run:
        sudo python esptool.py --port /dev/tty.usbserial-A50285BI flash_id
    4. If everything went well you should see something like this:
      Connecting...
      Manufacturer: e0
      Device: 4014
    5. In the folder Flasher/Build/ of the ESP8266 Basic you downloaded previously you should see several folders: 4M, 2M, 1M, 512k. Those represent the firmware for several memory configuration of the ESP8266 board.
      Upload the Basic firmware to your ESP8266 running:
      sudo python esptool.py --port /dev/tty.usbserial-A50285BI write_flash 0x00000 path/to/Flasher/Build/4M/ESP8266Basic.cpp.bin

      Replace path/to/Flasher/Build/4M/ESP8266Basic.cpp.bin with the correct path.
      If you know the memory configuration of your board replace 4M with the correct value. If you don't we can guess it doing a test later.

    6. Wait 30 seconds and on your computer or any wifi device look for a WiFi AP called ESP and pair with it. Open the url http://192.168.4.1/filemng in a browser. You should see a list of .dat and .bin files. If you don't, it means that you uploaded the firmware with the wrong memory size (4M in place of 1M for instance). In that case unplug and replug the USB and return to step 5 using the next lower memory value.
    7. Click on the [SETTINGS] link. You should see the Basic GUI stored inside ESP8266 which allows you to configure and program it. Connect it to your local wifi network filling the Station Mode fields (see documentation here) and press the Save button.
    8. Unplug the FTDI or Arduino from your computer
    9. Remove the connection of GPIO0 to GND
    10. Replug the FTDI or Arduino to your computer and after less than 6 seconds run the command:
      screen /dev/tty.usbserial-A50285BI
      If you are lucky you should see the ESP8266 IP address:
      1
      2
      3
      ...
      6
      
      Connected to you_AP_SSID
      IP Address : 192.168.3.x
      WIFIname
      WIFIpass
      file open failed
    11. Remember it and try to connect to this IP address with a web browser, you should see the same GUI.
    12. It is recommended to configure your router so the DHCP server associate a fix IP address to your ESP8266 (see your router's documentation).
  • Windows:
    1. Go to the Basic page and download the appropriate flasher for your operating system.
    2. Open a terminal (cmd) and enter the PowerShell by runing:
      powershell
      Then identify your COM port:
      [System.IO.Ports.SerialPort]::getportnames()
    3. Plug the FTDI or the Arduino to your computer and upload the Basic firmware to your ESP8266 following the flashing instructions.
    4. Wait 30 seconds and on your computer or any wifi device look for a WiFi AP called ESP and pair with it. Open the url http://192.168.4.1/filemng in a browser. You should see a list of .dat and .bin files. If you don't, it means that you uploaded the firmware with the wrong memory size (4M in place of 1M for instance). In that case unplug and replug the USB and return to step 3 using the next lower memory value.
    5. Click on the [SETTINGS] link. You should see the Basic GUI stored inside ESP8266 which allows you to configure and program it. Connect it to your local wifi network filling the Station Mode fields (see documentation here) and press the Save button.
    6. Unplug the FTDI or Arduino from your computer
    7. Remove the connection of GPIO0 to GND
    8. Replug the FTDI or Arduino to your computer and after less than 6 seconds run the PowerShell command:
      $port=new-Object System.IO.Ports.SerialPort COM1,9600,None,8,one;$port.open();while(1 -ne 0){$port.ReadLine();}
      replacing COM1 with your port name. If you are lucky you should see the ESP8266 IP address:
      1
      2
      3
      ...
      6
      
      Connected to you_AP_SSID
      IP Address : 192.168.3.x
      WIFIname
      WIFIpass
      file open failed
    9. Remember it and try to connect to this IP address with a web browser, you should see the same GUI.
    10. It is recommended to configure your router so the DHCP server associate a fix IP address to your ESP8266 (see your router's documentation).

References:

Step 4: Create a IFTTT Recipe

We will need a kind of bridge/gateway to send an alert to a mobile phone when the door alarm detects an intrusion.
For this we can use the wonderful ifttt.com. Create an account (or sign in) and follow the steps to create a recipe that will send an alert when triggered:

  1. Got to My Recipes and push Create a Recipe.
  2. Click on This and enter Maker in the search box. Click on the Maker channel icon.
  3. You might be asked to connect to that channel: push Connect. then push Continue to the next step.
  4. Click on Receive a web request trigger.
  5. Enter an event name, front_door for instance and push Create Trigger.
  6. Click on That and enter Gmail.
  7. You might be asked to connect to that channel: push Connect and follow the steps in the popup window.
  8. Push Continue to the next step.
  9. Click on Send an email action.
  10. Fill the fields:
    • To address: the email addresses of the people you want to be notified
    • Subject: something like: Home intrusion alert: {{EventName}}
    • Body: something like: Your {{EventName}} was opened on {{OccurredAt}}.
  11. Push Create action.
  12. Enter a meaningful name for your recipe like: If front door is opened, then send me an email #doorAlarm, and Push Create recipe.

Your recipe is set up, before going to the next step you need to recall 2 things:

  • Your Maker private key (go to the maker channel if you don't remember it)
  • Your event name, front_door in this case.

In this recipe I used the Gmail channel. If you don't have a Gmail account you can look for the Email channel. Also, if you prefer to use another/an additional way to be notified (like Tweeter, Facebook or even a phone call or a SMS!) feel free to do so!

Step 5: Program the ESP8266

This is time to write our custom code. The ESP8266 must be kept connected to power (3.3V) and optionally to the computer (Rx and Tx) if you want to track the logs.

  1. Go to your ESP8266 URL then Click on [EDIT]. Copy and paste the following code replacing the parameters {event_name} and {your_Maker_key} (including curly braces) with your values:
    print "-------------------"
    print "Sending request"
    print wget("maker.ifttt.com/trigger/{event_name}/with/key/{your_Maker_key}")
    print "Going to sleep"
    print "-------------------"
    Sleep 0
  2. Push Save
  3. Go to [SETTINGS], enable Run default.bas at startup and push Save
  4. Push Restart

You should receive an email alert very soon! If you don't: look at the logs, they might be helpful.

Here is a description of what we are doing:

  • when the ESP8266 boots, it connects to the wifi network and runs default.bas (this is the program we just wrote),
  • then, the url corresponding to our recipe's trigger is browsed and the response is returned by wget to the console via the print function, IFTTT do its magic sending an email,
  • right after receiving the response, the ESP8266 goes to deep sleep "forever".

In the next step we will connect the reed switch to the RST (reset) pin of the ESP8266 so it wakes it up when the door opens.
Each time the ESP8266 is woken up, it runs the program and goes back to deep sleep. This way we can save a lot of power and increase significantly the battery life!

I recommend "disabling" the ESP8266 power LED, if any, so it will save even more power (desolder it, or destroy it with small pliers).

Theoretical power consumption values:

StatusPower consumption[1]
Runningfrom 50mA to 170mA
Deep Sleep10μA
Experimental values:
StatusPower consumption[2]
Running72mA
Deep Sleep0mA

A regular alkaline AAA battery has a capacity of 1,000mAh[3], the average time needed to send the alert and go back to sleep is 32s[2] this leads to think that our alarm could potentially work for 1,500+ times. This is obviously larger than the reality as the battery voltage decreases during consumption preventing the ESP8266 to work normally. Some experimental tests must be made, feel free to comment!

[1]: ESP8266 data sheet.
[2]: Obtained using a INA219 with a 7ms logging rate.
[3]: Wikipedia

Step 6: Hack the Door Alarm Hardware

Open the door alarm enclosure and do some hacking:

  1. Remove the flat speaker
  2. Remove the autotransformer
  3. Isolate the alarm chip by cutting the tracks on the printed board

Solder the following connections:

ESP8266 pinDoor Alarm pin
GNDBattery holder GND
VCCPower switch output
RSTReed sensor

Additionally do the following:

  • Connect the second pin of the reed sensor to the battery holder ground
  • Connect the battery holder 3.3V wire to the power switch input
  • Wrap the ESP8266 in some electrical tape to prevent shorts

I recommend to do some tests before putting everything back in the enclosure:
Put 2 AAA new batteries in the slots.
When the RST pin is connected to GND it disables the ESP8266, this is why we need a pulse rather than a falling edge. To trigger a wakeup just bring the magnet close to the reed sensor and then back it off. Verify that you receive the alert. If you don't: check your wirings.

Once your tests are conclusive try to pack everything inside the original enclosure and screw the cover back on.

In the next step I will show you how to simulate the pulse when the door opens.

Step 7: Stick It on Your Door and Have Fun!

Now that we know our device is working as expected, we have to install it properly.

As described in the previous step, we need a pulse on the RST pin of the ESP8266. If we leave the RST pin connected to GND it will prevent the boot from loading, this is why I didn't use a pull-down resistor.

To simulate the pulse we will stick the part of the door alarm that goes on the door frame (either the body or the magnet depending on the opening side of your door) an inch away so the magnet is not activating the reed sensor while the door is closed. However, when the door opens, the magnet will enter in the activation zone of the reed sensor and then leave it as we continue to open the door. This will create a pulse Open-Closed-Open (the reed sensor is Normally Open) which corresponds to a momentary connection of pin RST with GND.

Test it ... you're done!

Step 8: Conclusion

This device has some obvious flaws: when the batteries run out, when the wifi/internet is not working etc. and you'll understand that it is not 100% reliable.
However, it is cheap, fun and relatively easy to make.

Now you can think of much more applications of the ESP8266, be creative!

I hope you enjoyed this Instructables, don't hesitate to put some comments with a picture of your own project!

Edit Oct. 2016: A software update is also available for this Instructable: IoT Door Alarm UPGRADED.

7 People Made This Project!

Recommendations

  • Epilog Challenge 9

    Epilog Challenge 9
  • First Time Author Contest 2018

    First Time Author Contest 2018
  • Sew Warm Contest 2018

    Sew Warm Contest 2018
user

We have a be nice policy.
Please be positive and constructive.

Tips

Questions

105 Comments

Hi, i'm new to eletronics, and i like this post, but my sensor is a little diferent, i can´t cut the chematic in the same way (as photo 3), can anyone help with the wiring for ESP8266 in the image layout.. or if i need to do some cuts in the circuit,

Thanks;

IMG_20170502_233134.jpg

Hi,

Here is a graphic explanation. I hope this helps. Feel free to ask for more help/details!

Good luck and happy hacking! :)

circuit.jpg

I forgot to mention that you need to cut the circuit on the blue lines.

Hi, thanks for your time to analize, see the attach with other side... i don´t understand one thing, the "to BAT GROUND" and "to ESP VCC" in the layout are the pins that support de button... is that the correct connection??
I send the photo of the button too.
Thanks..

IMG_20170507_130603.jpgIMG_20170507_131400.jpg

you can use it, you have that gig blck round thing! it is a inductor, and works exact the same as an reed switch my friend.

i bet that black thing was at the sensor side where the magnet most touch it for the alarm...? eeh....

Hi,

You are right, don't connect it as I told or you will make a short circuit :D

Your plate is quite strange, usually there is a bulb with 2 close wires inside, this is the reed sensor. In your case the bulb has been omitted. I don't even know how if the original alarm would work this way.

I added a cut to isolate GND and VCC. Do not cut the one at the bottom, I covered it with green line.

Good luck.

circuit.jpg

i think it would work, he has an inductor as a switch, that is an magnetic switch i think. or he must FOR CONVEIANCE buy our alarms that we all used heree from aliexpress for 1 USD.

if you want a link ask me, i have it, FREE SHIPPING, do it!!!!!!!!

Could you post a photo of the other side of the board please? I want to double check, I suddenly had a doubt about one thing.

Hi, I simply love this. What is your opinion on other wireless methods like Bluetooth or IR? If you have done something of that sort please share.

Thx. Will do one day!