Page 1 of 2

The call to esp_sntp_setservername does not seem to work.

Posted: Thu Jan 01, 2026 4:14 pm
by MickPF
Hello,

I wrote the following code (snippet):

Code: Select all

            /* Initialize and start SNTP client */
            if (cfg->client.sntp.server_no >= 1)
            {
                esp_sntp_setoperatingmode(ESP_SNTP_OPMODE_POLL);

                esp_sntp_config_t sntp_config = ESP_NETIF_SNTP_DEFAULT_CONFIG(cfg->client.sntp.server[0]);
                sntp_config.sync_cb = time_sync_event_handler;
                sntp_config.start = false;

                esp_netif_sntp_init(&sntp_config);

                sntp_set_sync_interval(cfg->client.sntp.interval * 1000);
                sntp_set_sync_status(SNTP_SYNC_STATUS_RESET);
                for (i = 1; i < cfg->client.sntp.server_no; i++)
                    esp_sntp_setservername(i, cfg->client.sntp.server[i]);

                esp_netif_sntp_start();
            }
            for (i = 0; i < cfg->client.sntp.server_no; i++)
            {
                char const   *name;

                name = esp_sntp_getservername(i);
                ESP_LOGI(MODULE_TAG, "NTP server no. %d : %s", i, (name == NULL) ? "<NULL>" : name);
            }

My server array contains two entries: "pool.ntp.org" and "time.microsoft.com" and the field cfg->client.sntp.server_no = 2.
Executing this code outputs:

Code: Select all

I (3556) WIFI: NTP server no. 0 : pool.ntp.org
I (3566) WIFI: NTP server no. 1 : <NULL>

Is something wrong in my code? Is the order of esp_??? calls wrong?

Thanks in advance,
Michael

Re: The call to esp_sntp_setservername does not seem to work.

Posted: Fri Jan 02, 2026 8:31 am
by nopnop2002

Code: Select all

                //for (i = 1; i < cfg->client.sntp.server_no; i++)
                for (i = 0; i < cfg->client.sntp.server_no; i++)
                    esp_sntp_setservername(i, cfg->client.sntp.server[i]);

Re: The call to esp_sntp_setservername does not seem to work.

Posted: Fri Jan 02, 2026 10:38 am
by MickPF
Hello,

First, I would like to point out that I use the following command to specify the first server:

Code: Select all

                esp_sntp_config_t sntp_config = ESP_NETIF_SNTP_DEFAULT_CONFIG(cfg->client.sntp.server[0]);
I tried your suggestion, and it leads to the same result:

Code: Select all

I (3387) WIFI: NTP server no. 0 : pool.ntp.org
I (3387) WIFI: NTP server no. 1 : <NULL>
I also tried using the macro
ESP_NETIF_SNTP_DEFAULT_CONFIG_MULTIPLE
, but it didn't work either!

Re: The call to esp_sntp_setservername does not seem to work.

Posted: Fri Jan 02, 2026 10:40 am
by MickPF
Hello,

First, I would like to point out that I use the following command to specify the first server:

Code: Select all

                esp_sntp_config_t sntp_config = ESP_NETIF_SNTP_DEFAULT_CONFIG(cfg->client.sntp.server[0]);
I tried your suggestion, and it leads to the same result:

Code: Select all

I (3387) WIFI: NTP server no. 0 : pool.ntp.org
I (3387) WIFI: NTP server no. 1 : <NULL>
I also tried using the macro
ESP_NETIF_SNTP_DEFAULT_CONFIG_MULTIPLE
, but it didn't work either!

Re: The call to esp_sntp_setservername does not seem to work.

Posted: Fri Jan 02, 2026 11:34 am
by nopnop2002

Code: Select all

                //esp_sntp_config_t sntp_config = ESP_NETIF_SNTP_DEFAULT_CONFIG(cfg->client.sntp.server[0]);
                esp_sntp_config_t sntp_config = ESP_NETIF_SNTP_DEFAULT_CONFIG_MULTIPLE(2,
                               ESP_SNTP_SERVER_LIST("ntp.nict.jp", "pool.ntp.org" ) );
                sntp_config.sync_cb = time_sync_event_handler;

                esp_netif_sntp_init(&sntp_config);

Re: The call to esp_sntp_setservername does not seem to work.

Posted: Fri Jan 02, 2026 1:06 pm
by MickPF
I'd like to end this discussion!

As I said, I'm perfectly happy with a sector size of 4096 bytes, and I have neither the time nor the inclination to play with all sorts of settings and code!

Re: The call to esp_sntp_setservername does not seem to work.

Posted: Fri Jan 02, 2026 1:16 pm
by MicroController
(re: viewtopic.php?t=47410#p152847 )

There's nothing to play with :)

Wear-levelling implies sector:=4096

But documentation of this could be improved.

Re: The call to esp_sntp_setservername does not seem to work.

Posted: Fri Jan 02, 2026 1:23 pm
by MickPF
!!! Sorry !!!

My last post belongs to an other discussion (about the sector size of FAT partitions)!!
I used the link in the email to reply to a post, but it led me to the wrong discussion!!

!!! Sorry !!!

Re: The call to esp_sntp_setservername does not seem to work.

Posted: Fri Jan 02, 2026 1:34 pm
by MickPF
Unfortunately, I can't use the macro ESP_NETIF_SNTP_DEFAULT_CONFIG_MULTIPLE because my server list isn't fixed. It can contain between one and three servers.

It's no use to me to pass this macro two fixed servers just to test whether it works in general or not. In real-world operation, this list is variable!

Re: The call to esp_sntp_setservername does not seem to work.

Posted: Fri Jan 02, 2026 1:54 pm
by MicroController
You have to provide the (maximum) number of servers you want to use to esp_netif_sntp_init().
You can try

Code: Select all

   esp_sntp_config_t sntp_config = ESP_NETIF_SNTP_DEFAULT_CONFIG_MULTIPLE(
                               3,
                               ESP_SNTP_SERVER_LIST( NULL, NULL, NULL ) );
for example to ensure there's space allocated for up to 3 servers.

- Or, don't initialize SNTP at all until you know at least one server's name, or how many servers you actually want to use.