Espressif ESP32 Core 3.1.1 Why ping by name is Ko ?

My environment:
Arduino IDE V1.8.19
Preferences : https://arduino.esp8266.com/stable/package_esp8266com_index.json,https://espressif.github.io/arduino-esp32/package_esp32_index.json
Library esp32 by Espressif Systems v3.1.1

Hostname : Seems to be ok (not too long, no special characters)
Some documentation says that hostname must be set before calling WiFi.mode and WiFi.begin
Adding or not WiFi.config : nothing change
A try with an ESP8266 NodeMCU V1.0 (with another code ...) it's ok to ping the module by it's name

Connection to WiFi : Ok
Ping by IP : Ok
ping 192.168.1.87
PING 192.168.1.87 (192.168.1.87) 56(84) bytes of data.
64 bytes from 192.168.1.87: icmp_seq=1 ttl=64 time=146 ms

Ping by hostname : Ko
ping ESP32TEMP
ping: ESP32TEMP: Nom ou service inconnu

Test code

#include <WiFi.h>

// Replace with your network credentials (STATION)
// const char* ssid = "REPLACE_WITH_YOUR_SSID";
// const char* password = "REPLACE_WITH_YOUR_PASSWORD";

String hostname = "ESP32TEMP";

void initWiFi() {

  // With or without WiFi.config : no change
  // WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
  WiFi.setHostname(hostname.c_str()); //define hostname
  
  WiFi.mode(WIFI_STA);
  
  // WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
  // WiFi.setHostname(hostname.c_str()); //define hostname
  
  //wifi_station_set_hostname( hostname.c_str() );
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());
}

void setup() {
  Serial.begin(115200);
  initWiFi();
  Serial.print("RSSI: ");
  Serial.println(WiFi.RSSI());
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(1000);
  Serial.print(WiFi.getHostname());
  Serial.println(" Ok");
} 

Monitor : ESP32TEMP Ok

A scan network with nmap

nmap -sL 192.168.1.*

Nmap scan report for ESP32TEMP (192.168.1.87)

I can't find any solution to my problem. Thank you in advance.

Try adding ".local" to the hostname with nmap and ping.
I use character arrays rather than String.

Edit: Here is the difference. My RPi hostname is raspberrypi.

tim@tim2:~$ ping raspberrypi
ping: raspberrypi: Temporary failure in name resolution

tim@tim2:~$ ping raspberrypi.local
PING raspberrypi.local (192.168.1.22) 56(84) bytes of data.
64 bytes from 192.168.1.22 (192.168.1.22): icmp_seq=1 ttl=64 time=0.583 ms

Hum ...
Thanks for your request

You can access with .local when you use

#include <ESPmDNS.h>

void setup()
{
  ...

 if(!MDNS.begin(hostname)) { // your custom hostname
    Serial.println("Error starting mDNS");
 }
}

But with only adding .local to the hostname , the ping is Ko.

And the use of ESPmDNS the http Request like http://"hostname".local on my ESP32 seems random ok with an http code 200 and ko with code -1
The same request with the IP of my ESP32 it is always good
This is why I excluded the use of mDNS and i want to use the hostname

There is a problem with the ESP32 but I don't know if it's my code that is not good or if the problem is in the WiFi library and the ESP32.

As I recall, the ".local" extension tells ping and nmap to use an arp scan rather than DNS.

ESP32 Code :
String hostname = "ESP32TEMP";

nmap result:
Nmap scan report for ESP32TEMP (192.168.1.87)

ping result:

ping ESP32TEMP.local
ping: ESP32TEMP.local: Nom ou service inconnu

Indeed, ping seems not to make the same request because it takes longer to respond but the result is the same.

That part may be a challenge with your code or the library.
I( presume this means "Name or service unknown"

Nom ou service inconnu

Edit: Here's an oddity. My RPi doesn't need the ".local" extension to use an arp scan.
I was testing on a Ubuntu box. It may just be the way your OS does the ping and nmap.

Edit2: I was wrong. the ".local" extension is still needed with ping and nmap on my RPi. You don't need the extension if the hostname is localhost (same computer).

All the verbose debug at startup ...

ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:4916
load:0x40078000,len:16436
load:0x40080400,len:4
ho 8 tail 4 room 4
load:0x40080404,len:3524
entry 0x400805b8
[     1][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type UART_RX (2) successfully set to 0x400d983c
[    12][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type UART_TX (3) successfully set to 0x400d980c
[    26][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type UART_CTS (4) successfully set to 0x400d97dc
[    39][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type UART_RTS (5) successfully set to 0x400d97ac
[    53][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type UART_RX (2) successfully set to 0x400d983c
[    66][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type UART_TX (3) successfully set to 0x400d980c
[    80][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type UART_CTS (4) successfully set to 0x400d97dc
[    93][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type UART_RTS (5) successfully set to 0x400d97ac
[   107][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type UART_RX (2) successfully set to 0x400d983c
[   120][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type UART_TX (3) successfully set to 0x400d980c
[   134][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type UART_CTS (4) successfully set to 0x400d97dc
[   147][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type UART_RTS (5) successfully set to 0x400d97ac
[   187][D][esp32-hal-cpu.c:263] setCpuFrequencyMhz(): PLL: 480 / 2 = 240 Mhz, APB: 80000000 Hz
[   224][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 3 successfully set to type UART_RX (2) with bus 0x3ffbdb74
[   235][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 1 successfully set to type UART_TX (3) with bus 0x3ffbdb74
=========== Before Setup Start ===========
Chip Info:
------------------------------------------
  Model             : ESP32
  Package           : D0WD-Q5
  Revision          : 3.01
  Cores             : 2
  CPU Frequency     : 240 MHz
  XTAL Frequency    : 40 MHz
  Features Bitfield : 0x00000032
  Embedded Flash    : No
  Embedded PSRAM    : No
  2.4GHz WiFi       : Yes
  Classic BT        : Yes
  BT Low Energy     : Yes
  IEEE 802.15.4     : No
------------------------------------------
INTERNAL Memory Info:
------------------------------------------
  Total Size        :   336808 B ( 328.9 KB)
  Free Bytes        :   296580 B ( 289.6 KB)
  Allocated Bytes   :    32964 B (  32.2 KB)
  Minimum Free Bytes:   290708 B ( 283.9 KB)
  Largest Free Block:   110580 B ( 108.0 KB)
------------------------------------------
Flash Info:
------------------------------------------
  Chip Size         :  4194304 B (4 MB)
  Block Size        :    65536 B (  64.0 KB)
  Sector Size       :     4096 B (   4.0 KB)
  Page Size         :      256 B (   0.2 KB)
  Bus Speed         : 80 MHz
  Bus Mode          : QIO
------------------------------------------
Partitions Info:
------------------------------------------
                nvs : addr: 0x00009000, size:    20.0 KB, type: DATA, subtype: NVS
            otadata : addr: 0x0000E000, size:     8.0 KB, type: DATA, subtype: OTA
               app0 : addr: 0x00010000, size:  1280.0 KB, type:  APP, subtype: OTA_0
               app1 : addr: 0x00150000, size:  1280.0 KB, type:  APP, subtype: OTA_1
             spiffs : addr: 0x00290000, size:  1408.0 KB, type: DATA, subtype: SPIFFS
           coredump : addr: 0x003F0000, size:    64.0 KB, type: DATA, subtype: COREDUMP
------------------------------------------
Software Info:
------------------------------------------
  Compile Date/Time : Mar 17 2025 15:30:11
  Compile Host OS   : linux
  ESP-IDF Version   : v5.3.2-282-gcfea4f7c98-dirty
  Arduino Version   : 3.1.1
------------------------------------------
Board Info:
------------------------------------------
  Arduino Board     : ESP32_WROOM_DA
  Arduino Variant   : esp32da
  Arduino FQBN      : esp32:esp32:esp32da:PartitionScheme=default,CPUFreq=240,FlashMode=qio,FlashFreq=80,FlashSize=4M,UploadSpeed=921600,LoopCore=1,EventsCore=1,DebugLevel=verbose,EraseFlash=none
============ Before Setup End ============
[   741][V][esp32-hal-uart.c:421] uartBegin(): UART0 baud(115200) Mode(800001c) rxPin(3) txPin(1)
[   750][V][esp32-hal-uart.c:510] uartBegin(): UART0 not installed. Starting installation
[   761][V][esp32-hal-uart.c:575] uartBegin(): UART0 initialization done.
[   795][V][NetworkEvents.cpp:113] _checkForEvent(): Network Event: 101 - WIFI_READY
[   955][I][WiFiGeneric.cpp:799] setDualAntennaConfig(): TX Antenna will be automatically selected
[   956][V][STA.cpp:186] _onStaEvent(): STA Started
[   968][V][NetworkEvents.cpp:113] _checkForEvent(): Network Event: 110 - STA_START
[   976][V][STA.cpp:110] _onStaArduinoEvent(): Arduino STA Event: 110 - STA_START
Connecting to WiFi ...[  1034][V][STA.cpp:206] _onStaEvent(): STA Connected: SSID: SFR_A760, BSSID: cc:2d:1b:73:a7:66, Channel: 1, Auth: WPA_WPA2_PSK
[  1046][V][NetworkEvents.cpp:113] _checkForEvent(): Network Event: 112 - STA_CONNECTED
[  1054][V][STA.cpp:110] _onStaArduinoEvent(): Arduino STA Event: 112 - STA_CONNECTED
[  1554][V][NetworkInterface.cpp:78] _onIpEvent(): sta Got New IP: 192.168.1.87 MASK: 255.255.255.0 GW: 192.168.1.1
[  1565][V][NetworkEvents.cpp:113] _checkForEvent(): Network Event: 115 - STA_GOT_IP
[  1572][V][STA.cpp:110] _onStaArduinoEvent(): Arduino STA Event: 115 - STA_GOT_IP
[  1579][V][STA.cpp:171] _onStaArduinoEvent(): STA IP: 192.168.1.87, MASK: 255.255.255.0, GW: 192.168.1.1

192.168.1.87
RRSI: -45
=========== After Setup Start ============
INTERNAL Memory Info:
------------------------------------------
  Total Size        :   336808 B ( 328.9 KB)
  Free Bytes        :   245364 B ( 239.6 KB)
  Allocated Bytes   :    81420 B (  79.5 KB)
  Minimum Free Bytes:   242896 B ( 237.2 KB)
  Largest Free Block:   110580 B ( 108.0 KB)
------------------------------------------
GPIO Info:
------------------------------------------
  GPIO : BUS_TYPE[bus/unit][chan]
  --------------------------------------  
     1 : UART_TX[0]
     3 : UART_RX[0]
============ After Setup End =============
ESP32TEMP Ok
ESP32TEMP Ok
ESP32TEMP Ok
...

Response given by espressif:

This is a different hostname. One that is reported to the DHCP server, not one that you can ping.

  • For Windows you needs NBNS (ping ESP32TEMP)
  • For Mac and Linux you need MDNS (ping ESP32TEMP.local)
#include "NetBIOS.h"
#include "ESPmDNS.h"
 #include <WiFi.h>

// Replace with your network credentials (STATION)
// const char* ssid = "REPLACE_WITH_YOUR_SSID";
// const char* password = "REPLACE_WITH_YOUR_PASSWORD";

String hostname = "ESP32TEMP";

void initWiFi() {
  WiFi.setHostname(hostname.c_str()); //define hostname
  
  WiFi.mode(WIFI_STA);
  
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());
}

void setup() {
  Serial.begin(115200);
  initWiFi();
  Serial.print("RSSI: ");
  Serial.println(WiFi.RSSI());
  NBNS.begin(hostname.c_str());
  MDNS.begin(hostname.c_str());
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(1000);
  Serial.print(WiFi.getHostname());
  Serial.println(" Ok");
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.