Reputation: 41
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 :)
Upvotes: 4
Views: 19824
Reputation: 91
I use an inexpensive DS3231 breakout board that has a builtin battery backup. It connects via I2C and you access it via the Real-Time Clock Library.
Upvotes: 3
Reputation: 418
Check out this library, ESP32Time
Time can be set as follows;
setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 15:24:30
setTime(1609459200); //specify the epoch, 1st Jan 2021 00:00:00
setTime(); // default 1st Jan 2021 00:00:00
Upvotes: 3
Reputation: 1267
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.
Upvotes: 4