Saturday, May 20, 2017

Demo 9: How to use mDNS to resolve host names to Arduino ESP32 IP addresses

1. Introduction
- Just imagine the situation, there is a network and your ESP32 is in that network. Now you want to communicate with ESP32, so you need to know the IP address of ESP32. There are 3 ways (or more) to know the IP address of it:
 + Print or show the IP address of ESP32 on to Terminal, LCD, …
 + Use a Network Scanner application (Fing -  on Android and iOS) to detect the IP address of ESP32
 + Use mDNS
- In this section we will use the third way mDNS. mDNS is stand for multicast Domain Name System. It resolves host names to IP addresses within small networks. Back to the situation above, you have an ESP32 connect to the network with its host name is "esp32.local" and there is an application that want to connect to ESP32. Using mDNS, the application on PC sends a multicast message to all members of network that asks the host having that name “esp32.local” to identify itself. When ESP32 received the message, it multicasts a message that includes its IP address. All machines in that network can then use that information to update their mDNS caches.
- Beside that there is one concept that we need to know: Service discovery. It is the automatic detection of services offered by these devices on a network. It means if ESP32 offers http service, this service can be detected and used by the others automatically.
Note: For those who use Windows, because Windows does not support mDNS as default (except Win10) so you need to install a third-party application, called Bonjour, you can download it here.
And follow these steps here to install and check it.
2. Hardware
You need not any extra hardware
3. Software
- In order to use mDNS in ESP32 we will include "ESPmDNS.h" which supplied some useful functions:
- MDNS.begin("esp32"): start mDNS with full host name is "esp32.local" (!!!when using mDNS we need to add suffix ".local" to host). To access just use "esp32.local" instead of IP address
- MDNS.addService("http", "tcp", 80): Add http service (port 80) to MDNS - Service discovery
For demonstration, we will reuse the Demo 8.1, so we change the requirement a little. In that sample instead of showing the "Hello world" when user access the webserver through IP address of ESP32, we will use the name "esp32.local".
3.1 Node-RED model
Just Import the Json string below:
[{"id":"866fb572.d4ebc","type":"tcp out","z":"f5a25538.3c5518","host":"esp32.local","port":"8088",
"beserver":"client","base64":false,"end":false,"name":"","x":410.5,
"y":163,"wires":[]}] 
You can see in this Json string the filed: "host":"esp32.local"
Then click the button Deploy (top-right Red button) to deploy the model.
3.2 ESP32
Create Arduino project and Save as esp32newserver with code:
 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
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <WiFi.h>
#include <ESPmDNS.h>

const char* ssid     = "tenda";
const char* password = "phong707";

/* create a server and listen on port 8088 */
WiFiServer server(8088);
void setup()
{
    Serial.begin(115200);
    Serial.print("Connecting to ");
    Serial.println(ssid);
    /* connecting to WiFi */
    WiFi.begin(ssid, password);

    /*wait until ESP32 connect to WiFi*/
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected");
    if (!MDNS.begin("esp32")) {
        Serial.println("Error setting up MDNS responder!");
        while(1){
            delay(1000);
        }
    }
    /* start Server */
    server.begin();
}

void loop(){
    /* listen for client */
    WiFiClient client = server.available(); 
    uint8_t data[30]; 
    if (client) {                   
      Serial.println("new client");         
      /* check client is connected */           
      while (client.connected()) {          
          if (client.available()) {
              int len = client.read(data, 30);
              if(len < 30){
                  data[len] = '\0';  
              }else {
                  data[30] = '\0';
              }    
              Serial.print("client sent: ");            
              Serial.println((char *)data); 
          }
      }   
    }
}
4. Result

Figure: Result after using mDNS

0 comments: