Introduction: 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.
- Go to the "My Applets" link located at the top
- Click on "New Applet" button
- Click on the blue "+this" button
- Search for "webhooks" and click on it
- On the next page select the "Receive a web request" card
- Then set the event name to whatever you desire, I chose to name it "sensor_activated"
- On the following page click the blue "+that" button
- Search for the "google sheets" service and select it
- On the following page choose the "Add row to spreadsheet" function
- 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
- Then click "Create Action"
- Lastly click "Finish" on the final screen
Now lets test that it works
- Go to this link https://ifttt.com/maker_webhooks
- Then click on the "Documentation" button (we will use this "Documentation" page again later)
- Now you will be at your own personal section for the IFTTT Webhooks applet
- Fill in the field where it says "{event}" with the name of your event in this case the event name is "sensor_activated"
- 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"
- 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
- Then click the blue "Test it" button
- If all went well then you should get a green "Event has been triggered." message at the top
- 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.
- Go to the "My Applets" link located at the top
- Click on "New Applet" button
- Click on the blue "+this" button
- Search for "google sheets" and click on it
- On the next page select the "New row added to spreadsheet" card
- 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"
- On the following page click the blue "+that" button
- Search for the "notifications" service and select it
- 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
- The title is changed to
- Then click "Create Action"
- 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
- Change the SSID to your WiFi SSID a.k.a. the name of your wifi
- Change the password to match the password for your wifi
- Change the url to match the url from the "Documentation" page of the "webhook" service
- 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.
Attachments
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.