I want to include a function on my ESP8266 project, which can return the current UTC time in time_t format.
My idea is to call a nist server on startup after the ESP8266 has connected to a WiFi router and retrieve the current UTC time. Then it will store this as well as the return of millis().
Then my currenttime() function will return a value that is the time retrieved from the NTP server plus the difference between millis() and the stored value of millis() at the time of UTC retrieval from the NTP server.
I have googled a lot for this but all I get are complete sketches (ino files) which do a lot of printing to serial and repeatedly call the NTP server inside the loop() function. That is not what I want, instead I need something like the following functions:
bool GetUTCFromNTP(); //Retrieve the UTC time from an NTP server, save value and millis() long GetCurrentTimeUTC(); //Return current UTC based on data saved during last use of GetUTCFromNTP()
All of the needed code should reside in a regular cpp file ready to be called from any ino sketch. I want to be able to include this file (and the h file) into any project where I need the time.
In order to keep the time updated I plan on using a check in the loop() function where the GetUTCFromNTP() function will be called once per 24 hours.
Already found these pages but they are all ino-centric, noone isolates the functionality into a separate file:
Arduino Time Sync from NTP server
Arduino/NTPClient.ino at master
Arduino/NTP-TZ-DST.ino at master
Is there anywhere any function like the GetUTCFromNTP() I need?