ESP32 Arduino: Getting station interface hostname

The tests from this tutorial were done using a DFRobot’s ESP32 module integrated in a ESP32 development board.

Introduction

In this tutorial we will learn how to obtain the hostname of the ESP32, using the Arduino core.

To test this functionality, we will first connect the ESP32 to a WiFi network and then obtain the hostname.

After that, we will ping the ESP32 from another machine connected to the same network using the obtained hostname (as already covered here, the ESP32 natively answers to ping requests when connected to a network).

The hostname should be correctly resolved to the ESP32 IP and thus the machine should be able to ping the it and receive an answer.

Note that if we don’t explicitly set the hostname of the ESP32, then it will default to “espressif“, as can be seen here. Thus, since we are only going to cover how to obtain the hostname and not how to set it, it is expected that we obtain “espressif” as result.

The tests from this tutorial were done using a DFRobot’s ESP32 module integrated in a ESP32 development board.

The code

We will start the code by including the WiFi.h library, so we can connect the ESP32 to a WiFi network.

#include "WiFi.h"

Naturally, we will also need the WiFi network credentials, more precisely the network name and password.

const char* ssid = "yourNetworkName";
const char* password = "yourNetworkAddress";

Moving on to the Arduino setup, we will initialize the serial interface, so we can output the hostname we will obtain.

Serial.begin(115200);

Then we will call the begin method on the WiFi extern variable, to start the connection to the WiFi network. This method receives as first input the network name and as second input its password.

WiFi.begin(ssid, password);

Since the connection will be performed asynchronously, we will poll its status until it is equal to the enumerated value WL_CONNECTED. We can obtain the status of the connection by calling the status method on the WiFi variable.

while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
}

After the previous loop ends, it means that the ESP32 is connected to the WiFi network. So, we will first obtain the local IP assigned to it by calling the localIP method on the WiFi extern variable.

Serial.println(WiFi.localIP());

Finally, to obtain the hostname, we simply need to call the getHostname method of the WiFi variable. This method takes no arguments and returns as output the hostname as a string.

You can check the implementation of this method here. Under the hood, this method calls the tcpip_adapter_get_hostname, a lower level function from the IDF framework.

Serial.println(WiFi.getHostname());

The final code can be seen below.

#include "WiFi.h"

const char* ssid = "yourNetworkName";
const char* password =  "yourNetworkAddress";

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

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  } 

  Serial.println(WiFi.localIP());
  Serial.println(WiFi.getHostname());
}

void loop(){}

Testing the code

To test the code, start by compiling it and uploading it to your device using the Arduino IDE. When the procedure finishes, open the Arduino IDE serial monitor and wait for the WiFi connection to be established.

After that, you should get an output like the one on figure 1, which shows the hostname getting printed to the console. Above, we also have the local IP assigned to the device on the network.

ESP32 Arduino getting the station interface hostname

Figure 1 – Output of the program.

Next we will try to send a ping to the ESP32 using the hostname instead of the IP (on this previous tutorial we have pinged the ESP32 using its IP). To do it, open a command line on a machine connected to the same network and send the following command:

ping espressif

As shown in figure 2, the hostname used in the command should be resolved to the actual IP of the ESP32 on the network (note that it matches the IP obtained on figured 1). Thus, the ESP32 should successfully answer the ping request.

Pinging the ESP32 by hostname

Figure 2 – Pinging the ESP32 using its hostname.

10 thoughts on “ESP32 Arduino: Getting station interface hostname”

  1. will interesting how one can set the host-name it seems that
    WiFi.setHostname(“HOSTNAME”); after wifi.begin don’t work

    1. Hi!

      I’ve also been running some tests on setting the hostname with no success so far.

      Even though it seems to be possible to set it on the ESP32 side (the next calls to the getHostname will return the new one), I cannot seem to, for example, ping the ESP32 on the new hostname.

      If you happen to find a solution, please let us know 🙂

      Best regards,
      Nuno Santos

  2. will interesting how one can set the host-name it seems that
    WiFi.setHostname(“HOSTNAME”); after wifi.begin don’t work

    1. Hi!
      I’ve also been running some tests on setting the hostname with no success so far.
      Even though it seems to be possible to set it on the ESP32 side (the next calls to the getHostname will return the new one), I cannot seem to, for example, ping the ESP32 on the new hostname.
      If you happen to find a solution, please let us know 🙂
      Best regards,
      Nuno Santos

  3. When I tried following code, it compile and upload but I am not able to ping by using cmd *ping espressif*

    can any one let me know, what is i am missing here.

    Thanks in Advance!!

    #include “WiFi.h”

    const char* ssid = “TP-Link_83A6”;
    const char* password = “59956150”;

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

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Terminal.println(“Connecting to WiFi..”);
    }

    Terminal.println(WiFi.localIP());
    Terminal.println(WiFi.getHostname());
    }

    void loop(){}

  4. It works for the IP, but when I use “ping espressif.local” this error message appears in the Terminal: “ping: espressif.local: Name or service not known”
    (though I got the hostname “espressif” from getHostname())

  5. // call is only a workaround for bug in WiFi class
    WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE); // Workaround: makes setHostname() work
    WiFi.setHostname(clientId);

Leave a Reply to antepherCancel reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading