esp_netif_sntp_sync_wait returns early

Swarkin
Posts: 2
Joined: Tue Sep 02, 2025 3:31 pm

esp_netif_sntp_sync_wait returns early

Postby Swarkin » Tue Sep 02, 2025 3:36 pm

Hello,
I have been trying to get SNTP to work, however when i call esp_netif_sntp_sync_wait, it returns before the time is actually set (time() returns 0).

Using the log output i can see that the time synchronizes successfully, after the function returns:

Code: Select all

I (9830) wifi_init: Got IP: 192.168.178.67
I (9830) app: sntp_begin
sntp_init: SNTP initialised
sntp_request: Waiting for server address to be resolved.
I (9840) app: sntp_wait
I (9870) wifi:<ba-add>idx:1 (ifx:0, 3c:a6:2f:8d:6f:46), tid:0, ssn:0, winSize:64
sntp_dns_found: Server address resolved, sending request
sntp_send_request: Sending request to server
I (9890) app: Done
W (9900) app: Current time: 0
sntp_process: Tue Sep  2 15:26:12 2025
, 991467 us
sntp_recv: Scheduled next time request: 3600000 ms
W (19900) app: Current time: 0
And even then current time is reported as 0

My main task:

Code: Select all

	xEventGroupWaitBits(wifi_event_group, WIFI_HAS_IP_BIT, pdFALSE, pdFALSE, portMAX_DELAY);

	ESP_LOGI(TAG, "sntp_begin");
	ESP_ERROR_CHECK( sntp_begin() );

	ESP_LOGI(TAG, "sntp_wait");
	ESP_ERROR_CHECK( sntp_wait(pdMS_TO_TICKS(10000)) );

	ESP_LOGI(TAG, "Done");

	ESP_LOGW(TAG, "Current time: %ld", time(NULL));
	vTaskDelay(pdMS_TO_TICKS(10000));
	ESP_LOGW(TAG, "Current time: %ld", time(NULL));
My SNTP wrapper:

Code: Select all

esp_err_t sntp_begin() {
    esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org");
    return esp_netif_sntp_init(&config);
}

esp_err_t sntp_wait(TickType_t tout) {
    return esp_netif_sntp_sync_wait(tout);
}

void sntp_end() {
    esp_netif_sntp_deinit();
}

Swarkin
Posts: 2
Joined: Tue Sep 02, 2025 3:31 pm

[Solved] esp_netif_sntp_sync_wait returns early

Postby Swarkin » Tue Sep 02, 2025 6:42 pm

I have managed to fix it by using the low level esp_sntp API.

Code: Select all

static const char *TAG = "sntp_init";
static const ip_addr_t ntp_addr = IPADDR4_INIT_BYTES(192, 168, 178, 1);

SemaphoreHandle_t sntp_sem = NULL;

static void time_sync_notification_cb(struct timeval *tv) {
	ESP_LOGI(TAG, "Time synchronized via SNTP");
	ESP_LOGI(TAG, "tv->tv_sec: %ld, tv->tv_usec: %ld", tv->tv_sec, tv->tv_usec);

	if (sntp_sem != NULL) xSemaphoreGive(sntp_sem);
}

void sntp_begin() {
	if (sntp_sem == NULL) {
		sntp_sem = xSemaphoreCreateBinary();
		if (sntp_sem == NULL) {
			ESP_LOGE(TAG, "Failed to create semaphore");
			return;
		}
	}

	setenv("TZ", "Europe/Berlin", 1);
	tzset();

	esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
	esp_sntp_setserver(0, &ntp_addr);
	esp_sntp_set_time_sync_notification_cb(time_sync_notification_cb);
	esp_sntp_init();
}

void sntp_end() {
	esp_sntp_stop();

	if (sntp_sem != NULL) {
		vSemaphoreDelete(sntp_sem);
		sntp_sem = NULL;
	}
}

esp_err_t sntp_wait(TickType_t timeout) {
	if (sntp_sem == NULL) return ESP_ERR_INVALID_STATE;

	if (xSemaphoreTake(sntp_sem, timeout) == pdTRUE) {
		ESP_LOGI(TAG, "SNTP synchronization completed");
		return ESP_OK;
	} else {
		ESP_LOGW(TAG, "SNTP synchronization timeout");
		return ESP_ERR_TIMEOUT;
	}
}


Who is online

Users browsing this forum: Amazon [Bot], Craig_2026 and 1 guest