LEIDS - Low Energy IOT Door Sensor

19K17922

Intro: LEIDS - Low Energy IOT Door Sensor

What is LEIDS?

LEIDS is an IOT sensor that is based around the ESP8266. This sensor makes use of this board, a soft latching circuit, a reed switch, and some magnets to create a door sensor that will send you an alert when your door opens and closes. This sensor can be hooked up to your MQTT server, IFTTT, or many other frameworks that support the ESP8266. This instructable will use IFTTT to easily create a sensor that will send an alert to your mobile device no matter where you are. The sensor can also be setup to allow for multiple alerts from separate doors, mailboxes, ovens and anything else that has an open/close or on/off physical position. The benefit of LEIDS is that it uses minimal power and only uses power when a door is open, then when the door is closed it sends a message to alert you the door was closed and turns off. This is better than deep sleeping the module as it uses no energy if the door is not opened.

STEP 1: Supplies

The lists below show the tools and parts I used for this project. The sensor can be redesigned to use different parts and look/function differently and just use similar concepts.

Tools:

  • Soldering Iron
  • ESP01-UART or Arduino Uno(programmer for ESP8266)
  • 3D printer (optional)
  • Jumper cables(optional)
  • Breadboard(optional)

Parts:

  • ESP8266 ESP-01S x 1
  • Reed Switch x 1 (preferably NC, I could only find normally open so have to use two magnets to make it work as normally closed)
  • AMS1117 3.3v Regulator
  • 2n3904 NPN Transistor x 1
  • P-Channel Mosfet x 1 (we want something that has a low VGS, preferably a NDP6020P)
  • 3.7v 100 mAh Lipo battery x 1
  • Assorted Resistors (100k ohm, 10k ohm, 220 ohm, 3k3 Ohm)
  • 6x3mm Magnets x 2
  • Double Sided mounting tape
  • Wire
  • Solder
  • Perfboard

3D Printed Parts:

  • Case for sensor

STEP 2: Soft Latching Circuit - Hardware

The soft latch circuit I am using is a modification of a schematic I found online. I am not an expert and I believe there are better ways to build this circuit but for my purpose this circuit does the job!

The way it works is that when the reed switched is closed by a magnetic field it allows current to the NPN transistor. The NPN transistor then grounds the PNP mosfet which allows current to flow to the ESP8266. The ESP8266 powers on, uses one of its pins to keep the PNP mosfet grounded which continues to power the ESP8266. Then when the ESP8266 is done with it's tasks, it sets that output pin to LOW which interrupts the NPN transistor and closes the gate on the PNP mosfet.

By using a circuit such as this the ESP8266 can be powered on by a switch, and then can power itself off completely via signal. This allows the circuit to draw no current when the sensor is closed/off.

STEP 3: ESP8266 - Hardware

The ESP8266 that I am using will be sending a message to alert when a door(or other thing) is open and then will wait for the door to close to send a close message and shut itself off. The ESP8266 will be powered by the soft latch circuit and will use one pin to read the reed switch and another pin to keep itself powered on and then to turn itself off. I chose to use the ESP-01s due to it's small form factor. The ESP will be sending a web request to notify us when the sensor is changed to "opened" and "closed". The easiest method I found was to use IFTTT and Google Sheets. This is explained in the following steps.

STEP 4: IFTTT for Remote Notifications - Part 1

In this instructable will be using IFTTT and implementing webhooks and google sheets to get remote notifications on our mobile device. To use these services you will need the following:

  • A Google account
  • An IFTTT account (can/should login with Google account)
  • The IFTTT application for your mobile device

Once you have created/logged in to your accounts we will create two applets. The photos at the top of this section show how to maneuver the webpage. The following steps are for the first applet, the event fired request. This applet will get a message from the sensor and save the event in a Google sheets document. Then you can view the history of your sensor and see when it was opened and closed.

  1. Go to the "My Applets" link located at the top
  2. Click on "New Applet" button
  3. Click on the blue "+this" button
  4. Search for "webhooks" and click on it
  5. On the next page select the "Receive a web request" card
  6. Then set the event name to whatever you desire, I chose to name it "sensor_activated"
  7. On the following page click the blue "+that" button
  8. Search for the "google sheets" service and select it
  9. On the following page choose the "Add row to spreadsheet" function
  10. Fill out the action as follows:
    • Spreadsheet Name, I named mine "Sensor History"
    • Formatted row needs to look like the following
      • {{OccurredAt}} ||| {{Value1}} ||| {{Value2}}
    • Leave the last field as is unless you want to change where the applet will save the status of the sensor
  11. Then click "Create Action"
  12. Lastly click "Finish" on the final screen


Now lets test that it works

  1. Go to this link https://ifttt.com/maker_webhooks
  2. Then click on the "Documentation" button (we will use this "Documentation" page again later)
  3. Now you will be at your own personal section for the IFTTT Webhooks applet
  4. Fill in the field where it says "{event}" with the name of your event in this case the event name is "sensor_activated"
  5. Now fill in the "value1" field, this is the field that will hold the location of your sensor, I am going to fill it in with "Front door"
  6. Now fill in the "value2" field, this is the field that will have the status of the sensor, I am going to fill it in with "opened" just for testing
  7. Then click the blue "Test it" button
  8. If all went well then you should get a green "Event has been triggered." message at the top
  9. Now if you want to see how it stored your message, you can open your google sheets and find the spreadsheet that it added, I have attached a picture to show what the spreadsheet will look like

Now on to the next step adding an applet to notify us of the status change.

STEP 5: IFTTT for Remote Notifications - Part 2

Now we will add an applet to notify us when the status spreadsheet gets a row added; in other words, when the sensor fires off an event. As before we will be creating a new applet. The following steps will guide you in creating the second applet. Since we have done something similar in the previous step I will only add the slides that are different to this step.

  1. Go to the "My Applets" link located at the top
  2. Click on "New Applet" button
  3. Click on the blue "+this" button
  4. Search for "google sheets" and click on it
  5. On the next page select the "New row added to spreadsheet" card
  6. Then fill out the fields to point to the spreadsheet that the other applet is writing to
    • In this example the path is "IFTTT/MakerWebooks/sensor_activated"
    • The filename is "Sensor History"
  7. On the following page click the blue "+that" button
  8. Search for the "notifications" service and select it
  9. Now we need to fill out the notification service fields
    • The title is changed to
      • {{ColumnB}} was {{ColumnC}}
    • The message is set to
      • {{ColumnB}} was {{ColumnC}} on {{ColumnA}}
    • The link is left alone
  10. Then click "Create Action"
  11. Lastly click "Finish" on the final screen

To test the second part of the IFTTT notification system make sure you have downloaded the IFTTT app on your mobile device and login with your credentials. In my case I am logging in with my google account. Now we can test this by going back to the "Documentation" page and firing off the test with the event name set as "sensor_activated", the "value1" field set to the location, and the "value2" field set to the status.

This should send you a notification. If you do not receive one make sure that you are logged in with the same account and that you have allowed the application to send you notifications.

STEP 6: Arduino IDE and Sketch

The software used for the board is compiled using the Arduino IDE.

The logic is as follows:

  • The reed switch is closed by opening the door
  • The ESP is then powered on and connects to WiFi
  • Once the WiFi connection is established it sends an http request (sends a command to the internet) that tells us that a door or something else has been opened
  • Then it reads a signal from the reed switch to determine if the door has been closed
  • When the ESP board discovers that the reed switch is opened(i.e. the door was closed) it sends another http request that tells us the door was closed
  • Then it signals the soft latch circuit to cut the power to the board

Download Arduino IDE

I have attached the arduino code to this section for you to download. If you do not have the Arduino IDE you can download it online at https://www.arduino.cc/en/Main/Software.

Install the proper board manager

To upload the code to your ESP board you need to install a board manager in the Arduino IDE using the following steps.

  • Go to File > Preferences to open the preferences window
  • In the preferences window find the field that says “Additional Board Manager URLs”
  • Paste the following link into this field http://arduino.esp8266.com/stable/package_esp8266com_index.json and press the "OK" button
  • Now open boards manager by going to Tools > Board, then the very first entry will be Boards Manager
  • Type "ESP8266" in the board menu and install “esp8266” I select version 2.5.0 as I have had some issues with the newer versions
  • Now go back and select your board from Tools > Board > Generic ESP8266 Module
  • Then close out and reopen the arduino ide to make sure the changes took place

Open the sketch in the Arduino IDE

We need to modify four line of the code so the sensor will work for you

  1. Change the SSID to your WiFi SSID a.k.a. the name of your wifi
  2. Change the password to match the password for your wifi
  3. Change the url to match the url from the "Documentation" page of the "webhook" service
  4. Change the location of the sensor to where you plan on placing it, this can be the front door, back door, mailbox, etc.

Now that you have modified the code to suit your needs we need to load it onto the board. In my case I am using a USB programmer and following the instructions for it. This can require a lot of troubleshooting so I suggest you look online for a more in depth tutorial. In my case I plug the ESP-01s to the programmer, then I plug it into the computer, then I flip the switch from uart to prog, lastly I unplug and plug the programmer back in. This now leaves the ESP in programming mode. Now I just click on the upload button and let the upload finish. If you want to see if it connects, you can open the serial monitor and look at the status messages. Once you have uploaded the sketch we will solder everything together, put it in an enclosure, and mount it to our desired location.

STEP 7: Enclosure (3D Printed)

I have designed the following enclosure to accommodate all the components, allow for the placement of a magnet and align the reed switch. The second part is just used to hold a second magnet that will allow the switch to be opened and closed.

Once all the components are soldered together we can press them down into the case, if you want you can add a bit of hot glue to the bottom to hold everything in. Then we need to press one magnet into the case with the electronics and align the reed switch as necessary. The other magnet should be pressed into the other enclosure. Once both enclosure are completed we can test it by putting the two enclosures together, then separating them and bring them back together. We should now get two notifications one for sensor opened, and one for sensor closed. Keep the two together to keep the battery from draining.

Attach mounting tape to the back of the sensors and apply them on the desired location. I mounted mine on my mailbox. Now test that the alignment is correct and that the sensor is able to send an open and closed message.

Voila, you now have your very own sensor to alert you when it is opened and closed. Let me know if you like it and if there is anything that you feel can be improved.

I am also submitting this instructable to the IOT contest so make sure to like and vote for this and any other IOT projects you have enjoyed!

STEP 8: Future Improvements

For this project I ran into a couple of issues. The first problem I encountered was the use of a mosfet that had too high of a Vgs so I had to switch to a different one. Second, the reed switch I used is normally open and in my case a normally closed switch would have been easier to implement and would have saved a lot of space. Lastly the esp 01s was very difficult to work with and I had to mess with the gpio pins to get it to work properly but I still had some issues. I think that this project was able to teach me a lot of concepts and has helped me come up with more ideas and implementations using the esp8266 boards and IFTTT.

18 Comments

I'm having issues with getting this to work. When I try and unlatch it the voltage drops from 4.1V to 3.4V which is high enough to keep the ESP8266 powered on. Any suggestions would be much appreciated.
A nice add would be to check battery level with activation of door and alert when at maybe 25% remaining
Hey RichardM350, that is a pretty good idea. I might look into using one of the other pins to keep a tab on the battery level.
Hi, Yes that's one way. Or a cheaper way is to use read the internal voltage of the ESP using ESP.getVcc() and then log it in the MQTT message. I do it that way. As the ESP can run till 3V it usually will take a few days to weeks for the voltage to fall from 3.3V to 3V on the ESP and you have ample time to respond to it and change the battery.
Nice, comprehensive project.
You say thay N/C reed switches aren't available. They are, but you nedd to buy a reed switch with changeover contacts. I bought some recently from RS or Farnell to allow me to take the power off a sealed battery powered wireless device
Thanks for the circuit. I had built something similar but it still
consumed ~6uA current in the idle state , so this one definitely seems
better. Although I find working with MOSFETS a bit difficult as they
tend to get damaged easily with static charge when handling them.
Can you please complete the circuit diagram with connections to the ESP 01? The diagram above does not specify the same.
EDIT: I was able to figure out the pins by looking at the sketch. 1- Vcc , 2 - GPIO2 , 3 - GPIO0 , 4 - GND.
Also, I would add a a LDO like HT 7333 between the MOSFET and the ESP so that it can withstand a voltage of a fully charged Lion battery of 4.1V.
I currently use a 1000mAh 18650 battery to power the existing door sensor which consumes ~6uA and it lasts for around 5-6 months for my main door which opens and closes at least 30-40 times a day. So this one should last for a verrrrrrrrrry long time.
Hey vks007, I was going to try to put a better schematic together after work today but yes you are right! Also I am using an AMS1117 to regulate the voltage to the Vcc so that it outputs a clean 3.3v at 1 amp. The hope was to have something that last a long time and was small/discreet, I am only using a 100mAh battery so I would hope it would last quite a while. I have not had this little sensor setup for very long and have not kept tabs on how long it lasts so I guess I will try to speed up the testing maybe by having something open close the switch continuously until the battery runs out.
HT7333 is better, if door is open/close offen...
Hey JurajP1, I agree it is a better component since it draws less current while on. So if I was deep sleeping it would be a lot better, in my case it was easier to get the AMS1117 and I wasn't going to be deep sleeping so I was not too concerned. Thanks!
Over all a very good post.

As an electronics engineer I offer 1 minor change, reduce R3 to about 20K to 50K though 20K is still the better. R3's only real purpose it to pull the gate HIGH to turn the P-MOSFET off when the gate is not high.

This will better guarantee full power off when the R5 and diode pin changes to turn off again.

If the resistance of R3 is too high and the P-channel MOSFET fails to turn "Fully Off" it can keep the ESP from properly booting on successive triggering.
I fully agree with you. The high resistance may have caused some of the bugginess I experienced. Thanks for the excellent tip!
Nice idea. I think i'll try it. Have you test to see how long the battery lasts before it needs replacement for an average usage scenario?
Same question here. I've heard connecting to WiFi uses so much power it would basically only last ~10 times...
Hey ReneB2, you are right it uses a lot of battery to use the wifi; however, the hope is that it should only use it for such a short time it won't waste much energy. I will need to test it more to see how long it can really last (hopefully a couple months of normal usage).
Hey Dram229, as per my other comment I have not really had a chance to test it extensively. I will try putting something together to open/close the switch a bunch of times or maybe leave it open and see how long it lasts.
Sounds like a nice little device and it looks really finished in the 3D printed enclosure :)