EspMQTTClient icon indicating copy to clipboard operation
EspMQTTClient copied to clipboard

Fixed hostname on ESP32

Open 1Prototype1 opened this issue 7 months ago • 0 comments

This commit fixes the hostname not getting applied on ESP32 since WiFi.mode() was called before setting the hostname.

Explanation

Taking a look at the WiFi library, you can see setHostname method calls set_esp_netif_hostname which sets the default_hostname variable with the provided hostname. This variable is read only by get_esp_netif_hostname which gets called in mode and only when the mode is changed to WIFI_MODE_STA ref

TL;DR

  1. Call WiFi.setHostname() to set internal variable default_hostname with desired hostname.
  2. Call WiFi.mode() with parameter WIFI_STA which will apply the hostname.

Tested and working on ESP32 (Arduino-ESP32 v2.0.17) and no breaking changes found on ESP8266.

1Prototype1 avatar Jul 07 '24 18:07 1Prototype1