【发布时间】:2019-10-24 21:48:18
【问题描述】:
1.问题:
- 我正在尝试使用本地 WiFi 连接两个 Arduino Nano33 IoT 主机名。
- 我正在努力避免设置静态 IP 地址。
- 使用 IP 地址连接时,整个程序运行良好。
- 但是,当我尝试使用主机名连接到网络服务器 arduino 时,客户端无法连接。 (例如连接到 google.com 时除外)
2。这两种方法我都试过了:
客户端方法一:
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");
}
客户端方法二:
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