1。问题:
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设备可以访问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
连接