From 2a0869743f1352c828583d259675ff105e3d01d7 Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Wed, 26 Aug 2026 18:57:57 +0200 Subject: [PATCH] Use ESP32 hostname for built-in mDNS --- README.md | 4 ++-- main.py | 31 ++++++++++--------------------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 8bcbe14..c820e5f 100644 --- a/README.md +++ b/README.md @@ -23,14 +23,14 @@ If that import fails, use an up-to-date native-USB MicroPython build for the boa ## 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. 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. 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`. -`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 diff --git a/main.py b/main.py index 751d2ac..30bf4ba 100644 --- a/main.py +++ b/main.py @@ -15,11 +15,21 @@ KEY_HOLD_MS = 80 def connect_wifi(settings): wlan = network.WLAN(network.STA_IF) wlan.active(True) + set_mdns_hostname(wlan) if not wlan.isconnected(): wlan.connect(settings["wifi_ssid"], settings["wifi_password"]) 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): return seconds * 1000 @@ -41,15 +51,6 @@ def ip_address(wlan): 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(): settings = load() @@ -72,8 +73,6 @@ def main(): release_at = None consumer_release_at = None next_wifi_retry = now - mdns_server = None - next_mdns_retry = now next_display_refresh = now while True: @@ -90,16 +89,6 @@ def main(): display.update(settings["wifi_ssid"], address) 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: hid.release_keys()