ESP32 mDNS support on Ethernet (class LAN)
Hi guys,
I wanted to know if there is a specific reason why the mDNS is supported on the WLAN interface (via hostname config parameter) for ESP32 but it is not supported on the LAN/Ethernet interface. There seems to be no limitation in IDF's TCP/IP Adapter library for this, but the ESP32 port does not expose a config parameter to set this.
ports/esp32/network_lan: Add mDNS support for Ethernet interface.
esp32: Add mDNS support for Ethernet (LAN) interface
Problem
Currently, mDNS responder is only initialized when WiFi connects (in network_wlan.c). When WiFi is disabled and only Ethernet is used, mDNS never starts, so the device is not discoverable via hostname.local.
This is problematic for IoT devices that use wired Ethernet connections and need to be discoverable on the local network without knowing their IP address.
Solution
Add mDNS initialization to network_lan.c when Ethernet gets an IP address via IP_EVENT_ETH_GOT_IP.
Changes
ports/esp32/network_lan.c:
- Add
#include "mdns.h" - Declare
extern bool mdns_initialisedto share state withnetwork_wlan.c - Add new
eth_ip_event_handler()that initializes mDNS when Ethernet obtains an IP - Register the handler for
IP_EVENT_ETH_GOT_IP
ports/esp32/network_wlan.c:
- Remove
staticfrommdns_initialisedto allow sharing withnetwork_lan.c
Testing
- Tested on ESP32-P4 with onboard Ethernet
- Verified device is discoverable via mDNS when only Ethernet is active (WiFi disabled)
- Verified mDNS still works when WiFi is active
- Verified mDNS is not re-initialized if already started by WiFi
Notes
- The
mdns_initialisedflag is shared between WiFi and Ethernet to prevent double-initialization - This follows the same pattern already used in
network_wlan.c - Requires
MICROPY_HW_ENABLE_MDNS_QUERIESorMICROPY_HW_ENABLE_MDNS_RESPONDERto be enabled