The call to esp_sntp_setservername does not seem to work.

User avatar
MickPF
Posts: 62
Joined: Tue Apr 03, 2018 8:47 pm

The call to esp_sntp_setservername does not seem to work.

Postby MickPF » Thu Jan 01, 2026 4:14 pm

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

nopnop2002
Posts: 347
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

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

Postby nopnop2002 » Fri Jan 02, 2026 8:31 am

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]);

User avatar
MickPF
Posts: 62
Joined: Tue Apr 03, 2018 8:47 pm

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

Postby MickPF » Fri Jan 02, 2026 10:38 am

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!

User avatar
MickPF
Posts: 62
Joined: Tue Apr 03, 2018 8:47 pm

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

Postby MickPF » Fri Jan 02, 2026 10:40 am

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!

nopnop2002
Posts: 347
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

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

Postby nopnop2002 » Fri Jan 02, 2026 11:34 am

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);

User avatar
MickPF
Posts: 62
Joined: Tue Apr 03, 2018 8:47 pm

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

Postby MickPF » Fri Jan 02, 2026 1:06 pm

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!

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

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

Postby MicroController » Fri Jan 02, 2026 1:16 pm

(re: viewtopic.php?t=47410#p152847 )

There's nothing to play with :)

Wear-levelling implies sector:=4096

But documentation of this could be improved.

User avatar
MickPF
Posts: 62
Joined: Tue Apr 03, 2018 8:47 pm

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

Postby MickPF » Fri Jan 02, 2026 1:23 pm

!!! 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 !!!

User avatar
MickPF
Posts: 62
Joined: Tue Apr 03, 2018 8:47 pm

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

Postby MickPF » Fri Jan 02, 2026 1:34 pm

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!

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

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

Postby MicroController » Fri Jan 02, 2026 1:54 pm

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.

Who is online

Users browsing this forum: No registered users and 18 guests