本文介绍了ESP32(DOIT DevKit)如何通过mDNS在同一LAN中找到另一个主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接到我的Wifi局域网的Raspberry Pi,它以 mqtt-broker.local 的形式响应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

我有一个ESP32 DOIT如果我使用IP地址 192.168.XXX.YYY 可以通过Wifi将消息发送到Raspberry Pi的DevKit设备,但是我希望ESP32使用mDNS解析主机。

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.

我无法使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...




  • 此代码出了什么问题?

  • 我应该把什么作为服务 MDNS.queryService( mqtt-broker, tcp)中?我什至尝试使用服务 mqtt 都没有运气,但是这没关系,无论Raspberry Pi(HTTP服务器,MQTT,FTP暴露了什么),mDNS东西都应该起作用等等...)

  • 在这里检查信息不多这种服务和原型,而我对底层C / C ++不太熟悉,这些是什么?

    • 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:

      推荐答案

      最终使用与Espressif提供的mDNS库中的类不同的方法( ESPmDNS.h ) ,组合形式:

      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);

      • 此检查的循环 serverIp.toString()== 0.0.0.0

      • 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();
      }
      

      这篇关于ESP32(DOIT DevKit)如何通过mDNS在同一LAN中找到另一个主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 22:28