Skip to content

Use the NTP server from DHCP on ESP32 - #1331

Closed
LordMike wants to merge 2 commits into
UtilitechAS:mainfrom
LordMike:fix/esp32-ntp-from-dhcp
Closed

LordMike wants to merge 2 commits into
UtilitechAS:mainfrom
LordMike:fix/esp32-ntp-from-dhcp

Conversation

@LordMike

Copy link
Copy Markdown

Closes #1330

Why

"Obtain NTP server from DHCP" has no effect on ESP32. The lwIP in the Arduino-ESP32 2.0.x SDK is precompiled without LWIP_DHCP_GET_NTP_SRV, so sntp_servermode_dhcp() is an empty macro and the DHCP client never requests option 42. Details in #1330.

This is a workaround, and not a pretty one: it can be dropped once the project moves to Arduino-ESP32 3.x, where lwIP has the option compiled in. It only runs when "obtain from DHCP" is ticked.

Changes

1. Ask DHCP for the NTP server on ESP32 (b2338d7c)

  • New src/DhcpNtp.{h,cpp} (ESP32 only). After the network comes up, the device sends a DHCPINFORM (RFC 2131 3.4) asking only for option 42 and uses the returned server for SNTP.
  • lwIP's DHCP client holds UDP port 68 without SO_REUSEADDR, so we can't bind our own socket there. Instead, on the lwIP thread, the request is sent from lwIP's own DHCP pcb, and a shim is put in front of its receive callback while the request is in flight. The shim takes only the reply with our transaction id and passes everything else to lwIP unchanged. It is removed on the reply or after 3 s.
  • handleNtp() arms the request instead of calling the no-op sntp_servermode_dhcp() on ESP32. handleDhcpNtp() in the main loop sends it, retries up to 3 times 60 s apart, and asks again after every reconnect. The main loop never waits on the reply, so reading the HAN port isn't held up.
  • If the server offers no NTP server or doesn't answer, the configured server (or pool.ntp.org) stays in use.
  • ESP8266 is unchanged.

2. Show the NTP server received from DHCP (c4ca1ef9)

  • NtpStatus records the DHCP-provided server in use. The NTP entry in the services status (Services tile, sysinfo.json and the MQTT services announcement) shows it instead of the configured name.
  • On the configuration page, the NTP server field is disabled while "obtain from DHCP" is ticked and shows the server in use.
  • A disabled field isn't posted, so handleSave() now keeps the stored server when ntph is missing. It stays the fallback when DHCP offers none.
  • ui/dist rebuilt.

Testing

On an ESP32-S2, on a VLAN without internet access whose DHCP server hands out an NTP server (option 42), with the NTP server field empty:

  • DHCP off, after reboot: NTP stays at "connecting", since pool.ntp.org isn't reachable.
  • DHCP on: the telnet log shows Using NTP server 192.168.8.30 from DHCP, and NTP syncs within seconds.
  • After a reboot with DHCP on, NTP syncs and the services status shows 192.168.8.30. The configuration page shows the same address in the disabled field.
  • pio run passes for esp32s2, esp32 and esp8266.

Not tested: saving the configuration page with DHCP ticked and a stored server (the keep-stored-server path), and other DHCP servers than UniFi.

🤖 Generated with Claude Code

LordMike and others added 2 commits November 23, 2026 22:21
"Obtain NTP server from DHCP" had no effect on ESP32. The lwIP in the
Arduino-ESP32 2.0.x SDK is precompiled without LWIP_DHCP_GET_NTP_SRV, so
sntp_servermode_dhcp() is an empty macro and the DHCP client never
requests option 42.

When the option is ticked, send a DHCPINFORM for option 42 after the
network comes up and use the returned server for SNTP. lwIP's DHCP client
holds UDP port 68 without SO_REUSEADDR, so the request is sent from its pcb
and a shim in front of its receive callback takes the matching reply,
passing everything else on. The main loop never waits for the reply.
Falls back to the configured server (or pool.ntp.org) when the server
offers none or does not answer; asks again after a reconnect.

ESP8266 is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The NTP entry in the services status (Services tile, sysinfo.json and the
MQTT services announcement) now shows the server DHCP handed out when one
is in use, instead of the configured name.

On the configuration page the NTP server field is disabled while "obtain
from DHCP" is ticked and shows the server in use. A disabled field is not
posted, so the save handler keeps the stored server, which stays the
fallback when DHCP offers none.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@LordMike
LordMike marked this pull request as ready for review November 23, 2026 21:25
@gskjold

gskjold commented Nov 26, 2026

Copy link
Copy Markdown
Member

Thank you for the thorough analysis and for the working implementation. We have decided to remove the option instead of carrying our own DHCPINFORM client next to lwIP's, as discussed in #1330. That is done in #1335, which also keeps sntp_servermode_dhcp(0) so a DHCP-provided server on ESP8266 can't replace the configured one. If we move to Arduino-ESP32 3.x, where lwIP supports DHCP option 42, we can bring the option back on top of that.

@gskjold gskjold closed this Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Obtain NTP server from DHCP" has no effect on ESP32

2 participants