[GH-ISSUE #578] Webconfiguration - HOSTNAME - new feature request #481

Closed
opened 2026-02-28 01:25:31 +03:00 by kerem · 20 comments
Owner

Originally created by @sorriso93 on GitHub (Mar 28, 2018).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/578

Basic Infos

NEW FEATURE REQUEST
Hello in addition to webserver ip address & MQTT configuration it would be useful to add hostname, also to be saved as persistent information together with remaining network configuration

Originally created by @sorriso93 on GitHub (Mar 28, 2018). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/578 ### Basic Infos NEW FEATURE REQUEST Hello in addition to webserver ip address & MQTT configuration it would be useful to add hostname, also to be saved as persistent information together with remaining network configuration
kerem closed this issue 2026-02-28 01:25:31 +03:00
Author
Owner

@tablatronix commented on GitHub (Mar 28, 2018):

You can implement this now in development branch using
custom parameters, spiffs, and WiFiManager.setHostname()

<!-- gh-comment-id:376959884 --> @tablatronix commented on GitHub (Mar 28, 2018): You can implement this now in development branch using custom parameters, spiffs, and WiFiManager.setHostname()
Author
Owner

@sorriso93 commented on GitHub (Mar 29, 2018):

Thanks, I will try but I don't think to be capable of such implementation... Anyway, can you point me to similar addition, if any available on examples or dev branches?
Thank you in advance

<!-- gh-comment-id:377171896 --> @sorriso93 commented on GitHub (Mar 29, 2018): Thanks, I will try but I don't think to be capable of such implementation... Anyway, can you point me to similar addition, if any available on examples or dev branches? Thank you in advance
Author
Owner

@tablatronix commented on GitHub (Mar 29, 2018):

The fsparameters examples are pretty much already there

<!-- gh-comment-id:377205968 --> @tablatronix commented on GitHub (Mar 29, 2018): The fsparameters examples are pretty much already there
Author
Owner

@sorriso93 commented on GitHub (Apr 5, 2018):

Many thanks, copied each occurence of mqtt_server and added the same row with new string variable host_name.
Before starting WiFiManager in the example, I added:

wifi_station_set_hostname (host_name);
WiFi.hostname(host_name);

Then, important to have the host name set, in the initial section of the sketch

extern "C" { // needed to set hostname
#include "user_interface.h"
}

<!-- gh-comment-id:378920026 --> @sorriso93 commented on GitHub (Apr 5, 2018): Many thanks, copied each occurence of mqtt_server and added the same row with new string variable host_name. Before starting WiFiManager in the example, I added: wifi_station_set_hostname (host_name); WiFi.hostname(host_name); Then, important to have the host name set, in the initial section of the sketch extern "C" { // needed to set hostname #include "user_interface.h" }
Author
Owner

@tablatronix commented on GitHub (Apr 5, 2018):

hostname is already included in wifimanager as I mentioned above.

    // set a custom hostname, sets sta and ap dhcp client id for esp32, and sta for esp8266
    bool          setHostname(const char * hostname);
<!-- gh-comment-id:378928531 --> @tablatronix commented on GitHub (Apr 5, 2018): hostname is already included in wifimanager as I mentioned above. ```cpp // set a custom hostname, sets sta and ap dhcp client id for esp32, and sta for esp8266 bool setHostname(const char * hostname); ```
Author
Owner

@sorriso93 commented on GitHub (Apr 5, 2018):

Using the WiFiManager.setHostname didn't get any change on hostname...

<!-- gh-comment-id:378950922 --> @sorriso93 commented on GitHub (Apr 5, 2018): Using the WiFiManager.setHostname didn't get any change on hostname...
Author
Owner

@tablatronix commented on GitHub (Apr 5, 2018):

hmm, yeah that makes sense, it only sets hostname for wifimanager, so if not calling autoconnect, it does not get set.
are you not using autoconnect?

<!-- gh-comment-id:378956792 --> @tablatronix commented on GitHub (Apr 5, 2018): hmm, yeah that makes sense, it only sets hostname for wifimanager, so if not calling autoconnect, it does not get set. are you not using autoconnect?
Author
Owner

@sorriso93 commented on GitHub (Apr 5, 2018):

Yes I used autoconnect and the fsparameter example (I also integrated in my sketch the OTA part in another setup procedure).
After a lot of trial this is the code doing the job...

void setup_WIFIWEBSERVER ()
{
  Serial.begin(115200);
  Serial.println();
  //clean FS, for testing
  //SPIFFS.format();
  //read configuration from FS json
  #ifdef debug_serial
   Serial.println("mounting FS...");
  #endif
  if (SPIFFS.begin()) {
    #ifdef debug_serial
     Serial.println("mounted file system");
    #endif
    if (SPIFFS.exists("/config.json")) {
      //file exists, reading and loading
      #ifdef debug_serial
       Serial.println("reading config file");
      #endif
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        #ifdef debug_serial
         Serial.println("opened config file");
        #endif
        size_t size = configFile.size();
        // Allocate a buffer to store contents of the file.
        std::unique_ptr<char[]> buf(new char[size]);
        configFile.readBytes(buf.get(), size);
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        json.printTo(Serial);
        if (json.success()) {
          #ifdef debug_serial
           Serial.println("\nparsed json");
          #endif
          strcpy(host_name, json["host_name"]);
          strcpy(mqtt_server, json["mqtt_server"]);
          strcpy(mqtt_port, json["mqtt_port"]);
          strcpy(blynk_token, json["blynk_token"]);
          // set hostname
          wifi_station_set_hostname (host_name);
          WiFi.hostname(host_name);        
          if(json["ip"]) {
            #ifdef debug_serial
             Serial.println("setting custom ip from config");
            #endif
            //static_ip = json["ip"];
            strcpy(static_ip, json["ip"]);
            strcpy(static_gw, json["gateway"]);
            strcpy(static_sn, json["subnet"]);
            //strcat(static_ip, json["ip"]);
            //static_gw = json["gateway"];
            //static_sn = json["subnet"];
            #ifdef debug_serial
             Serial.println(static_ip);
            #endif
            //Serial.println("converting ip");
            //IPAddress ip = ipFromCharArray(static_ip);
            //Serial.println(ip);
          } else {
            #ifdef debug_serial
             Serial.println("no custom ip in config");
            #endif
          }
        } else {
          #ifdef debug_serial
           Serial.println("failed to load json config");
          #endif
        }
      }
    }
  } else {
    #ifdef debug_serial
     Serial.println("failed to mount FS");
    #endif
  }
  //end read
  #ifdef debug_serial
   Serial.println(host_name);
   Serial.println(static_ip);
   Serial.println(blynk_token);
   Serial.println(mqtt_server);
  #endif
  // The extra parameters to be configured (can be either global or just in the setup)
  // After connecting, parameter.getValue() will get you the configured value
  // id/name placeholder/prompt default length
  WiFiManagerParameter custom_host_name("hostname", "hostname", host_name, 15);
  WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
  WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 5);
  WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 34);
  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  wifi_station_set_hostname (host_name);
  WiFi.hostname(host_name);
  WiFiManager wifiManager;
  //set config save notify callback
  wifiManager.setSaveConfigCallback(saveConfigCallback);
  //set static ip
  #ifdef static_ip_yes
   IPAddress _ip,_gw,_sn;
   _ip.fromString(static_ip);
   _gw.fromString(static_gw);
   _sn.fromString(static_sn);
   wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn);
  #endif
  //add all your parameters here
  wifiManager.addParameter(&custom_host_name);
  wifiManager.addParameter(&custom_mqtt_server);
  wifiManager.addParameter(&custom_mqtt_port);
  wifiManager.addParameter(&custom_blynk_token);
  //reset settings - for testing
  //wifiManager.resetSettings();
  //set minimu quality of signal so it ignores AP's under that quality
  //defaults to 8%
  wifiManager.setMinimumSignalQuality();
  //sets timeout until configuration portal gets turned off
  //useful to make it all retry or go to sleep
  //in seconds
  //wifiManager.setTimeout(120);
  //fetches ssid and pass and tries to connect
  //if it does not connect it starts an access point with the specified name
  //here  "AutoConnectAP"
  //and goes into a blocking loop awaiting configuration
  #ifdef display_yes
   scrivi_display("Acc. Point",WiFi.hostname(),WiFi.localIP().toString());
  #endif
  if (!wifiManager.autoConnect(host_name, password_AP)) 
  {
    #ifdef debug_serial
     Serial.println(WiFi.hostname());
     Serial.println("failed to connect and hit timeout");
    #endif
    delay(3000);
    //reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(5000);
  }
  //if you get here you have connected to the WiFi
  #ifdef debug_serial
   Serial.println("connected...yeey :)");
  #endif

  #ifdef display_yes
   scrivi_display(WiFi.SSID(),WiFi.hostname(),WiFi.localIP().toString());
  #endif
  //read updated parameters
  strcpy(host_name, custom_host_name.getValue());
  strcpy(mqtt_server, custom_mqtt_server.getValue());
  strcpy(mqtt_port, custom_mqtt_port.getValue());
  strcpy(blynk_token, custom_blynk_token.getValue());
  //save the custom parameters to FS
  if (shouldSaveConfig) 
  {
    #ifdef debug_serial
     Serial.println("saving config");
    #endif
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["host_name"] = host_name;
    json["mqtt_server"] = mqtt_server;
    json["mqtt_port"] = mqtt_port;
    json["blynk_token"] = blynk_token;
    json["ip"] = WiFi.localIP().toString();
    json["gateway"] = WiFi.gatewayIP().toString();
    json["subnet"] = WiFi.subnetMask().toString();
    File configFile = SPIFFS.open("/config.json", "w");
    #ifdef debug_serial
     if (!configFile) 
     {
       Serial.println("failed to open config file for writing");
     }
    #endif
    json.prettyPrintTo(Serial);
    json.printTo(configFile);
    configFile.close();
    //end save
  }
  #ifdef debug_serial
   Serial.println("\n ----- ");
   Serial.println("hostname");
   Serial.println(WiFi.hostname());
   Serial.println("local ip");
   Serial.println(WiFi.localIP());
   Serial.println(WiFi.gatewayIP());
   Serial.println(WiFi.subnetMask());
   WiFi.printDiag(Serial);
  #endif

}
<!-- gh-comment-id:378960484 --> @sorriso93 commented on GitHub (Apr 5, 2018): Yes I used autoconnect and the fsparameter example (I also integrated in my sketch the OTA part in another setup procedure). After a lot of trial this is the code doing the job... ```cpp void setup_WIFIWEBSERVER () { Serial.begin(115200); Serial.println(); //clean FS, for testing //SPIFFS.format(); //read configuration from FS json #ifdef debug_serial Serial.println("mounting FS..."); #endif if (SPIFFS.begin()) { #ifdef debug_serial Serial.println("mounted file system"); #endif if (SPIFFS.exists("/config.json")) { //file exists, reading and loading #ifdef debug_serial Serial.println("reading config file"); #endif File configFile = SPIFFS.open("/config.json", "r"); if (configFile) { #ifdef debug_serial Serial.println("opened config file"); #endif size_t size = configFile.size(); // Allocate a buffer to store contents of the file. std::unique_ptr<char[]> buf(new char[size]); configFile.readBytes(buf.get(), size); DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.parseObject(buf.get()); json.printTo(Serial); if (json.success()) { #ifdef debug_serial Serial.println("\nparsed json"); #endif strcpy(host_name, json["host_name"]); strcpy(mqtt_server, json["mqtt_server"]); strcpy(mqtt_port, json["mqtt_port"]); strcpy(blynk_token, json["blynk_token"]); // set hostname wifi_station_set_hostname (host_name); WiFi.hostname(host_name); if(json["ip"]) { #ifdef debug_serial Serial.println("setting custom ip from config"); #endif //static_ip = json["ip"]; strcpy(static_ip, json["ip"]); strcpy(static_gw, json["gateway"]); strcpy(static_sn, json["subnet"]); //strcat(static_ip, json["ip"]); //static_gw = json["gateway"]; //static_sn = json["subnet"]; #ifdef debug_serial Serial.println(static_ip); #endif //Serial.println("converting ip"); //IPAddress ip = ipFromCharArray(static_ip); //Serial.println(ip); } else { #ifdef debug_serial Serial.println("no custom ip in config"); #endif } } else { #ifdef debug_serial Serial.println("failed to load json config"); #endif } } } } else { #ifdef debug_serial Serial.println("failed to mount FS"); #endif } //end read #ifdef debug_serial Serial.println(host_name); Serial.println(static_ip); Serial.println(blynk_token); Serial.println(mqtt_server); #endif // The extra parameters to be configured (can be either global or just in the setup) // After connecting, parameter.getValue() will get you the configured value // id/name placeholder/prompt default length WiFiManagerParameter custom_host_name("hostname", "hostname", host_name, 15); WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40); WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 5); WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 34); //WiFiManager //Local intialization. Once its business is done, there is no need to keep it around wifi_station_set_hostname (host_name); WiFi.hostname(host_name); WiFiManager wifiManager; //set config save notify callback wifiManager.setSaveConfigCallback(saveConfigCallback); //set static ip #ifdef static_ip_yes IPAddress _ip,_gw,_sn; _ip.fromString(static_ip); _gw.fromString(static_gw); _sn.fromString(static_sn); wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn); #endif //add all your parameters here wifiManager.addParameter(&custom_host_name); wifiManager.addParameter(&custom_mqtt_server); wifiManager.addParameter(&custom_mqtt_port); wifiManager.addParameter(&custom_blynk_token); //reset settings - for testing //wifiManager.resetSettings(); //set minimu quality of signal so it ignores AP's under that quality //defaults to 8% wifiManager.setMinimumSignalQuality(); //sets timeout until configuration portal gets turned off //useful to make it all retry or go to sleep //in seconds //wifiManager.setTimeout(120); //fetches ssid and pass and tries to connect //if it does not connect it starts an access point with the specified name //here "AutoConnectAP" //and goes into a blocking loop awaiting configuration #ifdef display_yes scrivi_display("Acc. Point",WiFi.hostname(),WiFi.localIP().toString()); #endif if (!wifiManager.autoConnect(host_name, password_AP)) { #ifdef debug_serial Serial.println(WiFi.hostname()); Serial.println("failed to connect and hit timeout"); #endif delay(3000); //reset and try again, or maybe put it to deep sleep ESP.reset(); delay(5000); } //if you get here you have connected to the WiFi #ifdef debug_serial Serial.println("connected...yeey :)"); #endif #ifdef display_yes scrivi_display(WiFi.SSID(),WiFi.hostname(),WiFi.localIP().toString()); #endif //read updated parameters strcpy(host_name, custom_host_name.getValue()); strcpy(mqtt_server, custom_mqtt_server.getValue()); strcpy(mqtt_port, custom_mqtt_port.getValue()); strcpy(blynk_token, custom_blynk_token.getValue()); //save the custom parameters to FS if (shouldSaveConfig) { #ifdef debug_serial Serial.println("saving config"); #endif DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.createObject(); json["host_name"] = host_name; json["mqtt_server"] = mqtt_server; json["mqtt_port"] = mqtt_port; json["blynk_token"] = blynk_token; json["ip"] = WiFi.localIP().toString(); json["gateway"] = WiFi.gatewayIP().toString(); json["subnet"] = WiFi.subnetMask().toString(); File configFile = SPIFFS.open("/config.json", "w"); #ifdef debug_serial if (!configFile) { Serial.println("failed to open config file for writing"); } #endif json.prettyPrintTo(Serial); json.printTo(configFile); configFile.close(); //end save } #ifdef debug_serial Serial.println("\n ----- "); Serial.println("hostname"); Serial.println(WiFi.hostname()); Serial.println("local ip"); Serial.println(WiFi.localIP()); Serial.println(WiFi.gatewayIP()); Serial.println(WiFi.subnetMask()); WiFi.printDiag(Serial); #endif } ```
Author
Owner

@tablatronix commented on GitHub (Apr 5, 2018):

so you tried

wifiManager.setHostname(host_name); // sethostname before autoconect
  if (!wifiManager.autoConnect(host_name, password_AP)) 
  {...

what did logs say ?

<!-- gh-comment-id:378962052 --> @tablatronix commented on GitHub (Apr 5, 2018): so you tried ```cpp wifiManager.setHostname(host_name); // sethostname before autoconect if (!wifiManager.autoConnect(host_name, password_AP)) {... ``` what did logs say ?
Author
Owner

@sorriso93 commented on GitHub (Apr 5, 2018):

I must admit I don't know how to see log... you mean log of the ESP8266 program execution I imagine...

<!-- gh-comment-id:378962739 --> @sorriso93 commented on GitHub (Apr 5, 2018): I must admit I don't know how to see log... you mean log of the ESP8266 program execution I imagine...
Author
Owner

@tablatronix commented on GitHub (Apr 5, 2018):

the serial output

<!-- gh-comment-id:378964473 --> @tablatronix commented on GitHub (Apr 5, 2018): the serial output
Author
Owner

@sorriso93 commented on GitHub (Apr 5, 2018):

Ah ok, printing WiFi.hostname() I did get ESP*** hostname, not what I set from the ap's web interface

<!-- gh-comment-id:378973581 --> @sorriso93 commented on GitHub (Apr 5, 2018): Ah ok, printing WiFi.hostname() I did get ESP*** hostname, not what I set from the ap's web interface
Author
Owner

@tablatronix commented on GitHub (Apr 5, 2018):

That is the default.

Paste the wm: debug logging if you can test again please, thanks

<!-- gh-comment-id:378975000 --> @tablatronix commented on GitHub (Apr 5, 2018): That is the default. Paste the wm: debug logging if you can test again please, thanks
Author
Owner

@tablatronix commented on GitHub (Jun 29, 2018):

is this still an issue?

<!-- gh-comment-id:401417606 --> @tablatronix commented on GitHub (Jun 29, 2018): is this still an issue?
Author
Owner

@DaleSchultz commented on GitHub (Apr 21, 2019):

using Wifimanage 0.14.0 on ESP8266 NodeMCU

WiFiManager wifiManager;
wifiManager.setHostname("abcd");

results in:

error: 'class WiFiManager' has no member named 'setHostname'
wifiManager.setHostname("abcd");

<!-- gh-comment-id:485252423 --> @DaleSchultz commented on GitHub (Apr 21, 2019): using Wifimanage 0.14.0 on ESP8266 NodeMCU ``` WiFiManager wifiManager; wifiManager.setHostname("abcd"); ``` results in: > error: 'class WiFiManager' has no member named 'setHostname' > wifiManager.setHostname("abcd");
Author
Owner

@tablatronix commented on GitHub (Apr 21, 2019):

Development branch!

<!-- gh-comment-id:485253498 --> @tablatronix commented on GitHub (Apr 21, 2019): Development branch!
Author
Owner

@DaleSchultz commented on GitHub (Apr 21, 2019):

ah duh!
There seems to be an underlying problem setting hostname() sometimes it works and sometimes not. I suspect it is not a WifiManager issue but numerous people are having the problem. I am giving up.
Thanks.

<!-- gh-comment-id:485254747 --> @DaleSchultz commented on GitHub (Apr 21, 2019): ah duh! There seems to be an underlying problem setting hostname() sometimes it works and sometimes not. I suspect it is not a WifiManager issue but numerous people are having the problem. I am giving up. Thanks.
Author
Owner

@tablatronix commented on GitHub (Apr 21, 2019):

do you have a reference to problems?

<!-- gh-comment-id:485255806 --> @tablatronix commented on GitHub (Apr 21, 2019): do you have a reference to problems?
Author
Owner

@DaleSchultz commented on GitHub (Apr 21, 2019):

there are many threads and issues on it, some of which get confused by looking at what the router sees and reports etc. and many are closed when some workaround is found.

https://github.com/esp8266/Arduino/issues/2826
https://www.esp8266.com/viewtopic.php?f=160&t=15590
https://www.esp8266.com/viewtopic.php?f=29&t=11124
https://www.esp8266.com/viewtopic.php?f=32&t=11306
https://forum.arduino.cc/index.php?topic=558635.0

On Friday this was working fine for me on multiple ESP8266 NodeMCU units:

WiFi.hostname("abcd");
Serial.println(WiFi.hostname()); 

Then suddenly it no longer works and new downloads produce an empty string. Note if the call to hostname() works, the reading it back with hostname(); should return the name, so there should be no need to restart routers or load DNS systems etc., those all just confuse and obscure the issue that WiFi.hostname("abcd"); is demonstratively failing on some occasions.

It is erratic, which probably explains why there are so many threads online complaining that it does not work, and it works for others. Some people also report it stopped working after it was working.

In wading through all these threads I came across this thread suggesting to use WiFiManager.setHostname() as a workaround. It should not be needed.

https://github.com/esp8266/Arduino/issues/2826
has a statement from d-a-v saying

WiFi.hostname is broken in 2.4.0 with lwip-v2. Please update to latest git or use lwip-v1.4.

but it also fails with LwIP 1.4 for me.

<!-- gh-comment-id:485259736 --> @DaleSchultz commented on GitHub (Apr 21, 2019): there are many threads and issues on it, some of which get confused by looking at what the router sees and reports etc. and many are closed when some workaround is found. https://github.com/esp8266/Arduino/issues/2826 https://www.esp8266.com/viewtopic.php?f=160&t=15590 https://www.esp8266.com/viewtopic.php?f=29&t=11124 https://www.esp8266.com/viewtopic.php?f=32&t=11306 https://forum.arduino.cc/index.php?topic=558635.0 On Friday this was working fine for me on multiple ESP8266 NodeMCU units: ``` WiFi.hostname("abcd"); Serial.println(WiFi.hostname()); ``` Then suddenly it no longer works and new downloads produce an empty string. Note if the call to hostname() works, the reading it back with hostname(); should return the name, so there should be no need to restart routers or load DNS systems etc., those all just confuse and obscure the issue that WiFi.hostname("abcd"); is demonstratively failing on some occasions. It is erratic, which probably explains why there are so many threads online complaining that it does not work, and it works for others. Some people also report it stopped working after it was working. In wading through all these threads I came across this thread suggesting to use WiFiManager.setHostname() as a workaround. It should not be needed. https://github.com/esp8266/Arduino/issues/2826 has a statement from d-a-v saying > WiFi.hostname is broken in 2.4.0 with lwip-v2. Please update to latest git or use lwip-v1.4. but it also fails with LwIP 1.4 for me.
Author
Owner

@tablatronix commented on GitHub (Apr 21, 2019):

Those threads are very old, dns was broken until like 2.4

<!-- gh-comment-id:485269785 --> @tablatronix commented on GitHub (Apr 21, 2019): Those threads are very old, dns was broken until like 2.4
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#481
No description provided.