ESP8266 as Window Sensor with years of battery life

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
damn again the last housemate forgot to close the bathroom window and now i have to sit on the cold toilet seat oh no it's raining and the skylight is still open you see there are many use cases for a window sensor in this video i'm gonna show you how you can build a wifi enabled windows sensor based on an esp8266 whose battery lasts several years and which sends the current status via mqtt to your server i will show you that you don't have to be afraid of smd soldering and how you can design your own pcb with keycad with the help of this circuit it is easily possible to realize other similar applications for example you can use it to monitor your mailbox or doorbell we will see how the circuit works and how you can simulate it on your computer with circuit js [Music] welcome to my youtube channel in this video i assume again that you are familiar with the arduino ide and have a working iot stack for example a raspberry pi with mosquito node red influx zb and krafana follow the linked video if you don't know how to install it basically the hardware could also be implemented with the wemos c1 mini but since the deep sleep mode of the d1 mini is not usable we would have to modify the hardware and separate the voltage regulator and use our own for it to avoid all this tinkering i would like to design a real pcb with smd components by myself this time sure there are now also ready-made solutions for around 10 euros and my solution is not much cheaper this time but i decided to create my own design to be independent from a manufacturer's cloud and to be able to use the board also for other purposes than windows and doors besides this project was my first step into smd soldering and i will show you that there is no reason to be afraid of it but first let's have a look at the schematic on the german website esp8266 server by michael twarkin i came across this circuit the idea is to run an esp8266 which spends most of the time in deep sleep and wakes up only briefly when the state of the connected magnet switch changes it then sends the new status to the mqtt broker over wi-fi and goes back to sleep as soon as possible for example we can install the circuit next to a window and equip it with a switch on the window itself there is a small magnet that activates the reed switch when the window is closed if the window is opened the reed switch opens as well the challenge is to make an esp wake up only when the state changes but first let's look at what is required to run a bare esp8266 for the microcontroller to work the appropriate power supply pins must be connected to 3.3 volts and crowned also for it to boot normally the enable pin and gpio0 must be connected to 3.3 volt with a 10 kilo ohm pull up resistor also gpio 15 with a 10 kilo ohm pull down to ground but in order for it to wake up from deep sleep we still need to connect gpio 16 to reset so the esp can wake itself up by pressing the reset button when it is time to wake up a different way to restart the esp is to disconnect the enable pin from the power for a short time and this is what michael circuit does to understand the brilliant idea behind his circuit i rebuild it with a free program circuit js circuit js even works online in the browser but i prefer the offline version as we see in the simulation of the circuit there is a voltage on the wire at the bottom by default if we change the position of the switch the voltage is interrupted and is back again shortly afterwards this wire is connected to the enable pin so when we change the position of the switch the esp8266 will restart because the voltage will be interrupted for a short time let's have a look what the upper pin does this becomes high when the switch is pressed and low when the switch is open how is this achieved these are two xor gates xor means that the output is high when only one of the inputs is high if both inputs are high or both are low the output also remains low if the switch position changes now for example because we press the open switch now this leads to the result that both inputs of the second xor gate have the same signal for short moment as now low this is due to the fact that a capacitor is built in here which is charged first depending on its capacitance it goes faster or slower until the new signal also arrives at the input of the xor gate and thus also the output becomes high again if we open the button again the capacitor now briefly feeds the input of the xor gate and this again leads to a brief low at the output in the end the first xor only passes on the switch position to the upper wire perfect so if we now connect the lower line to the enable pin of our esp it will be reset shortly when the switch position changes we connect the upper line to say gpio 13 of our esp and so we can read out in our code what the new switch position is now maybe a few words about circuit js the operation is not always very intuitive but in the end is quite simple you can choose in the context menu what you want to put next into your circuit for example cables a resistor a led and ground and a voltage source you can click on view in scope at any point in the circuit to see the voltage and current over time okay now it's time to get into the details of our circuit for this i use keycat it first allows us to draw the schematic similar to what we just did in circuit js then we assign actual components to the schematic elements so that the 10k resistor now becomes a smd 10k resistor of the size 0805 after that we can start the actual design of the pcb but one after the other let's take a closer look at the schematic here below we find the circuit that we had just simulated a moment ago instead of a switch i have already specified here that it should be a read switch this one closes when a magnet is nearby and opens when the magnet is out of reach this is called normally open note that there are also normally closed reads but we won't use them here to keep the circuit simple i'll only show that the upper line is connected to gpio13 i take up this here above at the esp8266 i also added a flash and a reset button here and let out the rx and tx pins so that we can still connect the esp to our computer later via a terminal strip to reprogram it last but not least there is a voltage regulator here in the upper left corner that adjusts the higher voltage of the lithium-ion battery down from a maximum of 4.2 volts to 3.3 volts the ht has the particular advantage that it consumes almost no power even when our esp is in deep sleep but to prevent the hg7333 from being overwhelmed by the voltage peaks when the esp is booting we support it with a 1000 microfarad electrolytic capacitor and a 100 nanofarad ceramic capacitor once the actual schematic is created we switch to assign footprints this is where we can assign a specific element to each logical component where possible i would like to use smd components these are available in different sizes i personally find that 0805 components can still be soldered very well by hand and with bare eyes and with this you can make sufficiently small circuits if you have problems with your eyesight you should use bigger components or work with a microscope since the smd components only costs a few cents per piece it is worth to buy a kit of the standard components like resistors and capacitors additionally you need a few more electronic components these are especially the naked esp8266 modules the read switch capacitors resistors buttons and a logic chip with xor gates as power supply we use the lithium-ion battery for charge regulation and protection against deep discharge i use the tp4056 which can be bought from the far east for less than 2 euros you can find a list of components in the video description as you can see the selection of available parts in keycard is huge and you can even add your own parts if necessary so now we assign the desired components to the logical components then we can switch to the design of the pcb by clicking on the update pcb from schematic button we get the elements from the schematic below which are logically connected within lines we can move the elements with the m key first and rotate them with r now we connect the elements with traces since we are designing a double-sided pcb the elements on the front side are shown in red and those on the backside in green if we want to run a wire from the front to the back side we add a wire this is a small hole in the board that will pass the electrical connection through the board we can also assign the terminals whether it is crowned 3.3 volts or 3.7 volts for example after some fine tuning or board is ready and keycard even provides a simple but nice rendering of the board when we are satisfied we can generate the so called gerber files via file plot we can upload these as a zip file to a pcb service provider to have the board professionally manufactured if you accept the patience for the longer delivery times and order the pcbs in the far east you currently pay about 10 euros for 10 of these pcbs and the quality is quite remarkable all you need to do is upload the gerber zip file and select the properties of your board such as color number of layers and materials used after that you get a preview of your board and usually a plausibility test is done by an employee before production this is what the delivered board looks like in reality let's get straight to the assembly this is easier than expected we start by applying some flux to the pads it is a good idea to use thin solar wire 0.5 millimeters is sufficient for my purposes moreover you should not solder too hot usually 350 degrees celsius is quite sufficient then we place a small amount of solder on one side of each component then we place the component melts the solar again and we have fixed it in place you should always start with the smallest and flattest components and proceed to the higher components one by one now we put some solar on the other side and we are done with a little practice this is done easily and quickly we also fix larger components to one pad first and can then quickly and easily solder the other pads on youtube you can find many good examples how to solder smd ics with many contacts even faster by the way i do not use the perfect soldering tip here many experts recommend round or flattened tips for smd soldering but as you can see it also works pretty well with my tip be very careful with the reed switches the glass housing is not strong enough and will break if you try to bend the legs when we are done soldering we remove the remaining flux on the board using isopropyl and an old toothbrush this is how the assembled board looks like when it's finished you can program the board using the pins with a usb to ttl converter the code is quite simple so i won't explain it in detail this time it should only be said that it is worth to have a fixed rp address assigned the router to reduce the time needed to connect to the wi-fi to a minimum that's why i also avoid serial print outputs and the like you can find my code on github if you like my video subscribe to my channel and give me a thumbs up and feel free to send me a donation now it is time for a small test run i have already prepared my note red to receive the new mqtt communication when i hold my magnet to the read contact i now get a closed and when i pull it away again i get it open nice now i'm interested in the current consumption for the low current of the deep sleep my multimeter is not perfect but it is good enough for an estimation when the esp goes into deep sleep the current drops back to less than .03 milliamps after a while which is about 30 microamps i wrote myself a little spreadsheet to calculate the expected battery life while the device is awake it consumes about 80 milliamps this phase takes only about 3 seconds with a fixed ip address my cheap lithium ions have only about 1000 milliampere hours if the device wakes up once per hour on average i have a calculated battery life of about 1 year good lithium-ion batteries usually have more than twice the capacity and whether you really open your window 12 times a day and close it 12 times is also up to you the runtime could therefore quickly increase to several years but in this case i would recommend normal disposable batteries my lithium ions are only worth it because i ordered them cheap for 1 euros each i also designed a small case you can find the links to all the files as always in the video description print the case install the circuit board with battery attach the whole thing to the window with double-sided tape and attached a small neodoo magnet to the window and our window sensor is ready of course there are many more applications for the device if your mailbox is close to your wifi you can of course equip it with the device as well with an additional interface you can also use it to check your doorbell or to monitor whether your awning is open or closed the rest of the intelligence can be added with no threat or home assistant for example send me a warning if it's raining and the skylight is open at the same time or if the last person leaves the house with an open window or if a bathroom window is open for more than 15 minutes perhaps depending on the outside temperature of course you could also have your smart radiator thermostats turn down when the window opens or your mailbox sensor tells your esp32 cam to send a picture to your smartphone when a ladder is dropped in click on the link to watch my esp32 cam basics video with this you can build your own front door camera for less than 10 euros anyhow the board shown here is the perfect basis for many other projects washing machine heater candy car board flood sensor cat flap i am sure you will come up with many more ideas how to use the board for many more purposes for example with the help of an additional switch or a transistor i have no doubt that there is still a lot of room for improvements soldering the battery and fix it with hot glue is certainly far away from perfect a permanently mounted battery case would definitely have been better and whether you should use the lithium-ion battery if it is only charged every one to three years is also questionable there are of course also xor ics available in smd design and wi-fi is actually total overkill for this purpose so there are a lot of possibilities to optimize the design post in the comments what you would have done differently and feel free to optimize my design for your purposes and needs so forks on my guitar project and remixes of my case design are highly welcome and this brings me to the end of this video a special thanks to estrada and william who sent me generous donations i appreciated it very much if you want to do the same you can find the donation link below i am also happy about small donations have fun with tinkering and see you next time
Info
Channel: MakerMeik
Views: 169,841
Rating: undefined out of 5
Keywords: Window sensor, Door sensor, ESP8266, ESP32, Microcontroller, Arduino, PCB design, KiCAD, CircuitJS, Circuit Simulation, SMD, soldering, deep sleep, quiescent current, Li-Ion, XOR, SN74HC86N, TP4056
Id: vxbuO1zWo3w
Channel Id: undefined
Length: 17min 57sec (1077 seconds)
Published: Sat Oct 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.