8000 esp32: Add support for mDNS queries and responder. by dpgeorge · Pull Request #4951 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

esp32: Add support for mDNS queries and responder. #4951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

dpgeorge
Copy link
Member

This adds support to the esp32 for mDNS queries and a responder. See discussion in #4912.

For queries do: socket.getaddrinfo('device.local', 123)

The responder is automatically enabled, with default hostname "esp32.local".

TODO: work out a way to specify the hostname. Probably need a new config option wlan.config(mdns_hostname=...). Or could reuse wlan.config(dhcp_hostname=...). Even though there are different concepts of hostnames it'd probably be a good idea to unify them into a single config, eg wlan.config(hostname=...).

@dpgeorge dpgeorge mentioned this pull request Jul 26, 2019
@nickovs
Copy link
Contributor
nickovs commented Aug 5, 2019

Thanks! It would be great to have this for both queries and responder. Almost all of my ESP32s are currently running my somewhat flaky slimDNS so that I can find them on the network but using the built-in implementation in the ESPIDF would be much better.

As for setting the host name, I struggle to think of a situation where one would want to have different DHCP and mDNS host names, but I suspect that if it were disallowed then someone would quickly come up with a use case. There is a lot of existing code that expects to be able to set dhcp_hostname and I think that if the user just sets that then you should use it for the mDNS name too. It might make sense to add the mdns_hostname setting to allow them to be set independently just in case, as long as it's only taking up ROM.

@dpgeorge
Copy link
Member Author
dpgeorge commented Aug 6, 2019

As for setting the host name, I struggle to think of a situation where one would want to have different DHCP and mDNS host names, but I suspect that if it were disallowed then someone would quickly come up with a use case

Maybe. But it'd be good to er on the side of "keep things minimal" and not have separate hostname configs until there really is a use case.

There is a lot of existing code that expects to be able to set dhcp_hostname and I think that if the user just sets that then you should use it for the mDNS name too.

Yes, to maintain backwards compatibility we should keep dhcp_hostname. And then reusing it for mDNS makes sense, at least for now.

But in general the hostname is not specific to a given NIC, rather it's a system wide thing (like /etc/hostname is). So that would point to having a global configuration, something like network.config(hostname=...) (where network is the actual network module, not a specific NIC), which sets the default hostname for all NICs and all protocols (DHCP, mDNS, etc). Then this default could be overridden by specific configs like wlan.config(dhcp_hostname=...).

@nickovs
Copy link
Contributor
nickovs commented Aug 6, 2019

So that would point to having a global configuration, something like network.config(hostname=...) ... which sets the default hostname for all NICs ... Then this default could be overridden by specific configs like wlan.config(dhcp_hostname=...)

That seems like a sensible path to take. 8000 The ESP IDF deems the hostname to be a property of the adaptor but as you say it's really a property of the host.

In the mean time I patched your diff to just pull the current hostname back from the adaptor using tcpip_adapter_get_hostname() and use that for the mDNS name, and it seems to do the right thing. My tweaked version of the handler for the SYSTEM_EVENT_STA_GOT_IP event to read as below.

if (!mdns_initialised) {
    const char *hostname;

    ESP_EXCEPTIONS(tcpip_adapter_get_hostname(WIFI_IF_STA, &hostname));
    if (!hostname) {
        hostname = "esp32";
    }

    mdns_init();
    mdns_hostname_set(hostname);
    mdns_instance_name_set(hostname);
    mdns_initialised = true;
}

@dpgeorge
Copy link
Member Author

In the mean time I patched your diff to just pull the current hostname back from the adaptor using tcpip_adapter_get_hostname() and use that for the mDNS name,

Great, thanks for that, I've added this to the mDNS patch here and merged it in 2ccf030. I also added config macros for mDNS support because it may be something a user wants to disable (although eventually it could be dynamically configurable).

@dpgeorge dpgeorge closed this Aug 15, 2019
@dpgeorge dpgeorge deleted the esp32-mdns branch August 15, 2019 06:44
@jdtsmith
Copy link
jdtsmith commented Dec 29, 2019

I see in the new 1.12 release mDNS is enabled. Great work! I can get confirm resolving .local domains on the ESP32, but don't see a new esp32.local device. By examining my router I determined that the ESP32 has named itself espressif.local. Did a means of changing the hostname ever get settled on?

EDIT: Answering myself by reading through the code, the key was calling wlan.config(dhcp_hostname="myhost"), after the network connects. Here's my working wifi.py module to import. Would be helpful to add something along these lines to the docs!

import network
def start(quiet=False):
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        if not quiet: print('connecting to network...')
        wlan.connect('ssid', 'mypassword')
        while not wlan.isconnected():
            pass
        wlan.config(dhcp_hostname="myhost")
    if not quiet: print('network config:', wlan.ifconfig())
    return(wlan)

@street-grease-coder
Copy link

This example does not work for me in 1.13 (IDF4), no errors displayed. I can ping the device using the IP address, but not using the mDNS name 'myhost' (Ping request could not find host myhost. Please check the name and try again.). Is this expected behavior with IDF4, possibly?

I see in the new 1.12 release mDNS is enabled. Great work! I can get confirm resolving .local domains on the ESP32, but don't see a new esp32.local device. By examining my router I determined that the ESP32 has named itself espressif.local. Did a means of changing the hostname ever get settled on?

EDIT: Answering myself by reading through the code, the key was calling wlan.config(dhcp_hostname="myhost"), after the network connects. Here's my working wifi.py module to import. Would be helpful to add something along these lines to the docs!

import network
def start(quiet=False):
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        if not quiet: print('connecting to network...')
        wlan.connect('ssid', 'mypassword')
        while not wlan.isconnected():
            pass
        wlan.config(dhcp_hostname="myhost")
    if not quiet: print('network config:', wlan.ifconfig())
    return(wlan)

@jdtsmith
Copy link

This example does not work for me in 1.13 (IDF4), no errors displayed. I can ping the device using the IP address, but not using the mDNS name 'myhost'

You tried pinging myhost.local?

@street-grease-coder
Copy link
street-grease-coder commented Jan 10, 2021

I did. I'll check with the router what's going on. Ironically, if you use mDNS examples provided by espressif/arduino IDE (same device), the mDNS gets set flawlessly and you can access it - so I have some reasonable doubts about it not being my network. Should mention I'm pinging from windows, no bonjour service etc

@street-grease-coder
Copy link
street-grease-coder commented Jan 10, 2021

so some more info:

  • example code above does not work for me (i.e. cannot ping device over dns name)
  • I flashed the IDF3 version - this did not change anything.
  • The router recognizes the device with the correct IP (i.e. matches REPL output) - the name is recognized as espressif
  • neither espressif or myhost (nor the same with .local) can be pinged, I ensured I'm even in the same frequency of the network (2.4Ghz). Error in cmd under win10: Ping request could not find host the_hostnames_I_mentioned. Please check the name and try again.
  • I can ping the device without issues over IPv4
  • This works with the same esp32 in arduino code. My guess is the device name is set to espressif, but broadcasting of the mDNS to network / router doesn't work properly. I don't have a good enough understanding here to diagnose / help properly.
    -- cpp code for mDNS handling: https://github.com/espressif/arduino-esp32/tree/master/libraries/ESPmDNS/src
    -- arduino code that worked for me: https://github.com/espressif/arduino-esp32/blob/master/libraries/ESPmDNS/examples/mDNS_Web_Server/mDNS_Web_Server.ino

similar issue already mentioned here, possibly helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

< 3848 div class="participation">
4 participants
0