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
4. Result
- 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) { } |
Figure: Host name to IP
12 Comments
Regards,
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.
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??
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
i even tried with delay still its not working