How to get the IP address of a node by its mdns host name in Arduino ESP32

1. Introduction
- The benefit of mDNS is mentioned here.

- This is necessary when our ESP32 want to connect to a gateway/server (node) without knowing its IP address except the mdns host name.
- We will use the function "IPAddress queryHost(char *host)" which is available in the mdns library.
- In order to use this function, our Arduino ESP32 must join in the same network with the node and mdns service is initialized on ESP32.
2. Hardware
No extra hardware
3. Software
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiClient.h>

const char* ssid = "dd-wrt";
const char* password = "0000000000";
char * serverHostname = "iotsharing";//only use host name and remove .local

void setup(void)
{  
    Serial.begin(115200);

    // Connect to WiFi network
    WiFi.begin(ssid, password);
    Serial.println("");

    // Wait for connection
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());

    if (!MDNS.begin("esp32")) {
        Serial.println("Error setting up MDNS responder!");
        while(1) {
            delay(1000);
        }
    }
    Serial.println("mDNS responder started");
    IPAddress serverIp = MDNS.queryHost(serverHostname);
    Serial.print("IP address of server: ");
    Serial.println(serverIp.toString());
}
void loop(void)
{
}
4. Result
Figure: Host name to IP

Post a Comment

12 Comments

Anonymous said…
Great tutorial, thank U :)
Unknown said…
I have some stupid question, what is the different between connected to local IP address, and the server IP address? It is not the same? When ESP32 connected to the network, the router gives the ESP32 device a local IP address...Isn't this local IP address is the server IP??

Regards,
Kenny
Hi Kenny,

Imagining you are standing on the ESP and the IP of ESP will be local. If there are 2 ESPs (A and B) that connect to the same router and A acts as a server and you are standing on B so the IP of A will be IP server.
Unknown said…
@iotsharing,

Thank you for the info! Meanwhile, i do get the IP address of the server is 0.0.0.0, while my local IP address is 192.168.1.103. Is this abnormal??
it is abnormal. your esp did not connect to the network.
Bryan said…
I too have the same problem, the server IP address returns 0.0.0.0.
I have joined the network and received an IP address for the esp32 from the router.
I can ping the raspberry pi server from my mac with its name.local but the code does not return a server IP.
Any suggestions on how to troubleshoot?

Thanks
Bryan said…
so it turns out that there is a delay in the MDNS.queryHost() response. I have to wait a full 5 sec after the call to get the response. I just put a delay in and then it worked. Anyone have any ideas of what is going on?
Abraham said…
server IP address returns 0.0.0.0.
i even tried with delay still its not working
Canberk said…
The default timeout parameter for queryHost is 2000 milliseconds. Calling queryHost with longer timeout worked for me and may also work for you. e.g: mdns.queryHost("myhost", uint32_t timeout=5000);
buddydip said…
I am still not able to resolve the IPaddress from hostname from ESP32 though I can ping that local server (RPi) using the hostname from my laptop connected to LAN and it can resolve. I added 5000 timeout in the queryHost method and another 5000 delay after it but still getting IP address as 0.0.0.0. Any help will be highly appreciated as this is the single stucking point in my home automation project
buddydip said…
Got it working finally by changing the timeout value to 20000. It is taking longer time maybe I have around 25 devices connected to the same LAN. But happy that it worked and this blog is a life-saver for me. Thanks!