DHCP SNTP not sending packets, Not Working

savages
Posts: 6
Joined: Thu Sep 21, 2023 5:44 pm

DHCP SNTP not sending packets, Not Working

Postby savages » Sun Aug 03, 2025 4:57 pm

I am trying to use https_request to set time from dhcp.
I commented out code to make SNTP request to sync time every time the app starts.
I included the DHCP ACK to show that the ESP32 is receiving SNTP info, YES
I am using wireshark to see if the NTP packets are be sent to the server, NO they are not being sent
the SNTP request is send after the wifi connect and has an IP addesss.

Added info: CODE, DHCP packet, LOG

Code

void time_sync_notification_cb(struct timeval *tv)
{
ESP_LOGI(TAG, "Notification of a time synchronization event");
}

void initialize_sntp(void)
{
ESP_LOGI(TAG, "Initializing SNTP");
esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG_MULTIPLE(0, {} );
config.start = false; // start the SNTP service explicitly
config.server_from_dhcp = true; // accept the NTP offer from the DHCP server
// esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG_MULTIPLE(2,
// ESP_SNTP_SERVER_LIST("time.windows.com", "pool.ntp.org" ) );
config.renew_servers_after_new_IP = true;
config.index_of_first_server = 1;
config.ip_event_to_renew = IP_EVENT_STA_GOT_IP;
config.sync_cb = time_sync_notification_cb;

esp_netif_sntp_init(&config);
}

static esp_err_t obtain_time(void)
{
// wait for time to be set
int retry = 0;
const int retry_count = 10;
esp_netif_sntp_start();
while (esp_netif_sntp_sync_wait(pdMS_TO_TICKS(2000)) != ESP_OK && ++retry < retry_count) {
ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
}
if (retry == retry_count) {
return ESP_FAIL;
}
return ESP_OK;
}

esp_err_t fetch_and_store_time_in_nvs(void *args)
{
nvs_handle_t my_handle = 0;
esp_err_t err;

initialize_sntp();
if (obtain_time() != ESP_OK) {
err = ESP_FAIL;
goto exit;
}

time_t now;
time(&now);

//Open
err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
if (err != ESP_OK) {
goto exit;
}

//Write
err = nvs_set_i64(my_handle, "timestamp", now);
if (err != ESP_OK) {
goto exit;
}

err = nvs_commit(my_handle);
if (err != ESP_OK) {
goto exit;
}

exit:
if (my_handle != 0) {
nvs_close(my_handle);
}
esp_netif_sntp_deinit();

if (err != ESP_OK) {
ESP_LOGE(TAG, "Error updating time in nvs");
} else {
ESP_LOGI(TAG, "Updated time in NVS");
}
return err;
}

esp_err_t update_time_from_nvs(void)
{
nvs_handle_t my_handle = 0;
esp_err_t err;

err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Error opening NVS");
goto exit;
}

int64_t timestamp = 0;

//err = nvs_get_i64(my_handle, "timestamp", &timestamp);
//f (err == ESP_ERR_NVS_NOT_FOUND) {
ESP_LOGI(TAG, "Syncing time from SNTP server.");
if (fetch_and_store_time_in_nvs(NULL) != ESP_OK) {
err = ESP_FAIL;
} else {
err = ESP_OK;
}
//} else if (err == ESP_OK) {
// struct timeval get_nvs_time;
// get_nvs_time.tv_sec = timestamp;
// settimeofday(&get_nvs_time, NULL);
//}

exit:
if (my_handle != 0) {
nvs_close(my_handle);
}
return err;
}


DHCP ACK
Frame 399: 342 bytes on wire (2736 bits), 342 bytes captured (2736 bits) on interface wlan0, id 0
Ethernet II, Src: Raspberr_a3:c7:39 (d8:3a:dd:a3:c7:39), Dst: Espressi_04:24:b0 (d4:f9:8d:04:24:b0)
Internet Protocol Version 4, Src: 10.42.0.1, Dst: 10.42.0.252
User Datagram Protocol, Src Port: 67, Dst Port: 68
Dynamic Host Configuration Protocol (ACK)
Message type: Boot Reply (2)
Hardware type: Ethernet (0x01)
Hardware address length: 6
Hops: 0
Transaction ID: 0x4d6822d5
Seconds elapsed: 0
Bootp flags: 0x0000 (Unicast)
Client IP address: 0.0.0.0
Your (client) IP address: 10.42.0.252
Next server IP address: 10.42.0.1
Relay agent IP address: 0.0.0.0
Client MAC address: Espressi_04:24:b0 (d4:f9:8d:04:24:b0)
Client hardware address padding: 00000000000000000000
Server host name not given
Boot file name not given
Magic cookie: DHCP
Option: (53) DHCP Message Type (ACK)
Length: 1
DHCP: ACK (5)
Option: (54) DHCP Server Identifier (10.42.0.1)
Length: 4
DHCP Server Identifier: 10.42.0.1
Option: (51) IP Address Lease Time
Length: 4
IP Address Lease Time: (3600s) 1 hour
Option: (58) Renewal Time Value
Length: 4
Renewal Time Value: (1800s) 30 minutes
Option: (59) Rebinding Time Value
Length: 4
Rebinding Time Value: (3150s) 52 minutes, 30 seconds
Option: (1) Subnet Mask (255.255.255.0)
Length: 4
Subnet Mask: 255.255.255.0
Option: (28) Broadcast Address (10.42.0.255)
Length: 4
Broadcast Address: 10.42.0.255
Option: (3) Router
Length: 4
Router: 10.42.0.1
Option: (6) Domain Name Server
Length: 4
Domain Name Server: 10.42.0.1
Option: (42) Network Time Protocol Servers
Length: 4
Network Time Protocol Server: 10.42.0.1
Option: (255) End
Option End: 255
Padding: 0000

LOG
svvs@raspberrypi:~/esp/hydroesp $ idf.py -p /dev/ttyACM0 monitor
Executing action: monitor
Running idf_monitor in directory /home/svvs/esp/hydroesp
Executing "/home/svvs/.espressif/python_env/idf5.5_py3.11_env/bin/python /home/svvs/esp/esp-idf/tools/idf_monitor.py -p /dev/ttyACM0 -b 115200 --toolchain-prefix riscv32-esp-elf- --target esp32c3 --revision 3 --decode-panic backtrace /home/svvs/esp/hydroesp/build/hydroesp.elf /home/svvs/esp/hydroesp/build/bootloader/bootloader.elf -m '/home/svvs/.espressif/python_env/idf5.5_py3.11_env/bin/python' '/home/svvs/esp/esp-idf/tools/idf.py' '-p' '/dev/ttyACM0'"...
--- esp-idf-monitor 1.7.0 on /dev/ttyACM0 115200
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
I (195) esp_image: segment 4: paddr=00106444 vaddr=40388108 sizeESP-ROM:esp32c3-api1-20210207
Build:Feb 7 2021
rst:0x15 (USB_UART_CHIP_RESET),boot:0xc (SPI_FAST_FLASH_BOOT)
Saved PC:0x40384434
--- 0x40384434: rv_utils_wait_for_intr at /home/svvs/esp/esp-idf/components/riscv/include/riscv/rv_utils.h:79
--- (inlined by) esp_cpu_wait_for_intr at /home/svvs/esp/esp-idf/components/esp_hw_support/cpu.c:62
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd5830,len:0x15c0
load:0x403cbf10,len:0xba4
load:0x403ce710,len:0x3040
--- 0x403cbf10: esp_bootloader_get_description at /home/svvs/esp/esp-idf/components/esp_bootloader_format/esp_bootloader_desc.c:39
--- 0x403ce710: esp_flash_encryption_enabled at /home/svvs/esp/esp-idf/components/bootloader_support/src/flash_encrypt.c:89
--- 0x403cbf1a: call_start_cpu0 at /home/svvs/esp/esp-idf/components/bootloader/subproject/main/bootloader_start.c:25
entry 0x403cbf1a
I (24) boot: ESP-IDF v5.5 2nd stage bootloader
I (24) boot: compile time Jul 26 2025 17:34:05
I (25) boot: chip revision: v0.4
I (25) boot: efuse block revision: v1.3
I (25) boot.esp32c3: SPI Speed : 80MHz
I (26) boot.esp32c3: SPI Mode : DIO
I (26) boot.esp32c3: SPI Flash Size : 4MB
I (26) boot: Enabling RNG early entropy source...
I (27) boot: Partition Table:
I (27) boot: ## Label Usage Type ST Offset Length
I (28) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (28) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (29) boot: 2 factory factory app 00 00 00010000 00200000
I (29) boot: End of partition table
I (30) esp_image: segment 0: paddr=00010020 vaddr=3c0d0020 size=25024h (151588) map
I (55) esp_image: segment 1: paddr=0003504c vaddr=3fc94800 size=02ebch ( 11964) load
I (58) esp_image: segment 2: paddr=00037f10 vaddr=40380000 size=08108h ( 33032) load
I (65) esp_image: segment 3: paddr=00040020 vaddr=42000020 size=c641ch (812060) map
I (195) esp_image: segment 4: paddr=00106444 vaddr=40388108 size=0c524h ( 50468) load
I (205) esp_image: segment 5: paddr=00112970 vaddr=50000000 size=00020h ( 32) load
I (212) boot: Loaded app from partition at offset 0x10000
I (212) boot: Disabling RNG early entropy source...
I (213) cpu_start: Unicore app
I (221) cpu_start: Pro cpu start user code
I (222) cpu_start: cpu freq: 160000000 Hz
I (222) app_init: Application information:
I (222) app_init: Project name: hydroesp
I (222) app_init: App version: 542a0a5-dirty
I (222) app_init: Compile time: Aug 3 2025 09:49:02
I (223) app_init: ELF file SHA256: fd6ab7805...
I (223) app_init: ESP-IDF: v5.5
I (223) efuse_init: Min chip rev: v0.3
I (223) efuse_init: Max chip rev: v1.99
I (224) efuse_init: Chip rev: v0.4
I (224) heap_init: Initializing. RAM available for dynamic allocation:
I (225) heap_init: At 3FC9C3F0 len 00023C10 (143 KiB): RAM
I (225) heap_init: At 3FCC0000 len 0001C710 (113 KiB): Retention RAM
I (226) heap_init: At 3FCDC710 len 00002950 (10 KiB): Retention RAM
I (226) heap_init: At 50000020 len 00001FC8 (7 KiB): RTCRAM
I (227) spi_flash: detected chip: generic
I (227) spi_flash: flash io: dio
I (228) sleep_gpio: Configure to isolate all GPIO pins in sleep state
I (229) sleep_gpio: Enable automatic switching of GPIO sleep configuration
I (229) main_task: Started on CPU0
I (229) main_task: Calling app_main()
I (229) example_connect: Start example_connect.
I (239) pp: pp rom version: 9387209
I (239) net80211: net80211 rom version: 9387209
I (249) wifi:wifi driver task: 3fca4b70, prio:23, stack:6656, core=0
I (249) wifi:wifi firmware version: f3dbad7
I (249) wifi:wifi certification version: v7.0
I (249) wifi:config NVS flash: enabled
I (249) wifi:config nano formatting: disabled
I (249) wifi:Init data frame dynamic rx buffer num: 32
I (249) wifi:Init static rx mgmt buffer num: 5
I (249) wifi:Init management short buffer num: 32
I (259) wifi:Init dynamic tx buffer num: 32
I (259) wifi:Init static tx FG buffer num: 2
I (259) wifi:Init static rx buffer size: 1600
I (259) wifi:Init static rx buffer num: 10
I (259) wifi:Init dynamic rx buffer num: 32
I (259) wifi_init: rx ba win: 6
I (259) wifi_init: accept mbox: 6
I (259) wifi_init: tcpip mbox: 32
I (259) wifi_init: udp mbox: 6
I (259) wifi_init: tcp mbox: 6
I (259) wifi_init: tcp tx win: 5760
I (259) wifi_init: tcp rx win: 5760
I (259) wifi_init: tcp mss: 1440
I (259) wifi_init: WiFi IRAM OP enabled
I (259) wifi_init: WiFi RX IRAM OP enabled
I (259) phy_init: phy_version 1201,bae5dd99,Mar 3 2025,15:36:21
I (299) wifi:mode : sta (d4:f9:8d:04:24:b0)
I (299) wifi:enable tsf
I (299) example_connect: Connecting to test3...
W (299) wifi:Password length matches WPA2 standards, authmode threshold changes from OPEN to WPA2
I (299) example_connect: Waiting for IP(s)
I (2709) wifi:new:<11,0>, old:<1,0>, ap:<255,255>, sta:<11,0>, prof:1, snd_ch_cfg:0x0
I (2709) wifi:state: init -> auth (0xb0)
I (2719) wifi:state: auth -> assoc (0x0)
I (2719) wifi:state: assoc -> run (0x10)
I (2719) wifi:state: run -> init (0x2c0)
I (2719) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1, snd_ch_cfg:0x0
I (2719) example_connect: Wi-Fi disconnected 2, trying to reconnect...
W (3019) wifi:tx null, bss is null
I (5139) example_connect: Wi-Fi disconnected 205, trying to reconnect...
I (7549) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1, snd_ch_cfg:0x0
I (7549) wifi:state: init -> auth (0xb0)
I (7549) wifi:state: auth -> assoc (0x0)
I (7559) wifi:state: assoc -> run (0x10)
I (7569) wifi:connected with test3, aid = 1, channel 11, BW20, bssid = d8:3a:dd:a3:c7:39
I (7569) wifi:security: WPA2-PSK, phy: bgn, rssi: -34
I (7569) wifi:pm start, type: 1

I (7579) wifi:dp: 1, bi: 102400, li: 3, scale listen interval from 307200 us to 307200 us
I (7579) wifi:set rx beacon pti, rx_bcn_pti: 0, bcn_timeout: 25000, mt_pti: 0, mt_time: 10000
I (7579) wifi:<ba-add>idx:0 (ifx:0, d8:3a:dd:a3:c7:39), tid:7, ssn:0, winSize:64
I (7669) wifi:dp: 2, bi: 102400, li: 4, scale listen interval from 307200 us to 409600 us
I (7679) wifi:AP's beacon interval = 102400 us, DTIM period = 2
I (8589) esp_netif_handlers: example_netif_sta ip: 10.42.0.252, mask: 255.255.255.0, gw: 10.42.0.1
I (8589) example_connect: Got IPv4 event: Interface "example_netif_sta" address: 10.42.0.252
I (9229) example_connect: Got IPv6 event: Interface "example_netif_sta" address: fe80:0000:0000:0000:d6f9:8dff:fe04:24b0, type: ESP_IP6_ADDR_IS_LINK_LOCAL
I (9229) example_common: Connected to example_netif_sta
I (9229) example_common: - IPv4 address: 10.42.0.252,
I (9229) example_common: - IPv6 address: fe80:0000:0000:0000:d6f9:8dff:fe04:24b0, type: ESP_IP6_ADDR_IS_LINK_LOCAL
I (9229) example: Updating time from NVS
I (9229) time_sync: Syncing time from SNTP server.
I (9229) time_sync: Initializing SNTP
I (9829) wifi:<ba-add>idx:1 (ifx:0, d8:3a:dd:a3:c7:39), tid:0, ssn:2, winSize:64
I (11229) time_sync: Waiting for system time to be set... (1/10)
I (13229) time_sync: Waiting for system time to be set... (2/10)
I (15229) time_sync: Waiting for system time to be set... (3/10)
I (17229) time_sync: Waiting for system time to be set... (4/10)

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

Re: DHCP SNTP not sending packets, Not Working

Postby nopnop2002 » Mon Aug 04, 2025 8:21 am

I think you need to set the default NTP server that the WiFi router will use on the WiFi router side.


The time setting function may not exist depending on the model of your WiFi router.

savages
Posts: 6
Joined: Thu Sep 21, 2023 5:44 pm

Re: DHCP SNTP not sending packets, Not Working

Postby savages » Mon Aug 04, 2025 9:35 am

I am using a raspberry Pi with ntp install.
Here is the ntp server listening and accessing it

svvs@raspberrypi:~ $ sudo ntpdate
2025-08-04 03:32:34.269338 (-0600) -0.000003 +/- 0.000050 localhost ::1 s4 no-leap
svvs@raspberrypi:~ $ ss -lu
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
UNCONN 0 0 0.0.0.0:36239 0.0.0.0:*
UNCONN 0 0 10.42.0.1:domain 0.0.0.0:*
UNCONN 0 0 0.0.0.0:bootps 0.0.0.0:*
UNCONN 0 0 10.42.0.1:ntp 0.0.0.0:*
UNCONN 0 0 192.168.42.7:ntp 0.0.0.0:*
UNCONN 0 0 127.0.0.1:ntp 0.0.0.0:*
UNCONN 0 0 0.0.0.0:ntp 0.0.0.0:*
UNCONN 0 0 0.0.0.0:mdns 0.0.0.0:*
UNCONN 0 0 *:60936 *:*
UNCONN 0 0 [fe80::309:d698:dbe:d54a]:ntp *:*
UNCONN 0 0 [fe80::6b6e:a478:5035:ac20]:ntp *:*
UNCONN 0 0 [::1]:ntp *:*
UNCONN 0 0 *:ntp *:*
UNCONN 0 0 *:mdns *:*

savages
Posts: 6
Joined: Thu Sep 21, 2023 5:44 pm

Re: DHCP SNTP not sending packets, Not Working

Postby savages » Mon Aug 04, 2025 6:16 pm

This could be the solution, I am connecting to WiFi before I set config.

https://github.com/espressif/esp-idf/bl ... ple_main.c

* NOTE: This call should be made BEFORE esp acquires IP address from DHCP,
* otherwise NTP option would be rejected by default.
*/

Who is online

Users browsing this forum: coccocbot, Semrush [Bot] and 15 guests