SNTP server priority

ethanbowering24
Posts: 4
Joined: Sat May 25, 2024 9:00 pm

SNTP server priority

Postby ethanbowering24 » Fri May 30, 2025 3:38 pm

Code: Select all

esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG_MULTIPLE(2,
                                        ESP_SNTP_SERVER_LIST(CONFIG_SNTP_TIME_SERVER, CONFIG_BACKUP_SNTP_TIME_SERVER));
When using esp-netif for SNTP, will the first server in the list always take priority? What would cause the second server to be used? How could I know if the backup server was used so I can log a warning?

ahsrabrifat
Posts: 201
Joined: Sat Jan 18, 2025 2:31 pm

Re: SNTP server priority

Postby ahsrabrifat » Sat May 31, 2025 11:30 am

Unfortunately, esp-netif SNTP does not provide a built-in log or callback explicitly indicating which server responded. Use sntp_getservername() After Sync. After synchronization, you can query which server was used:

Code: Select all

const char *used_server = sntp_getservername(0);  // 0 refers to index of the server used
ESP_LOGI(TAG, "SNTP used server: %s", used_server);
This only works if you are using DNS names, not IP addresses set via sntp_setserver().
So, you can compare this string against CONFIG_SNTP_TIME_SERVER and log a warning if it's not equal:

Code: Select all

if (strcmp(used_server, CONFIG_SNTP_TIME_SERVER) != 0) {
    ESP_LOGW(TAG, "Primary SNTP server failed; backup server used: %s", used_server);
}

Who is online

Users browsing this forum: Applebot, Qwantbot and 3 guests