0

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?

Misha
  • 556
  • 1
  • 8
  • 25

1 Answers1

0

Your Arduino devices can reach google.com because your local DNS server knows how to resolve this hostname to an IP address. But your local DNS server doesn't know about the hostname that you have assigned to your web server.

mDNS was designed to solve your specific problem.

This uses multicast to resolve domains ending in .local

The ESP8266 implementation has example code at https://tttapa.github.io/ESP8266/Chap08%20-%20mDNS.html

The webserver would configure the mDNS responder using:

MDNS.begin("ArduinoWebServer")

The clients can then connect using the hostname ArduinoWebServer.local

Ben T
  • 4,656
  • 3
  • 22
  • 22
  • Thank you for your response. I will definitely give this a try. I got some questions if you done mind: 1. Do I use the mDNS to connect to the network instead of standard wifi.begin()? 2. If so, am I right in saying that I would have to use mDNS on bothe the server and all clients? 3. Will this work with Arduino Nano33 Iot? I dont see a library for it or one for the MKR1xxx – Misha Oct 25 '19 at 17:26
  • the library and line #include seems to be incompatible with Arduino Nano33 IoT. Unless I am missing something. – Misha Oct 25 '19 at 18:15
  • I also tried using the mDSNresolver proposed here: https://github.com/madpilot/mDNSResolver. Where I run into the same library issues. – Misha Oct 25 '19 at 19:51