Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Hide Buttons
SimpleIOThings

Simple Do-It-Yourself Internet-of-Things Projects

img_20151113_125816_22605161119_o
27

$10 DIY Wifi Door Sensor

This tutorial will teach you how to build a Wifi Door Sensor for about $10 dollars. You won’t have to code anything, and you won’t have to solder or buy expensive electronics equipment. Any services you are asked to use will be 100% free. Its that easy. Lets get started!

Some doors are meant to stay closed. For example your freezer door needs to stay closed to keep your energy bills down. Pet owners need to keep the door closed so their four-legged/winged companions don’t wander outside. Whatever door you need to keep an eye on, a wifi enabled door sensor can help you send a smartphone notification, SMS, email, and even an automated phone call when its been open too long.

The cool thing is, you can build one of these for about 10 dollars. I know you’ve seen these types of tutorials on the internet before, and usually they say something like, just buy a breadboard, soldering iron, breakout board, serial adapter, etc. etc. and after you’ve spent about $50 dollars you can build a cheap Internet of Things (IoT) device. Well, sorry internet. We nerds sometimes forget that most people don’t really have these things lying around. That’s why I built this website around the idea that IoT devices can be built without coding knowledge, soldering, or complicated prototyping.

STEP 1: Gather Your Materials

Door Sensor Materials

Its a good idea to buy your parts first because on the sites where the merchandise is cheapest (Ebay or Aliexpress) shipping times are rather lengthy. Depending on where you live in the world, it might take some time for your components to reach you so its better to do this sooner in the process than later.  Here’s a list of things needed for this project. FYI, I’ve included some affiliate links to  the cheaper places you could buy these components. I’ve done my best to ensure the ads display the lowest price goods that are relevant to this project, and if you buy through them you’ll be helping me maintain this site.  Thanks! 😉

Required Components:

(FYI, if you’re just seeing text below with no images, its probably because you have an adblocker on. I know ads suck, but in this case they were actually a creative way to continuously update the page with the lowest cost parts. Prices change, so if I used static links and pics, the link would quickly become outdated. Please consider turning off adblock for this site for the best experience.)

1 x ESP8266 Development Board
AliExpress.com Product – 2015 New product Wireless module NodeMcu Lua Wifi Nodemcu WIFI Network Development Board Based ESP8266,High quality products

1 x DIY Project Box Enclosure 100x60x25mm

2 x Female to Female Jumper Wires

1 x MC-38 Door Sensor

1 x MicroUSB cable (10′ Cable Recommended)

1 x USB Wall Charger

Suggested Tools:

These are tools needed to build the IoT device in this tutorial, but aren’t necessarily required.

1 x Pair of Pliers
1 x Hack Saw

Great. Now like I said, its gonna take a while for your stuff to get to you, so in the meantime lets setup some digital infrastructure that we can use once your stuff arrives.

Note: This is our third tutorial! You should be noticing a pattern by now, which is that steps 2 – 4 are pretty much the same for every device. If you already know how to do all the basic stuff, skip to step 5, where I start to talk about some stuff specific to this project.

STEP 2: Setup Your IFTTT Account & Recipes

The Wifi Door Sensor, and all of the devices on SimpleIOThings, uses a service called If This Then That (IFTTT). You can find a full tutorial on how to sign up and configure digital communications for your IoT devices in the Getting Started: Setup IFTTT post. Once you completed it, you should have completed the following:

  • Signed up for an IFTTT account
  • Downloaded the IFTTT App
  • Connected to the Maker Channel
  • Recorded your Maker Key
  • Organized Maker Events (Triggers) that lead to IFTTT Actions.

Okay! We’ve got some really nifty digital infrastructure setup for your project. Soooooo…its gonna take a while before you get all this stuff…so its probably best to bookmark this page and come back after it’s all arrived. See you in a few weeks!

STEP 3: Install Firmware to your Dev Board

Oh hi there! Welcome back. I know its been a while. Maybe the seasons changed in your part of the world.  Governments may have risen, switched hands, entered alliances, broken alliances, fallen, and then risen again. Yea…it takes a while to get your stuff. But now that it’s here, lets start building!

connect_devboard_to_pc

 

You can find a full tutorial on how to load firmware onto your ESP8266 Development Board on the Getting Started: Setup ESP8266 Dev Board post.  After you’re done with this tutorial you should have completed the following.

  • Connect your ESP8266 Dev Board and Install Drivers
  • Load Firmware onto Dev Board using ESP8266Flasher.exe.
  • Connect to Dev Board using LuaLoader.

STEP 4: Install Sensor Specific Software to your Dev Board

Great! Now your dev board speaks Lua and its ready to start communicating with the world and run programs. Now it just needs specific instructions or programs to be useful for your specific use case. Lucky for you, I’ve already written the programs needed to get your device up and running. Full instructions on how to program your device with specific settings for your use case can be found on the Getting Started: Loading Device Software Via LuaLoader tutorial page. In this tutorial you should have completed the following:

  • Download the latest SimpleIOThings.zip file from the Downloads page.
  • Extracted the zip file and run the SensorSetup.bat file.
  • Input your information into the command prompt  to enter user and device specific information (IFTTT Maker Key, the Maker Event name, the device’s location info, etc).
  • Entered “2” for the Sensor Type Number, identifying this sensor as a “continuous read” sensor (i.e. one that reads the sensor’s current state, and triggers an alert if a specific state [like an open door] is detected for more than a threshold determined number of seconds).
  • Enter input thresholds, input timeouts, and interval between notifications as prompted.
  • Uploaded device specific files to your ESP8266 Development Board using LuaLoader.
  • Set the Wifi Network Name and Password for your ESP8266 Development Board.

STEP 5: Modding Your Enclosure

All the way back in step 1, we talked about gathering materials. Now that you have everything in hand and your board is programmed, get all your materials together in one space.

Some suggestions on how to mod your enclosure can be found here at Getting Started (5): Modding Your Enclosure. After completing this tutorial you should have completed the following:

  • Created an opening in the rear of the enclosure for the microusb power cable.
  • Created an opening in the front of the enclosure for the jumper wires.

STEP 6: Build the Door Sensor

Now go ahead and unplug your dev board (there is almost no electrical danger to you from touching the dev board while plugged in, but might as well err on the side of safety).

Grab your two jumper wires. You can see that at the end of each wire is a plastic housing covering some metal. We’re gonna remove that plastic housing. Grab a needle or safety pin, and find a small tab in the plastic housing. Lift up on the plastic tab. Once lifted up, you should be able to smoothly remove the wire from the plastic housing.

20151113_124100_22804114020_o

Now, grab the MC38 sensor that you bought. Note that there are two halves to it. The side with the wires is the actual sensor, while the side without wires is actually just an enclosed magnet. The sensor half contains something known as a reed switch. A reed switch is either normally open or normally closed, and when it comes in range of a magnet, the switch inside will close or open to break or create a circuit.

Okay, so grab the sensor, and then note that the exposed wires. If the wires aren’t exposed, you’ll need to strip off some of the wire insulation. Once you’ve removed the jumper wire housings, and grabbed the MC38 sensor wires, the ends of the four wires should look like this.

20151113_124146_22573864958_o

One of the guarantees I make at the beginning of each tutorial is that the project won’t require any soldering. Since we have to connect these female jumper wires to these exposed sensor wires,  we’re gonna use a technique called “crimping.” Crimping is a way to join wires by using a pliable metal housing that you can compress with pliers to create a firm connection.

Insert your exposed sensor wire end into the remaining metal housing end (the part that was under your plastic housing. It should look like this:

20151113_124242_22573878618_o

Once the wire is fully inserted, use your pliers to “crimp” down on the metal housing, but not that hard! Hard enough to bend the metal to create a good connection, but not so hard it cuts through the metal housing and the wire. It should look like this:

crimping

Once your wires are successfully crimped, we also want to make sure that the two metal housings on separate wires do not touch one another. To do this, use duct tape or scotch tape to insulate your exposed wires.

crimp_tape

Now grab your dev board, and observe the names of the pins on the board. Go ahead and plug your jumper wires into the “GND” and “D2” pins on the right hand side. It should look like this:

22627004201_f05864362c_k[1]

Everything should now look like this:

20151113_124633_22966229176_o

Just a quick note for those of you who are interested in the electronics. When setup this way the door sensor, when opened, is acting as a bridge between the D2 pin and the electric ground (-). That way when the magnet is pulled away from the sensor, it grounds the D2 pin, registering a change in electrical signal which the chip detects as a sensor input. Pretty cool! Great, the electronics are all setup. Pretty simple right?

Alright, now you can put your electronics into the enclosure. Plug your usb cable into the dev board, and make sure your jumper wires are well positioned before you close the project box.

20151113_125134_23003439491_o

And we’re done!

20151113_125355_22966362436_o

Your sensor should now be fully operational. Give it a test run by separating and it should trigger whatever you’ve set it up to do, whether that’s a smartphone notification, SMS, phone call, email, etc.

I installed mine on my fridge. Every once and a while I position my frozen foods poorly. This pushes open the fridge door, everything in my fridge ends up thawing overnight. I built this sensor to tell me when my fridge door is open too long, but you can use it for your front door, a window, etc.

Door Sensor Fridge

I hope you enjoyed building a super simple Internet of Things device. Hopefully you will be able to use it for something useful, or use it to do something fun. If you enjoyed this tutorial, consider donating at the link below. Thanks for reading!

     
AutomationBeginnerboardCheapdevelopmentDevicesEasyESP8266HomeInternetIoTLuaNodeMCUThings

SimpleIOThings • November 14, 2015


Previous Post

Next Post

Comments

  1. Tom_Neverwinter March 27, 2016 - 5:19 am Reply

    Your site is easily the best iot site I’ve seen. no proprietary bs, no use our shitty architecture. I appreciate the barebones, Modular and functionalist system. now the last device I need to get running is a mailbox sensor with temp and light w/ maybe a few other sensors run from battery and solar.

  2. Paul August 13, 2016 - 1:22 am Reply

    Hi
    The door sensor project looks great and is just what I need. I have ordered all the parts and they have arrived. It now looks like your instructions for programming the board are for a PC. Can I do this on a Mac?

    Look forward to some advice on this.
    Many thanks for the work you have done on this.
    Paul

    • SimpleIOThings October 18, 2017 - 5:14 am Reply

      Hey Paul, I would recommend using a Windows virtual PC to load firmware and software onto the board.

  3. David Lamb September 5, 2016 - 4:39 am Reply

    This is pretty cool. Would it be possible to add wires on the board from a wired garage opener and update the code to trigger open/close.

    I’m thinking I could use IFTTT to integrate with this, especially with the maker channel, and potentially take a $10-$15 iot to automate my garage beyond knowing it’s open/close but also being able to change its state.

  4. Michael Fenton September 12, 2016 - 6:39 pm Reply

    Hi,

    Brilliant project – I recently left my freezer door open overnight and wanted to by a door switch that would alert me . Built the device but struggling to get to Alert me via sms . Set up ifttt account with recipe and input my mobile no and it sent me the text which i verified . Have you go any pointers of what to look for . The switch works as I can see the sensor inputs in the Lualoader software . What is the notification time in minutes for the default settings . I live in the uk so wasnt sure if this had anything to do with the problem

    Thanks in advance

  5. Jacob September 13, 2016 - 8:50 pm Reply

    Hi that setup seems pretty cool but im wondering is there any way to know the status of the at any time. let says I install it on my garage door can go on the IP of the wifi board and know if my garage door is open in real time?

  6. Aaron Peterson October 15, 2016 - 11:24 am Reply

    Now we need to make it have good life when running off of a battery.

    I think we could do that by making the device power on when the relay is triggered. (if the person wants the device to be transmitting when the door is closed, then just use a the opposite type of relay, or many relays have a normally open gate and a normally closed gate…

    We could also make it so that the device stays on for a bit after the gate is closed to transmit the door is closed signal.. Well then, we should use the normally open and normally closed gates on the relay, (magnetically controlled switch) and have the device wake up for just long enough to transmit the change of state, then power off. How to make it not use any power when it is off though?

    If we have an always running but of logic, we could use just one of the normally open or normally closed lines…

    • SimpleIOThings October 9, 2017 - 4:31 am Reply

      Yea there is definitely a battery friendly solution. When I have more time for my hobbies I’m gonna give the code a hard relook. :)

  7. Larry Elliott December 14, 2016 - 1:15 pm Reply

    Would it be possible to put multiple door sensors on the same board?

  8. Steve December 20, 2016 - 6:59 pm Reply

    Hi,
    I’m wondering how easy it would be to connect two door sensors to the board. What would the wiring look like?
    Thanks :)

  9. Iwantmyiot February 6, 2017 - 10:57 pm Reply

    Awesome site and pertinent information!
    Smoke alarm and this will be my next projects. Thank you for sharing.

    Some general questions…
    1) Can this sensor be picked up/detected by Homeassistant?

    2) Also regarding this project can this be used/modded to be a panic button, and trigger other IoTs?

    3) lastly, can these wifi devices be battery powered (and stay like that for months?) if not could they be modded to?

    Keep up the good work!

  10. Ken February 22, 2017 - 5:07 pm Reply

    I am really new to this. The programming part still has me confused along with
    what the door switch is suppose to do.

    I was thinking of using a few WiFi smart plugs to control a relay that would open and close
    my garage door. That part is easy using the app that comes with the device and a relay that gets powered by a 120ac to 5 volt d.c. supply. I wanted some
    type of email confirmation whenever the door is opened or closed.

    Again I am new here so I might have missed it .. how does the device described here report
    the status of the door opened or closed.

    Thanks,
    Ken

  11. Luke March 17, 2017 - 3:21 pm Reply

    Just wondering how you settings you used for the Sensor setup?

    I have tried to use the stock standard bat file examples and it doesn’t seem to do anything for the sensor.

    The example used the following
    alertDelay: 30
    timerThreshold: 10
    sensorThreshold: 1000

    Can you confirm that is what you used?

    Thanks,
    Luke

  12. Luke March 17, 2017 - 3:21 pm Reply

    Just wondering how you settings you used for the Sensor setup?

    I have tried to use the stock standard bat file examples and it doesn’t seem to do anything for the sensor.

    The example used the following
    alertDelay: 30
    timerThreshold: 10
    sensorThreshold: 1000

    Can you confirm that is what you used?

    Thanks,
    Luke

  13. John Brannen March 23, 2017 - 4:57 pm Reply

    Great project!!! I built one and have it working, but am having a little issue. I am using this to notify me if my detached workshop door is opened. Kind of as a pseudo alarm. The problem I have is that notifications to my phone sometimes have significant lag. It seems that if the sensor is not triggered for some time that the time from activation to receipt of the notification can take up to 10 minutes. If there has been a recent activation, the notification only takes seconds, but the first notification after a period of inactivity takes a long time. Is there anything that can be done to reduce this?
    .
    Also, I have a second ESP8266 that I want to experiment with and create a similar sensor that would send the notification of the door opened, but also be able to be pinged from my phone to check the state (ie: open or closed). Any help getting me started down this road would be greatly appreciated. Alternately, setting it up so that it would send a notification any time the state changes. For example “The XXX_Door was opened” and “The XXX_Door was closed”
    .
    Thank You, Thank You, Thank You for the great project and write-up.

  14. dan May 6, 2017 - 3:00 pm Reply

    for some reason, it’s working in reverse for me. when the door opens, nothing happens. when the door closes, it triggers the event.

    D2 to GND has continuity through the switch when the magnet is next to it.
    continuity is lost from D2 to GND through the switch when the magnet is away.

    so with the switch together(door closed), D2 is shorted to GND so it goes LOW. when the switch is separated (door open), D2 is open circuit to GND so it goes HIGH.

    i can get it to work if i connect 3.3v to the reed switch, reed switch to D2 and put a 15k resistor between D2 and GND.

    any way to get a status message every time there is a change from either door open to door closed and door closed to to door open? right now i can only get a message for one, not both.

  15. Brad Plett June 5, 2017 - 6:56 am Reply

    It seems to me that either there is a bug in it, or the generic sensor software provided on the downloads page (simpleiothings.zip) isn’t particularly well suited to a door ajar application. Of course, this may also be due to the fact that the file I should be downloading (SimpleIOThingsDoorSensor.zip) doesn’t appear to be there.

    For one thing, with the software provided, it only senses the door opening, not the door closing. If we can only sense the door opening, we’ll never know how long it’s been open.

  16. Chris June 22, 2017 - 7:09 pm Reply

    Hey there.

    I’m planning to set this up at home. Since I’ve got three doors next to each other, do you think it would be possible to use multiple MC-38 on just one ESP? I’m a total noob with these stuff, any help would be greatly appreciated!

    Regards,
    Chris.

    • SimpleIOThings October 9, 2017 - 5:04 am Reply

      It is possible but not with the current code base. Basically you’d need to rewrite the LUA code to monitor multiple GPIO pins for changes. Give it a shot!

  17. Mario Beaulieu July 11, 2017 - 5:03 pm Reply

    Hi,

    Thanks for this tutorial. This is very close to what I’m trying to achieve, which is to build (or buy) cheap switches that would communicate to Homekit or Homebridge.

    I have many devices connected to Homekit and Homebridge (on my Raspberry Pi). Now, I would like to “know” if a window is open or close, etc. So I need cheap intelligent switches.

    Is it possible to connect your solution to homebridge instead of going thru IFTTT?

    Thanks a lot

    Mario

  18. Salim M August 17, 2017 - 1:43 am Reply

    How could this be easily modified so that it has two magnet door sensors?
    One for the freezer and one for the fridge, using the same housing and dev board, just adding a second magnet sensor. Any suggestions? Thanks.

    • SimpleIOThings October 9, 2017 - 4:22 am Reply

      This can be done but you’d have to rewrite the code to listen for changes in electrical signals on more than one GPIO pin. Its definitely doable. Take a look in the sensor software code and see if its something you can tinker with! :)

  19. Steven August 19, 2017 - 5:36 am Reply

    HI, thanks so much for this info. I’ve followed through all the steps but I’m not sure if I’m doing something wrong?

    My problem is this:
    The door sensor triggers when the sensor ‘reconnects’, not when it has been disconnected after X number of seconds.

    1. Sensors are together (magnet engaged)
    2. Logs say Listening for Sensor Inputs
    3. I move the sensors apart
    4. Logs still say Listening for Sensor Inputs
    5. I put the sensors back together
    6. Logs say (1) sensor inputs counted

    I guess what I’m hoping for this to do is for the sensor to trigger when the sensors are moved apart. Am I missing something?

    Thanks

    • SimpleIOThings October 10, 2017 - 3:22 am Reply

      Hey Steven,

      Looks like something got goofed up with a recent code update. Sorry about this. I’ll be working on a new solution for the door sensor.

  20. jan September 21, 2017 - 6:14 pm Reply

    is it possible you can make a video/pictures on how to proceed on the SimpleIOThingsDoorSensor.zip, specifically what info goes into the maker account and so on, and perhaps also on the lua loader.
    Thank you…. great project

  21. Www.Topicemakers.Net October 6, 2017 - 10:19 am Reply

    It’s a shame you don’t have a donate button! I’d most
    certainly donate to this excellent blog! I suppose for now
    i’ll settle for bookmarking and adding your RSS feed to my Google
    account. I look forward to new updates and will talk about this blog with my
    Facebook group. Talk soon!

    • SimpleIOThings October 9, 2017 - 4:01 am Reply

      We do indeed have a donate button at the bottom if interested. Glad you enjoyed the blog. :)

Leave a Reply

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

css.php