#118 ESP8266 Deep Sleep 💥 and Bare Bones ESP (easy)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and welcome back now today you can sell my workbench a number of various forms of esp8266 s we got these ones which are not written on the back here their node mcs VIII's and they are probably the easiest to program because you don't need to press any reset and flash buttons like you do we say the ESP Dino or the d1 we must be one or anything like that just does it all for you which is why I've got this mounted on a nice little friendly node MCU base but we can power it up and let it get on with it then we have the hard path as it might be called because we're going to be looking at this there's Barenaked esp8266 that's going to be mounted on this breadboard and I'll show you how to flesh the code for one and then run it after having put it into deep sleep but it's it's all doable and not a problem but the very first thing we need to do is just take a step backwards on this because if you watched my Arduino deep sleep videos and then over two of those when you come to this one you think what that's how it works when it comes out of deep sleep you got to be kidding me nope the Atmel chips as used in the Arduino Uno and the Nano and all that do it in my view as you'd expect these ones they don't but let's have a look how they do it and discuss the merits and well the cons of doing it their way so let's think about what happens in the Arduino that is the 80 mega three to eight P chips as it comes into the setup it just does whatever it needs to do setting things up initializing reading things back from SD card or anything like that you might want to do and then it hits the loop and it wizards around the loop any number of times so it does this and comes back in again because perhaps you are unaware that there is in fact a program that controls all this that we don't see as part of the Arduino but sometimes you can catch a glimpse of it and what it says is go and do the setup and then go and do the loop forever so that's how the loop keeps getting executed anyway so the Arduino is whizzing around this loop but somewhere in here we say go to sleep please there's a condition that's been met or something and we say at this point here sleep now as you saw in my previous videos this is a watchdog timer sleep normally you can do an interrupt as well but let's assuming it's some on a watchdog timer when this watchdog timer expires this here comes into being this the sleep OSR it's not actually called that a special name you'll have to watch the video to find out what it is and it comes in here and the first thing is basically you've woken up right the time has expired and we say our timer watchdog time yes you're about to reset my machine on you don't do that I'll kill the timer I don't want to know we then end this sleep is our and control returns back to the exact point of where we were before the sleep was invoked did I say before asleep the exact point after the sleep the very next statement basically and it continues on that loop with all the variables and everything else that we've set in there and indeed set in the setup they've all been retained and that's it no problem at all now that's the 80 mega three to eight and it has to be the P version that's the Pico power so anyone that does this properly ok so ok that's then the Arduino how does the esp8266 do this hmm I'm glad you asked that because it's a little bit different so this is now for the esp8266 - whatever right they all work pretty much the same right so we come into the setup routine initialize whatever it is you need to initialize and then you just you just carry on you don't normally go into the loop because the concept is not there anymore yes there is a looped function you can use it if you want to and if your logic dictates you go around the loop a few times before going into sleep that's okay but normally you'd do it all in the setup and at some point in that setup you'd say now I want to go into sleep and we all talked in deep sleep here okay so we're going to sleep and then after a certain number of microseconds we wake up and then what happens so we've got is asleep we've woken up and then we execute the very no no we don't we come back in at the top here so after asleep we come back in to the very top which is like what who designed this chip and basically hits it really is a bit of a hash you mind you I mean yeah I'm sure the people who's on the chip or much more intelligent than me but not to have a proper sleep resumed mode I just don't get it what happens is having come into the sleep mode again you can then say why am I here have I just woken up from deep sleep or is this the very first time I'm through here well okay let's assume that there's a nice easy mechanism for determining why you are here is it because it's the first time power up so that's probably one good reason isn't it power up somebody's press the reset button mmm that's not gonna work and then of course there's deep sleep wake up now there is a way of determining which one of these it is so we come into here and our logic in here now has to say ah this is not my first time through therefore I won't do all the setup that I would have done I'll just carry on a little bit except of course that all the variables in here haven't been set now you really have to do the setup from scratch that means especially with an esp8266 it means connecting up to Wi-Fi for example what isn't it that's one of the things you probably want to do so within the sleep even if you know why you're here you have to be very careful in what you're doing here in terms of power up or deep sleep and go which bits do I need to do now one of the tricks that people use for esp8266 is to say when I connect to Wi-Fi I'm gonna use a fixed IP address do not use DHCP because that takes far far longer to establish a connection so it's a fixed IP address and then you come along sort out a bit via logic and then carry on with the main part of your routine again it's it's nowhere near as nice as the arduino 80 mega 3 to 8p version because there's there's some convoluted logic you have to put into here and well that's the way it is I must admit when I first saw this and realized how it was all gonna work I thought you just gotta be kidding it was just I mean we could do this could be not with an Arduino we could just reset the Arduino set one of the GPIO pins high or low whatever so that the Arduino can go why am I here read that GPIO pin Oh I've woken up from deep sleep I'll go and do all this and it's it's pretty unsophisticated really so there we are that's the way is though and espresso if chose this particular chip that's that's the one there on screen now that obviously doesn't have this resume from sleep particularly well controlled and given that this is such a high power high power consumption chip it's it's a shame really but there we are that's the way it is so that's how it works so let's go and far up that that board this one here in fact we're gonna fire this up even if we put this one to sleep that chip might be into deep sleep but nothing else would be so yeah but it's a good thing to develop on and then put it on to that bear one which is coming up later in video so let's do that next right there are a couple of interesting things than about the no diems heal rather esp8266 is generally okay first of all if we look at I'll connect this up and I'm powering up this dev board with them a separate 12 volt supply and at the moment is taking us have a look if I go on to the camera over there anyway we're picking up perm v 41 milliamps there and it's waiting for me to do something at the moment okay now if we look at the port's window on this is windows I don't quite know how it look on Mac if I bring up my ports that you can see here this is the port that we're on however if I disconnect the external 12 12 volts supply on here they might have heard a noise there and it disappears basically this is not being supplied sufficiently by the USB socket alone even though the little lights on down there and it doesn't help so that has to be plugged in you just heard the noise there we are and it's backing in fine so this is one of the things I've found with the esp8266 is it needs an auxiliary power supply now maybe my hub should be powered it's not but even when I tried one I tried a little one what I tried this one here I'll try to dlink one and I powered it but it made absolutely no difference just just didn't want to know until I powered this externally if you like it didn't spring into life at all which is why I suspect is a lot of people who say they can't get it to work have that problem it might just about load up and go into deep sleep but when it comes back out it just hangs basically so what so you have to be aware of that okay just just a heads up do power it sufficiently this is running on 12 volts I could put nine in there it'd still be okay but it does need an extra power supply right let's get rid of the ports and go to the code window right so I've reset in is waiting for a carrot as I'll just press end there we are and it's going into sleep every well it's a lot quick enough or wash less so kind of sleeping what's it doing this ever have a quick look it should be sleeping for five seconds oh I know what's happened haha this is an old sketch it's not the sketch that I've actually got on my code window as we speak so it's not this this code here at the background that you can see because if you say I've got it as ESP deep sleep five seconds what I was doing on this particular bear code sleeping it for 100 milliseconds just to see how well it worked and how many times I could get it to come out of deep sleep and well it just went for thousands tens or hundreds of thousands of times without any issue whatsoever now as you can see we didn't actually have to press anything I've connected up the reset to GPIO 16 which is what you have to do so basically at the end of deep sleep the timer the RTC on board here brings GPIO 16 low which means if it's connected to the RTC it's the same as giving it a reset pulse and off it goes and of course it does know whether it's woken up from a deep sleep or not I'll cover that in the code and the next bit of the videos is once we've gone through the naked video I show you exactly how that's done but the key of course is that we do know that it is a deep sleep wake up so what it's saying here is look deep sleep wake up so it does know that it's been deep slipped deep slips is that even an expression and it's not somebody basically pressing the reset in fact while it's sleeping we can't do it because it's too quick here but while it's sleeping if you were to press the reset it would actually wake up from deep sleep and go ah I've been in deep sleep that's why I've woken up it would not say you've pressed the reset and start again okay so just bear that in mind if you're testing a program cool okay so that's that's trundling away and we'll just keep going in to do sleeping back out again now if we go to the meter the multimeter and see how much current we're taking now so we've got the whole board here that we're looking at but it's okay let me move that aside there now so what it is it's saying 40 milliamps roughly when it's just sort of running and when it's in to sleep mode it does drop to about well it says fall there doesn't it but it's all a bit quick so let's let's upload the other sketch that I just showed you go over to the code window alright and I'm going to upload this now the good thing as I say about this board is that you don't have to do anything you just just leave it like that and off it goes so let's upload and see what happens right that's uploaded no trouble at all just left it exactly the same way as it is now and it's come up the screen saying send any character so what it's done of course is reread the reason why it was started and is determinate was an external system that's to reset it which basically means power up or even the reset button so if we were to press the reset button now yes he would know that it's an external system no it's done again and now we've got five seconds so this would be the interesting bit now so we'll do this that gives you a first time only then it's sleeping for 5 seconds fine therefore I press reset while it's sleeping look it says deep sleep wake up so it doesn't reset in the usual manner if you reset it while it's deep sleeping now if I can catch it I will have to wait another fun the minute this comes out of sleeping on and press reset this instant now there are external systems that did know because it wasn't in deep sleep anymore obviously the system itself the chip itself had cleared the flag that says I've woken up out of clear out of deep sleep or then press reset and it goes it must be an external system there are many reasons why is it sin that set up routine but the ones we're interested in is well anything that says deep sleep or not really so there are that's that I mean it's all pretty simple isn't it now if you get some if you haven't got one of these boards and well from what we've already seen I would say it's it's almost essential if you want to play with these because you need to power it up somehow don't ya so I'd say get one because otherwise you're just gonna be laughing about but if you even if you didn't you might want to do it like this which is to breadboards put so there's one breadboard that bit and then there's another one they click together where there's little tabs and things and then you straddle it over two of the power supply things and don't use those as power well you could use those as power but I mean because it's covering most of it you might just want to put the power on to here fine but then you've got lots of room having you for other connections that's one way of doing it because they are a very wide board and they do take a lot of pins up otherwise that would never work on a single board because I think it actually goes from the top set of pins to the bottom or maybe one up or whatever it takes to many but two boards like that works rather nicely as me but you still have to power this with something other than just the usb right so what we've covered so far then is that the this this particular module loads fine without you doing anything so that's a good module with which to start it will load the firmware the flash the there's program the sketch onto it no trouble at all and you can run it without making any adjustments you don't connect anything to anything ok so that's for a beginner this is a really good board to get to actually use in terms of sleep mode it's not ideal is it because you've got led you got that one there there's a couple more somewhere that casually come up and perhaps not in this sketch maybe we're safe that one I'm driving but even as it starts up you get a very short pulse first there we are you see you know you want to see that again so when it wakes up after five seconds you see the very short pulse and then the full 500 milliseconds which is us so that short one that one is being generated by this probably trying to attach to a network or something and as we said the the actual current four point three milliamps in sleep mode 40-ish when it's running now yeah that's the saving and if you just wanted to run it for a you know a few hours that would be enough but not if you run it want to run it for weeks on end that's when you need the Barenaked esp8266 12v or something like it you can use no one no one you can use any of the ESP range really but you do need to have it on a separate board let's talk about that next so this is the bear esp8266 12 e module here now don't muck about with it too much because as you can see it's a little bit of a rat's nest but believe me these connections are simple and I will be giving you a proper diagram and some of these connections are only used at boot time or rather at flash time and some at run time so the other boot is two ways of booting this one into ready to receive a new program and another way the usual way which is just to run the embedded program now I have got a diagram already prepared but I'll probably be putting that either into paint or something like that you know paint shop pro or something all that ilk by the end of this video I'll certainly talk you through much more detail now the reason why it's all a bit delicate at the moment is because this is as I say a bare 12 e running in sleep mode and waking up every now and again so I'm going to clear the debug window this is all run from the arduino ide by the way so if i bring up the debug window and it's bare at the moment as you can see and this little button down here this one here is a reset bundle well if reset thee and the 12a runs it so let's just press that now the interesting thing about this of course this says well first of all I'm waking up from deep sleep and this is iteration 12 and now it's going back into sleep mode now I've got it at the moment sleeping for five minutes 300 seconds so this is the the meter that's currently set to milliamperes as you can see but because it's now sleeping I can now switch that to micro amps so now the current is indeed flowing through that meter may you see it's running at 73 micro amps according to my notes it runs from anything from 46 my crimes - well 73 is the highest I think of a very urgently 73 no particular reason why it runs at one setting compared to another just seems a little bit random really just wind down to 68 doesn't it so sleeping this is fine and that's the lowest I can get it down to I can't get it down any lower than that and this is an absolute bear board well I say it's a bear board as we can see in the front there the bear board is soldered onto a backing board and that backing board is simply a a breadboard friendly way of doing it so it would fit into a rough header pins or something like that and also your your breadboard and the anything on here is a couple of 10k resistors there's one there and there's one I don't know somewhere else probably is that it down there whatever and I guess that's just enable the the chip to boot up or something whatever that's all it is is absolutely bare there's no LEDs on their pot from the blue one of course on the actual device itself okay so we've proven then it does go back to sleep and it does wake up while it wakes up when I press the reset button which is an anomaly in itself basically if it's in deep sleep mode this is the only the esp8266 I haven't tried it with the Arduino if it's in deep sleep mode and you press the reset button it wakes up and goes are I was in deep sleep mode therefore I must be in that sort of you know wake up cycle so if you have got one of these built into a real product and you press the reset hoping it's going to reset it from the beginning it won't you'd have to disconnect the power now if we do disconnect the power which we can do quite easily just keep watching that debug window so that's this one here so we can disconnect and now if I reconnect now what it's saying down here now and the exonic as well look the reason for the reset was an external system something resets me in other words properly and yes that was true because it's the it was me unplugging it so now we've gotta send any carrots again so I can just send any old thing here cuz I've got the newline property set so if I press send on an empty string here just end the newline off it goes it's his first time only initializes a loop count and it goes back to sleep cool I'll show you the code in a minute but there's this something else I got to show you first so I'll show you how we can upload that sketch onto that device just by connecting a couple of wires up right this is the code we're running it's the simple blink program slightly modified to make it go into deep sleep so we'll whiz through this basically we're set in the baud rate at the beginning to 74 880 because that seems to be the speed that the ESP itself seems to report when it actually boots up before any code is run at all so I've kept it the same right at the top we've got the built in LED which is pin to be known as d4 so once again there's a double numbering system on here so what we're doing here is saying why are we running and there's a very nice little utility called ESP get reset reason and that is of course the key to knowing whether you're starting up the first time or whether you're coming out of deep sleep and we just printed to the screens ever know what's happening now if we're not waking up from deep sleep then we well first of all pause ever the user to send something to so I can demo its properly but then we're initializing the loop account and writing it away to the real-time clock memory on this device that survives a deep sleep it will not survive a power down though that's what we do now this here we're casting the value here to an unsigned integer 32 now in this particular sketch we don't need to because I've actually got an unsigned int here but if you were trying to store anything else it'll always right away the two bytes size of loop account that means how many bytes are we taking up and in this case we're taking up two and it is a deep sleep wake up and we're just saying go and get me the loops out loop count and print it out what iteration it is and we'll just increment that a bit later now coming out of a setup we're going into the loop we don't have to all this code could be written in the setup and indeed many examples are but it have to be you could be running the setup then doing a loop of many times voided sided ah now I need to get into deep sleep as long as you remember that of course when you wake up you'll always get start again from that set up so we just flash the LED on and off remember the low and high had reversed compared to the arduino z' it's a low B's on and high is off update the loop count write it away exactly the same as before I'm writing away at memory location one that's because I was using memory location zero for something else but I didn't need it the other day are then just sleeping and I'm sleeping here for there are five seconds now you can see down here I've just flashed II actually within it started back up again from deep sleep mode because we hadn't killed the power the real-time clock bit survive that so when it started up because ah I've just woken up from deep sleep which is total nonsense it hasn't of course so I thought I just show you the flash mode make sure it actually works so we'll make this a little bit bigger here then you've seen the code and I'll connect up the power so the power goes to this one here and I can say flash but running that so shouldn't compile very much so we've just done it right it's trying to go into the flash mode now here is that's writing away all the settings by the way for the flash I'll put in the github but just by clicking on the tools you can see all the settings here quite easily right that's it that gonna complete there we are so we're done if we go back to the debug window so what that's saying is now send any character which you've seen before but what the difference now is that we're just going to sleep for five seconds so uh send that says I'm sleeping and then after five seconds without any jiggery-pokery there is it says um rebooting it's hung how it's probably done something there's meter let me just let me just take this meter out of the equation because that really does muck it up and we'll be right back well actually actually I was blaming my meter which which could have been the cause admittedly but it wasn't that time it's because of course we've left it in boot mode my so GPI zero is this why here we go back to workbench so we've put into boot mode by connecting gpio zero to ground so that one can come out and we've connected GPI two to VCC 3.5 so they can come out right so now if I press reset it should say first time and then come out sleep as well right so we'll send a character right sleeping and it should now come out of sleep because we're not going back into boot modes here yeah so it's come out of deep sleep wake here it is and it's iteration one which is and then deep sleep wake and it's iteration two and now I've had them not this particular one it's the previous one that you might have seen it's this this one here that one ran for thousands and thousand tens of thousands if not hundreds of thousands of sleep modes without issue whatsoever right I think that's proven that this works the code that demonstrates this is is the simplest in the whole because it's the blink program plus the bit that says you know write to the user memory so we don't lose that fact cool okay let's move on right let's just talk about a couple of other little things that haven't come up in this video so so things we haven't covered in so far here's the we mas d1 and this is an ESP did we know both work in what I can tell is absolutely identical ways you can upload that code just the same and it will run exactly the same and these were both going to sleep but once again because these boards take so much power anyway is almost a pointless exercise yes it will same sub power some power it will but given that you know 5 10 20 Mill even is being consumed by LEDs on these boards it's not ideal good for developing on again though but then you would have to transfer it to one of those naked ones to get the real power savings now we mentioned also on both boards the naked one this one and this one that they needed additional power supplies and it wouldn't work even when I connected up this this lithium-ion cell from three point three just would not work and it wouldn't run just from the power on here this is an unpowered hub that it went into but it wouldn't work on that only though so okay let's let's talk about this a bit more then let's clear the decks and think about the connections from here so this end goes into your USB cable that goes into your PC or hub or something and these are the ones that go into your bare naked yes p12 here okay so on this this board you can just about make out that the first pin on the right well the left hand side as we're looking at it is ground CTS we don't use and you've got VCC and then you got TX then Rx and finally over here you've got DTR which we don't use either so in fact that green one is not being used as part of the ESP we had to use that as part of or do we know one but not here so there are four wires okay how how does that translate then back down here so TX on here which is the orange goes to the rx on here in about two or second room left and all right yellow goes to TX which is the first one on there that's fine ground goes to ground VCC to VC say do you make sure you've got this set to 3.3 volts in fact it just has three volts on a rod the three points ribbit do not set it to 5 volts because you'll immediately cook this it will not stand that not even for a split second so do be very careful now I did read somewhere that GPIO 15 supposed to be connected to crown but that's Tosh frankly it didn't make any difference I don't see why it would GPIO 0 needs to be connected to ground when we want to flush it as does GPIO two need to be connected to VCC when we want to flash it okay so to flush it you just connect them in as it shows you here this will then on powerup go into like flashme mode and that's it then it will flash and then it'll reboot and it will actually start running at that point now at runtime mode because the timer on the chip sets GPIO 16 low at the end of the timer setting you know 5 seconds 100 milliseconds whatever you need to connect that to your reset pin here which then effectively is the same as you press on that button no different all it just grounds that reset button for a little bit a pulse this springs in to life and it restarts and the only good thing is that the the chip itself must use some of that real time clock memory because some of those memory locations are reserved for system uses as well ESP say that and I'm guessing that's where it stores the information so when it goes into deep sleep it must write to that the same as we are and that's how it knows that it's been in deep sleep so DTR we're not using CTS we're not using that's it I mean it's it's simple but made more complex because on my setup I a GPIO to floating around really to being a connected to VCC and GPIO ready to be connected to ground if you take those two away you've only got four wires and yeah okay that's it as I said I'll put this picture either as ease or enhanced or whatever up on the github so you can print it out which is probably a good thing if you're playing about with this and all will be well okay that's it then for this video I hope you enjoyed it remember to give me a thumbs up because YouTube like that when people give thumbs up to videos lets them know that people are watching it put comments likewise YouTube like it when people comment on videos because it means a lot of interaction going on I do try and answer every single comment and write how all that thank you for watching this video and see you in the next one 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: 28,863
Rating: undefined out of 5
Keywords: ESP8266, Deep Sleep, Sleep Mode, Bare Bones, Wake up, esp easy, low power, Arduino, Beginners, electronics, C++, microcontrollers, programming, gadgets
Id: NgPwPKXCLFY
Channel Id: undefined
Length: 34min 17sec (2057 seconds)
Published: Fri Jul 13 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.