Arduino客户端无法连接到主机名服务器

问题描述 投票:0回答:1

1。问题:

  • 我正在尝试使用以下方法通过本地WiFi连接两个Arduino Nano33 IoT主机名。
  • 我试图消除不必设置静态IP地址的情况。
  • 使用IP地址连接时,整个程序运行完美。
  • 但是,当我尝试使用主机名连接到Web服务器arduino时,客户端无法连接。 (例如,当连接到google.com时除外)

2。我尝试了两种方法都不起作用:

客户端方法1:

char serverName[] = "ArduinoWebServer";
//char serverName[] = "google.com";         // if I use this as the serverName the client connects successfully

while (!client.connect(serverName,iPort)){
  Serial.println("Server not found");
  delay(5000);
}
if (client.connect(serverName,iPort)){
  Serial.println("Connected to Server");
}

客户端方法2:

  IPAddress ipServer;
  int err = WiFi.hostByName(serverName, ipServer) ;
  if(err == 1){
        Serial.print("Ip address: ");
        Serial.println(ipServer);
  } else {
        Serial.print("Error code: ");
        Serial.println(err);
  }

  while (!oClient.connect(ipServer,iPort)){
    Serial.println("Server not found");
    delay(5000);
  }
  if (oClient.connect(ipServer,iPort)){
    Serial.println("Connected to Server");
  }

服务器代码:

  WiFi.setHostname("ArduinoWebServer");
  //WiFi.config(ipServer);                // when using this instead of WiFi.setHostname the code works
  while (status != WL_CONNECTED){
    status = WiFi.begin(cSSID, cPass);

    if (status != WL_CONNECTED){
      Serial.println("Network not found, waiting to reconnect");
      delay(5000);
    }
  }
  oServer.begin();

有人知道解决方法吗?

arduino android-wifi hostname
1个回答
0
投票

您的Arduino设备可以访问google.com,因为您的本地DNS服务器知道如何将此主机名解析为IP地址。但是您的本地DNS服务器不知道您已分配给Web服务器的主机名。

mDNS旨在解决您的特定问题。

这使用多播来解析以。local结尾的域

[ESP8266实现在https://tttapa.github.io/ESP8266/Chap08%20-%20mDNS.html处有示例代码

Web服务器将使用以下方式配置mDNS响应器:

MDNS.begin("ArduinoWebServer")

然后,客户端可以使用主机名ArduinoWebServer.local连接

© www.soinside.com 2019 - 2024. All rights reserved.