Search code examples
esp8266arduino-esp8266esp32mdns

How is the ESP32 (DOIT DevKit) finding another host in the same LAN via mDNS?


I have a Raspberry Pi connected to my Wifi LAN that responds to mDNS as mqtt-broker.local.

I can find it on my laptop with this command:

$ avahi-resolve-host-name -4 mqtt-broker.local
mqtt-broker.local   192.168.XXX.YYY

I have an ESP32 DOIT DevKit device that can send messages to the Raspberry Pi via Wifi if I use the IP address 192.168.XXX.YYY, however I would like my ESP32 to resolve the host using mDNS.

I am not able to get the mDNS working, the code at the bottom prints:

Finding the mDNS details...
No services found...
Done finding the mDNS details...
  • What's wrong with this code?
  • What should I put as service in MDNS.queryService("mqtt-broker", "tcp")? I have tried even with service mqtt with no luck, however this shouldn't matter, the mDNS stuff should work regardless what's exposed from the Raspberry Pi (HTTP server, MQTT, FTP whatever...)
  • Checking here https://github.com/espressif/arduino-esp32/blob/master/libraries/ESPmDNS/src/ESPmDNS.h#L98 there is not that much information about this "service" and "proto", and I am not that much familiar with low-level C/C++, what are these things?

This is the code I am using:

// import the headers
#include <ESPmDNS.h>

void findMyPi() {
  Serial.println("Finding the mDNS details...");

  // make sure we are connected to the Wifi
  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
    Serial.println("Not yet connected to Wifi...");
  }

  if (!MDNS.begin("whatever_this_could_be_anything")) {
    Serial.println("Error setting up MDNS responder!");
  }

  // what should I put in here as "service"?
  int n = MDNS.queryService("mqtt-broker", "tcp");
  if (n == 0) {
    Serial.println("No services found...");
  }
  else {
    for (int i = 0; i < n; ++i) {
      // Print details for each service found
      Serial.print("  ");
      Serial.print(i + 1);
      Serial.print(": ");
      Serial.print(MDNS.hostname(i)); // "mqtt-broker" ??? How can I find it???
      Serial.print(" (");
      Serial.print(MDNS.IP(i));
      Serial.print(":");
      Serial.print(MDNS.port(i));
      Serial.println(")");
    }
  }
  Serial.println("Done finding the mDNS details...");
}

This function has been inspired by this example:

https://github.com/espressif/arduino-esp32/blob/master/libraries/ESPmDNS/examples/mDNS-SD_Extended/mDNS-SD_Extended.ino


Solution

  • Ended up using a different method from the class on that mDNS library provided by Espressif (ESPmDNS.h), a combination of:

    • IPAddress serverIp = MDNS.queryHost(mDnsHost);
    • while loop on this check serverIp.toString() == "0.0.0.0"

    This is the code that glues up all together:

    // on my laptop (Ubuntu) the equivalent command is: `avahi-resolve-host-name -4 mqtt-broker.local`
    String findMDNS(String mDnsHost) { 
      // the input mDnsHost is e.g. "mqtt-broker" from "mqtt-broker.local"
      Serial.println("Finding the mDNS details...");
      // Need to make sure that we're connected to the wifi first
      while (WiFi.status() != WL_CONNECTED) {
        delay(250);
        Serial.print(".");
      }
      if (!MDNS.begin("esp32whatever")) {
        Serial.println("Error setting up MDNS responder!");
      } else {
        Serial.println("Finished intitializing the MDNS client...");
      }
    
      Serial.println("mDNS responder started");
      IPAddress serverIp = MDNS.queryHost(mDnsHost);
      while (serverIp.toString() == "0.0.0.0") {
        Serial.println("Trying again to resolve mDNS");
        delay(250);
        serverIp = MDNS.queryHost(mDnsHost);
      }
      Serial.print("IP address of server: ");
      Serial.println(serverIp.toString());
      Serial.println("Done finding the mDNS details...");
      return serverIp.toString();
    }