Page 1 of 1

SNTP returns same value each time

Posted: Fri Jun 10, 2022 2:17 pm
by alqine
Hi all,

I have SNTP client up and running on my ESP32C3, but it returns the same value each time time is synced.

Code: Select all


// Callback for time-sync event
void time_sync_cb(struct timeval *tv) {
    printf("Time sync finished: %lld\n", tv->tv_sec);
}

void sntp_start() {
    printf("SNTP starting\n");

    // Setup TimeZone. Different values - UTC+1, EST5EDT,M3.2.0/2,M11.1.0 etc don't have any effect.
    setenv("TZ", "UTC", 1);
    tzset();

    sntp_setservername(0, "pool.ntp.org");
    sntp_set_sync_interval( 15*1000 ); // 15 s
    sntp_set_time_sync_notification_cb(time_sync_cb);
    sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED); 
    sntp_init();
}
Function time_sync_cb prints:

Code: Select all

Time sync finished: 77309542412
When I try reading current timestamp with time() function later on, it returns value 62500.

The code should be pretty straightforward, after few hours of changing pools, intervals, timezones and adding more code to it, I don't see the error in the code. May it be in hardware? There are no error messages in idf monitor. And the device is connected to the WiFi.

Re: SNTP returns same value each time

Posted: Fri Jun 10, 2022 8:36 pm
by gtjoseph
After you've called sntp_init(), call sntp_get_sync_status() in a loop...

Code: Select all

	int retry = 0;
	const int retry_count = 10;
	while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
		ESP_LOGI("SNTP", "Waiting for system time to be set... (%d/%d)", retry, retry_count);
		delay(2000);
	}
Does the status ever go from RESET to COMPLETED?

Re: SNTP returns same value each time

Posted: Sun Jun 12, 2022 4:03 pm
by alqine
Hi gtjoseph,

yes, it does leave the loop and after that, the user provided callback is called. I suspect, there is some error when parsing received values. For some reason lwip_ntohl is macro for lwip_htonl. Should not that be individual function transforming from big-endian (received from NTP server) into local endianess, which should be little-endian?

Another thing is, that SNTP_SET_SYSTEM_TIME_US macro adds value DIFF_SEC_1970_2036 (2085978496L) to seconds value. Shouldn't that be rather subtracted and the value should be something like DIFF_SEC_1900_1970 (nonexistent constant I made up), since NTP time is 1900 based and unix timestamp starts in 1970?

Re: SNTP returns same value each time

Posted: Sun Jun 12, 2022 11:40 pm
by Craige Hales
Every 15 seconds is too often. Try something like the default (1 hour) or

Code: Select all

sntp_set_sync_interval(2*3600*1000); // 2 hours
(see also: https://www.pool.ntp.org/tos.html)

My experience was it takes several seconds to sync. Maybe more than 15.
I remember an sntp sample as well; it was a good starting point.

The esp-idf-v4.3.1 sntp code worked great for me.

Re: SNTP returns same value each time

Posted: Sun Jun 12, 2022 11:58 pm
by Craige Hales
...and the tv parameter was not useful to me. I didn't try to figure it out because the system time is already updated.

Code: Select all

void time_sync_notification_cb(struct timeval *tv)
{
    time_t now;
    struct tm timeinfo;
    time(&now);
    localtime_r(&now, &timeinfo);
    char strftime_buf[64];
    strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
    ESP_LOGI(TAG, "Notification of a time synchronization event %s on core %d", strftime_buf, xPortGetCoreID());
}

Code: Select all

I (7495) clocksntp: Initializing SNTP on core 0
sntp_get_sync_interval 3600000
sntp_get_sync_interval 7200000
I (7595) clocksntp: Waiting for system time to be set... (1/10) on core 0
I (7695) clocksntp: Waiting for system time to be set... (2/10) on core 0
I (7795) clocksntp: Waiting for system time to be set... (3/10) on core 0
I (7895) clocksntp: Waiting for system time to be set... (4/10) on core 0
I (7995) clocksntp: Waiting for system time to be set... (5/10) on core 0
I (8095) clocksntp: Waiting for system time to be set... (6/10) on core 0
I (8195) clocksntp: Waiting for system time to be set... (7/10) on core 0
I (8295) clocksntp: Waiting for system time to be set... (8/10) on core 0
I (8395) clocksntp: Waiting for system time to be set... (9/10) on core 0
... << about 4 seconds >> ...
I (12065) clocksntp: Notification of a time synchronization event Sun Jun 12 19:53:20 2022 on core 0