Use ESP32 hostname for built-in mDNS

This commit is contained in:
2026-08-26 18:57:57 +02:00
parent 0e9637c474
commit 2a0869743f
2 changed files with 12 additions and 23 deletions
+2 -2
View File
@@ -23,14 +23,14 @@ If that import fails, use an up-to-date native-USB MicroPython build for the boa
## Install ## Install
1. Flash a current ESP32-S3 MicroPython build, then verify that `from machine import USBDevice` and `import mdns` both succeed at the REPL. 1. Flash a current ESP32-S3 MicroPython build, then verify that `from machine import USBDevice` succeeds at the REPL. A separate `mdns` module is not required on the standard ESP32 port.
2. Copy `settings.example.json` to the device filesystem as `settings.json` and replace all three credentials. Do this **before first boot**: the firmware cannot join Wi-Fi with the placeholder defaults. 2. Copy `settings.example.json` to the device filesystem as `settings.json` and replace all three credentials. Do this **before first boot**: the firmware cannot join Wi-Fi with the placeholder defaults.
3. Copy `boot.py`, `main.py`, `hid.py`, `settings.py`, `web.py`, and `display.py` to the device root filesystem. 3. Copy `boot.py`, `main.py`, `hid.py`, `settings.py`, `web.py`, and `display.py` to the device root filesystem.
4. Reset the board and plug its native USB connector into a dedicated test host. It should enumerate as a keyboard, mouse, and media-control HID device. 4. Reset the board and plug its native USB connector into a dedicated test host. It should enumerate as a keyboard, mouse, and media-control HID device.
5. Once it joins Wi-Fi, visit `http://polterhid.local/`. If the client/network does not support mDNS, find the device IP address in the training Wi-Fi DHCP leases and visit `http://DEVICE_IP/` instead. 5. Once it joins Wi-Fi, visit `http://polterhid.local/`. If the client/network does not support mDNS, find the device IP address in the training Wi-Fi DHCP leases and visit `http://DEVICE_IP/` instead.
6. Sign in with username `admin` and the `web_password` from `settings.json`. 6. Sign in with username `admin` and the `web_password` from `settings.json`.
`boot.py` configures the native USB interface before USB initialisation, while `main.py` runs the application. The device is intentionally **HID-only**: its built-in USB serial/CDC REPL is replaced by the HID device. Use the BOOT button to enter download mode for recovery, and verify the network configuration before deployment. On a LILYGO T-Dongle-S3, its built-in 160×80 ST7735 screen shows the configured Wi-Fi SSID and either `connecting...` or the DHCP-assigned IPv4 address. It advertises the HTTP page through mDNS as `polterhid.local`; this requires a MicroPython ESP32 build containing the `mdns` module. Configuration saved in the web page is retained in `settings.json` and takes effect immediately. `boot.py` configures the native USB interface before USB initialisation, while `main.py` runs the application. The device is intentionally **HID-only**: its built-in USB serial/CDC REPL is replaced by the HID device. Use the BOOT button to enter download mode for recovery, and verify the network configuration before deployment. On a LILYGO T-Dongle-S3, its built-in 160×80 ST7735 screen shows the configured Wi-Fi SSID and either `connecting...` or the DHCP-assigned IPv4 address. The firmware sets the ESP32 network hostname to `polterhid` before joining Wi-Fi; standard ESP32 MicroPython builds use their built-in mDNS responder to announce `polterhid.local`. Configuration saved in the web page is retained in `settings.json` and takes effect immediately.
## Operational safeguards ## Operational safeguards
+10 -21
View File
@@ -15,11 +15,21 @@ KEY_HOLD_MS = 80
def connect_wifi(settings): def connect_wifi(settings):
wlan = network.WLAN(network.STA_IF) wlan = network.WLAN(network.STA_IF)
wlan.active(True) wlan.active(True)
set_mdns_hostname(wlan)
if not wlan.isconnected(): if not wlan.isconnected():
wlan.connect(settings["wifi_ssid"], settings["wifi_password"]) wlan.connect(settings["wifi_ssid"], settings["wifi_password"])
return wlan return wlan
def set_mdns_hostname(wlan):
"""Set the hostname used by ESP32's built-in mDNS responder."""
try:
network.hostname("polterhid")
except AttributeError:
# Older ESP32 builds expose the same setting through the WLAN object.
wlan.config(dhcp_hostname="polterhid")
def seconds_to_ms(seconds): def seconds_to_ms(seconds):
return seconds * 1000 return seconds * 1000
@@ -41,15 +51,6 @@ def ip_address(wlan):
return wlan.ifconfig()[0] return wlan.ifconfig()[0]
def start_mdns():
"""Advertise the configuration page as http://polterhid.local/."""
import mdns
server = mdns.Server()
server.init("polterhid", "PolterHID")
server.advertise_service("_http", "_tcp", 80, "PolterHID")
return server
def main(): def main():
settings = load() settings = load()
@@ -72,8 +73,6 @@ def main():
release_at = None release_at = None
consumer_release_at = None consumer_release_at = None
next_wifi_retry = now next_wifi_retry = now
mdns_server = None
next_mdns_retry = now
next_display_refresh = now next_display_refresh = now
while True: while True:
@@ -90,16 +89,6 @@ def main():
display.update(settings["wifi_ssid"], address) display.update(settings["wifi_ssid"], address)
next_display_refresh = time.ticks_add(now, 1000) next_display_refresh = time.ticks_add(now, 1000)
if (wlan.isconnected() and mdns_server is None and
time.ticks_diff(now, next_mdns_retry) >= 0):
try:
mdns_server = start_mdns()
print("mDNS available at http://polterhid.local/")
except (ImportError, OSError, AttributeError) as error:
# Keep HID and web handling available by IP if this firmware
# build lacks mDNS or Wi-Fi is not ready yet.
print("mDNS unavailable:", error)
next_mdns_retry = time.ticks_add(now, 10000)
if release_at is not None and time.ticks_diff(now, release_at) >= 0: if release_at is not None and time.ticks_diff(now, release_at) >= 0:
hid.release_keys() hid.release_keys()