resync NTP
Posted: Wed Jun 12, 2019 7:09 pm
Hey everyone,
In my code upon wake up I want to have the esp retrieve time using sntp. The sync works perfectly the first time as it updates from 1970 to 2019, althought I have no idea what to make the condition for subsequent sync's to allow it to resync.
Here you can see that it checks the year to discern whether it has been synced. although with future syncs the year might be fine, but the minutes could have drifted, Is there anyway to handle resyncing? I'm using the stable branch of the esp-idf.
Thanks in advance for any help!
In my code upon wake up I want to have the esp retrieve time using sntp. The sync works perfectly the first time as it updates from 1970 to 2019, althought I have no idea what to make the condition for subsequent sync's to allow it to resync.
Code: Select all
time_t now = 0;
struct tm timeinfo = { 0 };
int retry = 0;
const int retry_count = 10;
while(timeinfo.tm_year < (2019 - 1900) && ++retry < retry_count) {
ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
vTaskDelay(2000 / portTICK_PERIOD_MS);
time(&now);
localtime_r(&now, &timeinfo);
}
Thanks in advance for any help!