【问题标题】:NodeMCU resolving Raspberry's local DNS through mDNSNodeMCU通过mDNS解析树莓的本地DNS
【发布时间】:2017-10-26 13:00:20
【问题描述】:

我在 Raspberry Pi 上设置了 .local 地址,可以从 PC 上的地址 raspberrypi.local 访问它。

现在我希望能够使用其 .local 地址从 NodeMCU 向 Raspberry 发出 HTTP 请求。

我发现这个答案提到 NodeMCU 需要设置 mDNS 解析器: ESP8266 nodemcu resolving raspberry's local dns

如何在 NodeMCU 上设置 mDNS?

【问题讨论】:

    标签: dns raspberry-pi esp8266 nodemcu mdns


    【解决方案1】:

    找到了解决办法!

    这里是注释代码。

    您需要包含ESP8266WiFiESP8266mDNS 库。

    // hostString will be used to identify this device, 
    // but not relevant as we're not providing mDNS services
    char hostString[16] = {0};
    
    void findMDNS() {
      // Need to make sure that we're connected to the wifi first
      while (WiFi.status() != WL_CONNECTED) {
        delay(250);
        Serial.print(".");
      } 
    
      if (!MDNS.begin(hostString)) {
        Serial.println("Error setting up MDNS responder!");
      }
    
      // We now query our network for 'device-info' service
      // over tcp, and get the number of available devices 
      int n = MDNS.queryService("device-info", "tcp");
      if (n == 0) {
        Serial.println("no services found");
      }
      else {
        for (int i = 0; i < n; ++i) {
          // Going through every available service,
          // we're searching for the one whose hostname 
          // matches what we want, and then get its IP
          if (MDNS.hostname(i) == RASPBERRY_HOSTNAME) {
            JENKINS_HOST = String(MDNS.IP(i)[0]) + String(".") +\
              String(MDNS.IP(i)[1]) + String(".") +\
              String(MDNS.IP(i)[2]) + String(".") +\
              String(MDNS.IP(i)[3]);
          }
        }
      }
    }
    

    【讨论】:

    • 也许我在这里有点偏离轨道,但是,“JENKINS_HOST”应该是什么?我无法理解它。看起来应该是 IP 地址?
    猜你喜欢
    • 2016-10-22
    • 2015-06-17
    • 1970-01-01
    • 2015-02-17
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多