#229 ESP32 - Saving Variables Across Deep Sleep

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and welcome back yes now today we're going to be talking about sleep modes in esp32 devices and specifically deep sleep mode yes indeed deep sleep because if you're running off battery that's probably the mode you want to be running your esp32 in when you're not actively doing something now you can of course turn off the wi-fi and bluetooth separately to save a better power but if you're doing that you're still using power running the rest of the processor and the memory and all that there may be a use case for that but um i would say that if you're going to power down the esp32 to save battery power then you probably want to shut down everything except the rtc the real time clock because that's the thing that will wake up your device either because the time has expired or because you've either pressed a button like this one on here or like i'm doing in my smart workshop heater controller the remote temperature sensor is also going to be detecting whether or not the power's on so when the power comes back on a pin goes high because the external power has come on and the rtc says whoa powers come on i'll wake up the device and then my logic within the program has to say why have i started why am i here have i have i started as a reboot completely you know fresh at the factory almost or was it a deep sleep restart in which case the logic of your program could be subtly different some cases quite a lot different i want to do a big shout out for pcb way pcb prototype the easy way now the first thing i want to bring your attention to is the um smt ordering 30 for up to 20 pieces so if you're a small business or want uh a sample run this is ideal for you not only that but they can do flexible pcbs you know those bendy things you got from finding cars and specialist machines they will do those for you too and if you're really at the high end of stuff advanced pcbs pcby have got it all covered if you need it they've got it finally they can order custom parts now this is a really exciting area that they're doing you can have online cnc machine services 3d printing and sheet metal fabrication now as you can see here they do plastic moldings aluminium stainless steel a lot if you need some small prototype runs this is obviously the place to come to i previously mentioned the smt assembly service that they offer let's have a closer look because hobbyists can make use of that as well for just 30 dollars you can get up to 20 piece assembled and they'll assist with getting your parts sourced and fitted i mean it sounds ideal for those small prototype runs it's definitely worth checking out what pcb way i've got to offer find out today ok so what this demo is doing is staying awake for 10 seconds of flashing that led and when the led stops flashing it means it's gone into deep sleep mode right now and the only thing that will wake it up is the either the expiry of the real time clock which i've set for 10 seconds or we can press that button like that and it will wake up immediately now let's have a think about deep sleep if you've ever done it in arduino mode you can put the arduino to deep sleep and what's the maximum you can ever put the arduino to sleep four hands up yeah you're at the back yeah exactly eight seconds yeah that's the maximum after all the arduino is an 8-bit device so it counts up the milliseconds in a little tiny variable and after eight seconds it goes that's it time's up that's the maximum you can do it shorter but eight seconds is what you do and then in fact that's exactly what i do for my my uh storage bin lids out there when they're raised up they get switched on and every eight seconds uh they go well they've gone to sleep and they come out asleep going oh the bin's still up i'll send a warning and then go back to sleep again and eight seconds is all you've got okay now if we fast forward to the esp8266 the smaller brother of this one here they can go to sleep for about i think it's about 71 minutes which is quite a bit more but still not really what hobbyist wants is it there's quite a few use cases out there where they want to go to sleep for an entire day and every 24 hours wake up so what's the maximum amount of time then that you can go to sleep on an esp32 and i'll give you a clue all right we're counting up microseconds in a 64-bit variable so how long do you think that is a week a bit longer month a year no you can actually sleep if the thing doesn't crash on you for five years and really five years well that's if your battery lasts of course yeah you can put your esp32 to sleep for five years if you really want i don't suppose anybody's going to who knows but it's nice to know that effectively the sleep time deep sleep time on here is unlimited it's great i mean you don't have to think about it you go yeah go to sleep for two hours go to sleep for 24 hours 48 hours whatever good stuff okay so in order to get this little demo running then i've wired up this leds that just flashes in the loop that's all it's doing waiting for the timer to expire until it goes oh there we are looks gone to sleep now for 10 seconds and after 10 seconds the real time clock says right your time's up i'm waking you up and once it's woken up it then just goes around the loop again another 10 seconds and then goes back to sleep okay and as i say the other way we can wake it up is by pressing the switch which which is also attached to the rtc yes there we are it's attached to the esp32 as well but also the rtc has access to that gpi open and there are only a few available okay let's have a look at the output log then the serial monitor output to see how this looks in in real life right here we have a blank cool term monitor now the thing's asleep at the moment so when it wakes up it's going to start putting stuff out into the serial monitors there it goes now the thing that we need to look at is um why are we awake yeah the first thing it does is say look this is reboot number 135 yeah i know it's been running a while and the wake up was caused by the real-time clock timer so that means the 10 seconds or whatever i put this asleep for it said right time's up go yeah uh now obviously it's gone back to sleep again if i don't know it's awake again let me just press the button when the flashing is stopped and we'll see the different message not rtc timer so if i press it now there we are look wake up caused by external signal using rtc io which basically means one of the pins went either high or low whatever you've specified and yeah you pushed it great so those are the two that i'm using because they're the two most likely all the other stuff is available in the um expressive and or engineers web page that i'm going to point you to because it's it's good reading and you your particular project might need a certain tweak or whatever as to what kind of sleep mode you need but just to reiterate on this one everything on that esp32 dies power off until the rtc wakes it up okay that's the only thing that's running but it will always go through the setup as though it were the first time so your code has to say why am i awake is it a reboot or is it something else was it pna was it pin b was it the real time clock timer that woke me up and take the appropriate action within that now you're probably thinking if everything's dead and then comes alive again all my variables that i've set up are all dead as well absolutely except there's a solution for that you can put some of those variables into the real-time clocks sram okay and you just i'll show you how to do that in the code walkthrough you just say put this variable in the real-time clock sram please not the esp32s one and that works very well but there is an 8k limit okay and remember that if you press reset or the power dies entirely you've lost the lot so you've got to take care of that maybe put them into little fs or spiffs or something on the actual device even an sd card if it's important if you're logging you know b's in the beehive or something you don't want to put the count into the real time clock sram and then have that die on you because all your information is lost okay pretty simple we'll do a walkthrough and i'll show you the code and it really is simple i urge you and recommend that you download the sample sketch that i've written and which most of which was taken from the espressif documentation i might add and just play about with it and see how easy it is to put your device to sleep however do bear in mind this is very different from arduino deep sleep you put your arduino into deep sleep yeah for a maximum of eight seconds but when the arduino wakes up it can it resumes execution the very next statement in your code with all the variables intact it's a very different deep sleep on the esp32 bear that in mind now uh a couple of announcements okay announcement time announcement one number one when i moved house at the beginning of this year or february 2021 i couldn't reply to all comments as you know that i normally do so there are lots and lots of comments still sitting there unanswered and i'm not going to answer them now because obvious months ago so i'm going to remove all those unanswered comments all right so don't take it personally if your comment suddenly disappears if you can remember actually writing it because i'm going to clean up my own backyard as it were and get rid of all those ones so that i don't have any outstanding comments because youtube tells me that you've got all these comments you haven't answered yet and it gets in the way and muddies the water for me to see the comments from say this video all right so that's the first thing announcement number two over the years a lot of people have said where's your patreon page so i can support you ralph i really want to support you and i've said not happy with that i don't think i'll go down that route yet and they go come on we're going to win okay i've now launched or joined if perhaps would be a better word the buy me a coffee organization right so if you think i've done you a good turn you think yeah i'm going to support you either as a one-off or regularly you can go to my video description down there all the github up there i don't know why i keep pointing it up up there anyway um and you can buy me a coffee now the price of a coffee a single coffee is five dollars which makes it quite an expensive coffee really doesn't it but i don't get all of that some of it goes to the buy me a coffee company or organization whatever they are some of it goes on paypal fees so i end up with about 4.34 um and in pounds that's about 3 pound 20 or something right so it's still a reasonably priced coffee but that's that's what i get okay so that was announcement number two announcement number three esp32s behave differently today than what they did say three months ago why okay the answer is in the format of the flash mode accessing now if we take a typical flash mode set of um tools let's have a look on the arduino ide and you'll see what i mean right there you can see a snapshot of the parameters that you can specify when you have got an esp32 right the frequency uh the core debug level which i strongly recommend you put to verbose in the first instance because there are some debugging messages behind the scene written by espressif that you'll never get to see otherwise and they are very very helpful when things go wrong okay flash frequency normally 80 megahertz if you have problems you can put it down to 40 but i've never had a problem now flash mode that's the thing that's changed okay now a year ago when i did my esp32 web radio for example i always but always put it in as qio in fact every single esp32 i've ever owned has been set to qio on the flash mode quad input output uses four lines on your esp32 at least and it accesses the the flash very quickly and it's the most efficient way of doing it except there was a bug when you specified qio espressif seemed to interpret that as dio so you thought you were running in qio mode you were actually running in dio mode and that that worked well i guess it's slower but i guess would we notice you know it's a good question isn't it and um they fixed it now so that what was qio is actually being output as qio except what happens to every single one of my esp32s is that they go into continual reboot you may or may not get a few lines of sort of debugging but basically means i'm fubar i can't and it'll just it would just keep looping and looping and looping of course that caused me quite a bit of consternation and trouble and i've i went on to the espressif uh github site and asked the question and a few people came back and going oh is it this is it that you know power and all this sort of thing but gradually between us we sort of narrowed it down and i suddenly realized when i was i reverted back to framework 3.30 and at that point i realized that although i'd said qio at the at reboot if you look at the boot messages on the esp32 it's coming out as dio all right not qio so i thought ah so went back to framework 3.3.2 or whatever the latest one is and it said oh no i'm ending qio mode and then the reboot started so i thought okay what can i put it until i put it back to dio as soon as i put it back to dio it came out as dio and it worked i thought well is there a faster one more q out quad output is a little bit faster only second to qio and that works fine so if you've got problems with your esp32s and you've specified qio previously you may have to revert to q out okay dio is slower and d out is the slowest of the lot i believe i'm not totally sure on that anyway q quad is better because it uses more lines so it can get the data quicker i guess in parallel and finally announcement number four uh on this little crib piece of paper where i had to tell you about qio that little device there anybody identify that i don't know how close i can get it before it goes out of focus but yeah it's um a voltage regulator an smd voltage regulator in fact it's identical to the voltage regulator on this board here and it's that little black blob there guess what happens when you connect the 3.3 volts output of this voltage regulator to ground does this still work afterwards well i think the clues in the fact i've had to desolder it and i'm waiting for some spares to come from rs components later on today and as you can see in a little video there i've had to cobble together a microchip mcp 1700 device 3.3 volts just to let my smart heater controller continue to work in the meantime because i don't want to solder up a complete new board yeah so be very careful with your multimeter probes when you're touching the 3.3 volts on those boards okay enough of these announcements let's go and have a look at a very quick code walkthrough shall we okay let's have a quick whiz through the code all right not necessarily line by line but enough to get you going i think okay so at the top this i'm using the arduino ide 2.0 okay so the new version this is beta12 so the top look and um yeah it's it's slowly getting there it's slowly getting there the emphasis on the slowly i think yeah beta 12 gives you um the uh control over this bottom window let's see that can the output whatever it is you can you can turn that on and off now which is great gives you more room for coding right so let's start at the top and work our way through um i do have to include arduino.h otherwise you get red squigglies everywhere i don't know if they're going to fix that ever or you have to put in forever i don't know our led pin is defined and our button pin do read the comments here about which pin you're using to wake things up okay a few variables here that we're just using as defines now this is important when your esp32 goes to sleep as i say the memory and the code and all that just dies it's just garbage all right when it wakes up there's nothing there so if you want to retain some variables across the deep sleeps you must put them into the rtc runtime memory and you do that by typing in rtc data attributes in front of your declaration just like that and so this inch boot count will be available whenever i wake up and i'm just doing that as a demo and we'll output that boot count every time it wakes up from deep sleep or push button and it will increment if however you press reset or the power dies and it restarts all those variables get wiped out so do bear in mind that important stuff if you're logging stuff for example don't put them into the rc rtc sram put them into um spiffs or even better little fs of course much more much more robust okay so the setup what are we doing the setup apart from starting our serial monitor up the first thing we do is get the wake up reason why why am i suddenly awake yeah we need to know that so that your code can take the appropriate action within it yeah you might wanna if it's a brand new startup from powerup you might say well i'm gonna do all this stuff but if it's just a wake up you might think well i've done all that i don't need to do all that again i don't need for example i don't know format an sd card i don't want to do that i just want to keep writing to it as an example okay to find a couple of pin modes the led is obviously an output and the push button i've said it's a an input one because obviously we're we're bringing that to ground so we're making it low and that's it all done okay right so the loop which is this tiny bit of code here basically it's just just hanging around it's just doing nothing except flashing the led in this little bit here and then counting to go have i been in this loop for enough seconds yet before i can go back to sleep and the seconds to hang about is it's not going to show me is it the pop-ups have disappeared now on variables i think the seconds to hang about is 10 seconds all right so 10 seconds times 1000 milliseconds and if that's true it goes to sleep right and the led then will stop flashing and the go to sleep routine which is the one down here it basically enables two ways to wake up again one is to say by the external rtc clock gpio pin and there are a few of those and also by the wake up timer and the seconds of sleep i think once again is 10 seconds yeah it's really annoying that they've broken that and they it's not telling me what that is i think it's 10 seconds and we multiply the seconds by microseconds per second so i could have typed in here a million but magic numbers let's let's keep it as a proper defined variable in fact i stole all that from the espressif documentation on deep sleep just well it just makes it look better doesn't it and then you just say go to sleep and that's that's it bang your device effectively switches off apart from the rtc which just trickles along very very slowly tiny amount of um current it requires on that one with the led and everything else is say almost three milliamps to two milliamps or something but most of that is probably the led on it to be quite honest okay and when it wakes up it says why am i awake well this this is what we uh execute this one here get wake up reason and basically it's the case oh there's the boot let's do that bit first this is reboot number uh or percent d so remember this is printf okay log w i'm using let's explain that first within the esp32 framework or expressive framework generally they've got log underscore something so you can have log underscore e for errors w for warnings d for general debugging and v for verbose and um you switch that on and off within your sketch itself okay now i'm this is a warning so if i didn't want warnings to come out and i said no i'm only interested in errors by declaring a certain variable at the top of my sketch this would not produce anything at all okay it just wouldn't happen which is very similar to the video we did a few weeks ago about um getting rid of serial.print stuff isn't it right let's let's think about the reason why we've woken up then so what we're saying is get the sleep wake up cause it's just a variable that um the sp32 knows about and then we have a switch case uh construct here and it tells you why it's woken up for us we'd expect either we've opened up by the io so real time clock i o push button or by the timer which is this one here we wouldn't expect this or touchpad because we're not using that or ulp but we're not doing any of that i'll let you read those up in our own time yeah and if we pressed reset or it powered down completely you know the power failed and then restarted we'd get this here and chances are the wake up reason would be zero at that point and that that is it that is all the code there is to demo this it's pretty pretty simple isn't it really yeah there's quite a few comments in there so read those and download it just have a play really it's it's a nice little thing to play with now as i say there's more to to sleep modes on the esp32 than just deep sleep although i suspect that is the one that most people are going to go for if you're really keen on saving battery power the one here on my workbench that i'm going to be using for my smart heater controller remote temperature sensor that also detects when the power's on in here when the power's off and i'm running on battery power i want the maximum battery power saving don't i so it can run all night so it would definitely be going into deep sleep which is where this video came from and it works absolutely great um the actual device that i'm using is very very similar to this it's not the same shape or anything but it's it's similar and it takes overall three milliamps when it's in deep sleep mode which isn't great i mean it should be down to sort of a half a milliamp 150 microamps but then of course remember we are powering the entire board up as far as it needs to be powered up which includes i guess other components and there might be resisted network dividers and things all of which take a little bit of power yeah no leds though thank goodness and it works fine i've got a little battery and i'll show you all that in due course when i do my smart heater controller video so esp32 deep sleep i think is the way to go so please have a look at the code download it there'll be lots of links either down there in the video description for you to look at as well as being in the github i'll do a couple of pdfs or something so in case that information never disappears don't forget you can now support me with buyer coffee if you feel so inclined and do like this video if you like it of course and um yeah i'll i'll reply to the comments once i've cleaned up all that stuff and see you in the next video i hope you're finding these videos useful and interesting there are plenty more videos to choose and a couple are shown below and if you'd like to subscribe to this channel just click on my picture below and enjoy the rest of the videos thanks for watching
Info
Channel: Ralph S Bacon
Views: 10,019
Rating: undefined out of 5
Keywords: Arduino, Beginners, electronics, C++, microcontrollers, programming, gadgets, ardiuno
Id: 919hSPthnb4
Channel Id: undefined
Length: 24min 25sec (1465 seconds)
Published: Fri Nov 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.