How to enable mDNS?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
bulletmark
Posts: 59
Joined: Mon Mar 29, 2021 1:36 am
Location: Brisbane Australia

How to enable mDNS?

Post by bulletmark » Sat Aug 14, 2021 2:24 am

I understand MicroPython on ESP32 enables mDNS host announcement (since v1.12) but how can I get this to work? My ESP32 is connecting to my local network, e.g:

Code: Select all

import network
wlan = network.WLAN(network.STA_IF)
if not wlan.isconnected():
    wlan.active(True)
    wlan.connect('myssid', 'mypassword')
    while not wlan.isconnected():
        pass
I get an address and I can communicate fine on the network. How do I set this up to advertise a .local address so I can use this from a client machine instead of the IP address?

I can't find anything about mDNS in the MicroPython documentation and older threads here and elsewhere are vague.

bulletmark
Posts: 59
Joined: Mon Mar 29, 2021 1:36 am
Location: Brisbane Australia

Re: How to enable mDNS?

Post by bulletmark » Mon Aug 16, 2021 4:42 am

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to enable mDNS?

Post by pythoncoder » Mon Aug 16, 2021 6:58 am

That is neat and useful. You could alternatively use machine.unique_id().
Peter Hinch
Index to my micropython libraries.

bulletmark
Posts: 59
Joined: Mon Mar 29, 2021 1:36 am
Location: Brisbane Australia

Re: How to enable mDNS?

Post by bulletmark » Mon Aug 16, 2021 7:32 am

machine.unique_id() returns exactly the same byte string as wlan.config('mac'). There is no point importing another package.

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: How to enable mDNS?

Post by scruss » Wed Jul 13, 2022 9:18 pm

bulletmark wrote:
Mon Aug 16, 2021 7:32 am
machine.unique_id() returns exactly the same byte string as wlan.config('mac'). There is no point importing another package.
On ESP32 it might, but not everywhere. For instance, on a Raspberry Pi Pico W:

Code: Select all

mac: b'(\xcd\xc1\x00\x9d\xad'
unique_id: b'\xe6aLw[$15'
machine is usually imported by default, seemingly

bulletmark
Posts: 59
Joined: Mon Mar 29, 2021 1:36 am
Location: Brisbane Australia

Re: How to enable mDNS?

Post by bulletmark » Wed Jul 13, 2022 10:44 pm

scruss wrote:
Wed Jul 13, 2022 9:18 pm
machine is usually imported by default, seemingly
On a Raspberry Pi Pico W it might, but not everywhere. For instance, on a ESP32:

Code: Select all

>>> machine.unique_id()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'machine' isn't defined

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to enable mDNS?

Post by jimmo » Thu Jul 14, 2022 12:43 am

scruss wrote:
Wed Jul 13, 2022 9:18 pm
machine is usually imported by default, seemingly
On some ports there is frozen code that runs at startup to mount the filesystem etc which might import machine... Best not to rely on it.

Post Reply