I am developing an application to start a motor two times a day for filling pellets to my pellet bunker.
The internet is sometimes flaky and might be off for a few days.
Due to remote location there are regular power outings.
That is the reason that I have a Real Time Clock as central time keeper.
Hence the procedure is:
- Obtain time from NTP server once in 24 hours.
- Synchronize the RTC (DS3231) with NTP time when internet works.
- Synchronize the ESP8266 with the RTC
I have no issues except one:
Despite scouring the internet for hours I have not found a way to update the RTC with Unix time.
I use RTClib.h and the obvious NTP code.
The NTP code gives me a time_t object as Unix time (starting seconds from 1970) say
time_t unixTime
One way seems to be to breakdown 'time_t unixTime' to year - month - etc and to synchronize the RTC with the following format asin RTClib:
rtc.adjust(DateTime(YYYY,mm,dd,hh,mm,ss)
However in the technical docs of RTClib (RTClib: DateTime Class Reference) there is a DateTime constructor:
DateTime::DateTime (uint32_t t = SECONDS_FROM_1970_TO_2000)
(btw the notation is above my level of knowledge)
Would this mean that I could use:
rtc.adjust(DateTime(unixTime))
In a test sketch it seems to work however I am not sure because of other use of EEPROM in the sketch.
I am not copying the complete code now it is quite long and mainly boilerplate hence not adding something to the question I think.
Hope someone can help me. Much appreciated.