(resolved) SNTP sync erratic

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

(resolved) SNTP sync erratic

Postby mzimmers » Mon Nov 02, 2020 2:13 am

Hi all -

I'm trying to use the SNTP sync facility as described in the docs, and demonstrated in the example. The results are all over the map - sometimes it syncs really quickly; sometimes it takes a while, and sometimes it never does. I'm wondering what I'm doing to cause this.

v4.1 of the IDF; running on WROVER.

Here's my initialization code (called once):

Code: Select all

    setTimeZone(m_tasks->flash->get(NVS_KEY_TIMEZONE));
    sntp_setoperatingmode(SNTP_OPMODE_POLL);
    sntp_setservername(0, m_tasks->flash->get(NVS_KEY_NTP_SERVER).c_str());
    sntp_init();
And periodically I check to see if time is synced; if not, I call this routine:

Code: Select all

esp_err_t Wifi::sntpSync()
{
    const int MAX_TRIES = 10;

    esp_err_t err;
    int retry = 0;
    time_t now = 0;

    while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < MAX_TRIES)
    {
        ESP_LOGI(TAG, "sntpSync(): attempting to sync SNTP (%d/%d).", retry, MAX_TRIES);
        vTaskDelay(2000 / portTICK_PERIOD_MS);
    }
    if (retry < MAX_TRIES)
    {
        ESP_LOGI(TAG, "sntpSync(): time is synced.");
        time(&now);
        err = ESP_OK;
    }
    else
    {
        ESP_LOGE(TAG, "sntpSync(): failed to sync time.");
        err = ESP_FAIL;
        sntp_restart();
    }

    return err;
}
Can someone see what I might be doing wrong?

Thanks...
Last edited by mzimmers on Thu Nov 05, 2020 3:14 pm, edited 1 time in total.

willemmerson
Posts: 40
Joined: Mon Mar 18, 2019 12:34 pm

Re: SNTP sync erratic

Postby willemmerson » Thu Nov 05, 2020 9:02 am

It works fine for me, using v4.2 and mostly just like the example in esp-idf. I have noticed a few times it has taken longer than usual, but I put this down to the time server doing some kind of throttling. If you keep it to something sensible like once an hour then it works fine.
The only other thing I do is make sure I have an ip address before doing it, otherwise it's definitely going to do wierd things.

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: SNTP sync erratic

Postby mzimmers » Thu Nov 05, 2020 3:13 pm

Thanks for the reply. I modified my code to check for a viable year:

Code: Select all

bool Wifi::sntpIsSynced()
{
    time_t now;
    tm *timeinfo;
    bool rc;

    time(&now);
    timeinfo = localtime(&now);

    if (timeinfo->tm_year < (2016 - 1900))
    {
        rc = false;
    }
    else
    {
        rc = true;
    }
    return rc;
}
And in my event loop, if this returns true, I don't call my sync routine. This seems to work considerably better, so perhaps you're right about the time server objecting to excessive requests.

willemmerson
Posts: 40
Joined: Mon Mar 18, 2019 12:34 pm

Re: (resolved) SNTP sync erratic

Postby willemmerson » Fri Nov 06, 2020 10:28 am

You seem to be making this more complicated than it needs to be, unless I'm misunderstanding something. When you initialise sntp it then gets the time every hour from that point onwards, you don't need to do anything. If you need to you can define a custom callback and get some visibility into what's going on but it's unnecessary.
Does it really matter if it's off by a few minutes? The esp RTC keeps reasonable time once set, even just syncing once a day would be sufficient in most cases.

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: (resolved) SNTP sync erratic

Postby mzimmers » Fri Nov 06, 2020 3:30 pm

It's as simple as I can imagine. My event loop performs a quick check to see if the time is set (based on whether tm->year is seemingly valid), and notifies the user if it isn't. That's pretty much it.

I guess I could eliminate the check, but then the user wouldn't have any way of knowing that he should probably reset the unit, which could lead to bogus timestamps in log messages, etc.

I welcome any ideas on simplifying this further.

willemmerson
Posts: 40
Joined: Mon Mar 18, 2019 12:34 pm

Re: (resolved) SNTP sync erratic

Postby willemmerson » Fri Nov 06, 2020 3:43 pm

I didn't realise you were making a user facing app, in that case it probably makes sense. When I first used sntp I didn't realise it was syncing automatically every hour, I just thought you might have made the same mistake.

Who is online

Users browsing this forum: andy_wharton_uk, Google [Bot], ok-home and 74 guests