#33 Internet of Things with ESP8266 #5: Watchdogs, Timers & Stability

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
create see youtubers here is the guy with a Swiss accent again our IOT device can now get data from the smartphone get data from the web and be programmed over-the-air in the next step the user interface has to have fast reaction time and I want that my thing can recover from a bad or blocked state without any human interaction this is the goal of today's episode to create such a user interface for a push button and to stabilize the device as much as possible everybody with windows experience know how to repair an unresponsive or blocked computer we hard boot windows and usually the problem disappears the same concept is used by small chips if something really goes wrong we reboot the chip but of course we cannot do this manually like with Windows we have to use an automated procedure to do it a commonly used solution for this problem is called watch dog watch dogs generally use interrupts for those of you who do not know the concept of interrupt all microcontrollers support this concept it allows to interrupt your sketch without your knowledge all Arduino zhh use this concept to update the Milly's and micros counter you do not need to do something these counters are always up to date this is done by a timer in the microcontroller chip itself and these timers run more or less independent from your sketch this fact can be used to build such watch dogs a watch dog is a simple timer which counts up every millisecond or every second as soon as it reaches a pretty find value it automatically resets the microcontroller and it reboots from scratch exactly as with Windows here is the example sketch we use a library called ticker we initialize a ticker with a name second tick and attach the subroutine is our watchdog is R stands for interrupt service routine this name is used to show that this is not an ordinary subroutine it will never be called by our sketch these two lines of code make that subroutine is our watchdog is executed every second and in this subroutine we count our watchdog timer up if it reaches 5 the esp8266 is reset it by the command ESP dot reset if we start the sketch we see the effect every 5 seconds the watchdog bites and resets the esp8266 but you might say now this is not a very intelligent behavior because it makes our thing very unstable because it crashes every 5 seconds but what would happen if we feed the watchdog before it bites let's try this we set the watchdog count to zero in each loop but before we can do that we have to declare our variable a little bit different if we want to write to a variable from our sketch and from our is our routine we have to declare our variable as volatile these things said and done we upload our example and start it now watchdog count never reaches 5 and the chip runs forever this effect can be used to stabilize our let's assume we feed our watchdog at the beginning of our loop during normal operation our loop will never use five seconds to execute only if something bad happens and our code is blocked somewhere we do not come back to the place where we feed the watchdog and after five seconds it is really hungry enough and bites and the ESP reboots and get again into a stable condition this concept of course is not a protection against bad coding but it is a protection against unforeseen effects just as with the newer versions of Windows where we have to reboot only rarely and this is exactly what we want for a device which has to operate without any professional interaction by the way the very same concept is used by the esp8266 itself all wifey traffic is handled in the background without our knowledge if you block now these background works with your sketch the watchdog in the ESP cannot be fed and resets the chip blocking the background jobs is very simple you start the wifey and stay in this loop then the ESP crashes were quite fast soft wdt reset means translated the watchdog bites if you insert the command yield the ESPE runs stable because this command invisibly gives the wifey in the background the chance to get its work done and of course feed the watchdog before it bites if you heart boot your windows after a severe problem usually you lose all your work if this is not acceptable and in my case it is not acceptable you have to include recovery routine in your code I will not cover this topic on YouTube interested Watchers however find the needed ideas in my code on github now we go to the user interface when you start programming arduino s-- one of the first commands you use is delay this is a very handy command to slow down the chip if needed unfortunately during the execution of the command delay the chip is completely blocked if you for example press a button during this time the chip is not able to see it because it cannot read input signals during the delay therefore delay is not used if your sketch has to be responsive in this example the sketch reacts slow to the pressing of a button and short presses are completely ignored if we do not like this behavior we have to use other methods one possibility is to use while loops you see here we have the same functionality but much better responsiveness of the push-button and no delay command is necessary another possibility is the usage of interrupts like we used for our watchdog in this example the loop contains only the coding for the push-button the ISR completely takes care of the printing never print in an ISR routine because printing takes a long time is our routines have to be very short and fast I just did it here to keep the example simple and to the point for the moment this was the last video on the topic of Internet of Things with esp8266 you find a link to the code in the comment the next episode will be a short one I will demonstrate how you can increase the flash size of your ESP the a1 modules to 4 megabyte this is particularly important because with this memory size you can use upload via air and can keep the scarce GPIO s for your application I hope this video was useful or at least interesting for you bye
Info
Channel: Andreas Spiess
Views: 66,680
Rating: undefined out of 5
Keywords: ESP8266, Timer, Internet Of Things (Industry), Electronics (Field Of Study), Do It Yourself (Hobby)
Id: D_7ciW_TCac
Channel Id: undefined
Length: 9min 17sec (557 seconds)
Published: Sun Dec 06 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.