1. Problem:
- I am trying to connect two Arduino Nano33 IoT's over local WiFi using a hostname.
- I am trying to eliminate having to set static IP addresses.
- When connecting using IP addresses the entire program works perfect.
- However, when I try to connect to the webserver arduino using a hostname, the client fails to connect. (except when connecting to google.com for example)
2. I have tried the two methods both dont work:
Client method 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");
}
Client method 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");
}
Server Code:
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();
Does anyone know a fix to this?