Sorry if this is a noob question, but I've exhausted my research via http://www.cplusplus.com/reference/ctime/tm/ tutorials etc.
I'm using the Arduino SimpleTime example for the ESP32, but would like to be able to set the time without internet access.
I was using TimeLib.h for a past project which had a time setting functionality using a setTime(Hour, Minute, Second, MDay, Mon, Year) function, but that library and clock implementation seems to be susceptible to time advances as per Espressif issue #120: https://github.com/espressif/arduino-esp32/issues/120
I've tested the SimpleTime sketch and haven't found any time advance drift due to ADC reads, so would like to be able to set up the ESP32 from bootup also in circumstances without internet access, and still be able to run with a time so I can use time(NULL), mktime(tm_struct) etc.
Is there a different implementation of configTime(gmtOffset_sec, daylightOffset_sec, ntpServer) which might work?
Thanks in advance :)
I had exactly the same issue and found a way. I wanted to store the time in the EEPROM and only sync time via ntp rarely. So in case of a power loss, the time will be loaded from the EEPROM.
That's how I set time
tm local;
if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_UNDEFINED) { // if reset or power loss
  struct timeval val;
  loadStruct(&local); // e.g. load time from eeprom
  const time_t sec = mktime(&local); // make time_t
  localtime(&sec); //set time
} else {
  getLocalTime(&local);
}
That's it! Your time is set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With