#299 Tricks to get NTP time for the ESP32 and the ESP8266 incl. Summer- and Daylight Saving time

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in many projects we need accurate local time with our ESP chips we have some libraries to get NTP time as shown in video number 67 unfortunately most of these libraries do not adjust for daylight saving or summer time today we will change that by using standard ESP functionality and we will see if hippies were involved in this matter pretty youtubers here is the guy with a Swiss accent with a new episode and fresh ideas around sensors and microcontrollers remember if you subscribe you will always sit in the first row today we will create a small sketch that gets local time adjusted to all sorts of daylight saving and summer times and to show you how to use it in a project I will create a fake TC f77 transmitter with an ESP 32 let's start ntp stands for network time protocol a network of servers around the world can be connected by our ESP devices to get an accurate time unfortunately these servers have a significant disadvantage they only transmit UTC they do not offer time zone support nor do they provide support for daylight saving or summer time and sometimes they limit the number of requests so you have to limit the number of calls to we have to solve these problems plus we want to base it on standard functionality provided by the ESP 32 and the esp8266 kernel this is important because the ESP 32 comes with some advanced commands which are not supported by the esp8266 this spring g6e JD showed in two of his videos that standard unix time functions also work on our SPS because I frequently get questions about my old library and because I wanted to port my DCF 77 transmitter from my raspberry to an ESP 32 I had a reason to dig into the topic but I'm too fast what is unix time it is also called epoch time or POSIX time and it is the number of seconds that elapsed since the new year of 1970 you find many online calculators to convert local time into UNIX time and PAC if you call a standard NTP server you get this number which is usually called epoch right now it is this number and because the current format is 32-bit it will overflow and go to 0 in 2038 in my age I have to decide if this is a problem I have to care about or not according to this website I should still be around by then so I hope we will not get the next year 2000 PUC in 2038 anyway to get accurate local time we have to perform these three steps we have to get the time by the way this function uses the SMTP protocol we have to convert the epoch into a usable time and we have to adjust this time to our local time including time Corrections fortunately all this is provided by standard UNIX functionality and is coded in the time dot H library this library is already installed in the ESPE kernels so we can go on and define the NTP server for our purpose it does not matter which one we select because they all deliver precisely the same thing and travel time from the server to your device is not relevant for most of our projects I usually select a so called pool of servers because they should be more reliable here you find a list of all available servers before we call a time server we have to define the time zone in simple NTP libraries we only can choose one time zone fortunately the POSIX standard not only cares about time zones but also about anomalies like summer or daylight saving times here you should find the appropriate string for your time zone you do not have to understand how these strings work just copy the string your sketch but if you are interested we can have a closer look at the strings for Switzerland and the UK for Switzerland it is this string and for UK it is this one c ET c est GMT and PST are just names the numbers minus 1 and minus 2 mean that in winter here we are one hour behind UTC and in summer two hours the UK in winter is on UTC and in summer one hour behind UTC this part means that we change from winter to summer time in the third month which is March in the fifth week which is always the last week in the month on the zero which is Sunday at one o'clock in the night up till now the British do the same in UTC but who knows if after breaks it they will not create their own time zone you never know with the British this part means the dark part of the year starts in October also on Sunday in the last week at 3:00 in the morning nothing complicated and taken care of by the ESP kernel cool now we are ready to call the define NTP server with this function after a successful call the variable now contains the epoch of the current time as said before a 32-bit number of type time T with the second from 1970 on not very useful for most of our sketches to go further we have to introduce a structure and two functions the first function local time converts the epoch number into a string with these three lines you can print it but if we need the numbers for further calculations as in my fake TCF 77 transmitter we need to know a structure called TM for all who do not know structures these are variables like int or float but they do not only contain one value you can include as many variables as you want and the compiler takes care of that usually structures are not predefined and you first have to write your own definition in our case TM is defined in the ESP kernels in time dot H it contains at least nine integer values and the name of the structure definition is TM be aware that this is only the definition of the structure if we want to use it we have to create a variable based on this structure this is done in our sketch where we create a variable called time info but please pay attention before they define the fields the guys probably drank too much beer why do I know of course I do not know if it was only beer or something else we have to remember these definitions were done in the 1900 and 70s somewhere in California maybe by hippies where LSD was nothing special if you do not believe me you can read this book by famous Tom Wolfe a but maybe you were there and can tell us the real story but I digress I have to come back to how they defined the structure hours minutes and seconds start with zero as we would expect but month also begin with zero not as everybody would expect with January being month 1 the weekdays start ok with Monday as 1 but sunday is zero not seven and the year does not start from 1970 as the epoch no it begins with the year 1900 now you know it and it's not too complicated to adjust for it but don't forget it otherwise you search for errors like I did without ear by the way to fill this structure we can use the function local time underscore are we already know now is the epoch number and time info is the name of the structure after that command the time infrastructure contains the actual time we can see by the way if we forget to set the POSIX time string we get UTC that's it if you do not call the NTP server too often if you do not want to call the server every second you can use the function get time reduced traffic it updates now using the internal ESP clock without calling an NTP server of course this clock is not accurate and has to be adjusted from time to time then the NTP server has to be called every hour is probably ok for most of our applications let us now use this function in a real situation in video number 287 I built a fake TCF 77 transmitter using a Raspberry Pi in the mean time I found a sketch for the ESP 32 and created a cute little box ethnic uses spanish variable names and comments and he also uses commands like convict ii set time and get local time which are only available in ESP 32 so I changed the language into English used the function shown before and did some other tweaks be aware that the sketch only works on an ESP 32 because it uses the PWM functionality which is implemented differently in DSP 32 and I'm not sure if we can reach a PWM frequency of seventy-seven point five kilo Hertz on an esp8266 for the antenna you can use what was shown in video number 287 and here you can see how you can use the variables inside a structure you just use the name of the structure a point and the valuable name inside the structure simple summarized we have discovered a possibility to use standard commands available in the kernel for both ESP s to get correct local time we can write code which compiles without changes for the esp8266 as well and the ESP 32 we know how POSIX time strings work as well as we learned about the availability of a structure called TM we still do not know if beer or LSD was the reason for the weird definition of some variables in the TM structure we also got a simple function to reduce the traffic to the NTP servers and finally we used the knowledge to build a fake DC f77 transmitter using an ESP 32 I hope this video was useful or at least interesting for you if true please consider supporting the channel to secure its future existence you will find the links in the description thank you bye
Info
Channel: Andreas Spiess
Views: 78,555
Rating: undefined out of 5
Keywords: arduino, arduino project, beginners, diy, do-it-yourself, eevblog, electronics, esp32, esp32 datasheet, esp32 project, esp32 tutorial, esp32 weather station, esp8266, esp8266 datasheet, esp8266 project, greatscott, guide, hack, hobby, how to, iot, lorawan, nodemcu, project, simple, smart home, ttgo, wemos, wifi, ntmp, ntntp time, ntp summertime, ntp daylightsaving, NTP esp32, ntp esp8266, DCF77, DCF77 transmitter, dcf77 emulator
Id: r2UAmBLBBRM
Channel Id: undefined
Length: 11min 45sec (705 seconds)
Published: Sun Nov 24 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.