Repository navigation
Conversation
"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
marked this pull request as ready for review
November 23, 2026 21:25
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, sosntp_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)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.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-opsntp_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.pool.ntp.org) stays in use.2. Show the NTP server received from DHCP (
c4ca1ef9)NtpStatusrecords the DHCP-provided server in use. The NTP entry in the services status (Services tile,sysinfo.jsonand the MQTT services announcement) shows it instead of the configured name.handleSave()now keeps the stored server whenntphis missing. It stays the fallback when DHCP offers none.ui/distrebuilt.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:
pool.ntp.orgisn't reachable.Using NTP server 192.168.8.30 from DHCP, and NTP syncs within seconds.192.168.8.30. The configuration page shows the same address in the disabled field.pio runpasses foresp32s2,esp32andesp8266.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