resync NTP

Ian777
Posts: 21
Joined: Sun Jun 17, 2018 11:31 pm

resync NTP

Postby Ian777 » 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.

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);
    }
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!

Who is online

Users browsing this forum: Bing [Bot] and 86 guests