Well after reading around bug reports etc, and with some trial and error, I found the solution to my own question so will state it here given there is little documentation anywhere on how this works.
To be clear I want each esp32 running MicroPython on a DHCP network to create a unique hostname to which it will respond to regardless of the IP address it gets assigned (i.e. will respond a unique hostname to a mDNS query). I discovered that MicroPython (v1.16) does actually create a generic mDNS hostname called "Espressif" but that is useless when you have multiple esp32's on a network as I do. So I modified the above code to:
Code: Select all
import network
wlan = network.WLAN(network.STA_IF)
if not wlan.isconnected():
wlan.active(True)
mac = wlan.config('mac')
host = 'esp32-' + ''.join('{:02x}'.format(b) for b in mac[3:])
wlan.config(dhcp_hostname = host)
wlan.connect('myssid', 'mypassword')
while not wlan.isconnected():
pass
host = wlan.config('dhcp_hostname')
print('Wifi connected as {}/{}, net={}, gw={}, dns={}'.format(
host, *wlan.ifconfig()))
Now you can ping/connect/etc to the device via it's reported unique name "esp32-xxxxxx.local" regardless of it's assigned IP address.