Use ESP32 hostname for built-in mDNS
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user