From d1507ae6f2d7dbca2734b8852a873e225519b2c2 Mon Sep 17 00:00:00 2001
From: Eric Duminil <eric.duminil@gmail.com>
Date: Mon, 21 Feb 2022 22:15:20 +0100
Subject: [PATCH] Setting Hostname (more) reliably

---
 ampel-firmware/src/lib/IotWebConf/src/IotWebConf.cpp | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/ampel-firmware/src/lib/IotWebConf/src/IotWebConf.cpp b/ampel-firmware/src/lib/IotWebConf/src/IotWebConf.cpp
index cacbf07..7ade73d 100644
--- a/ampel-firmware/src/lib/IotWebConf/src/IotWebConf.cpp
+++ b/ampel-firmware/src/lib/IotWebConf/src/IotWebConf.cpp
@@ -93,13 +93,7 @@ bool IotWebConf::init()
   this->_apTimeoutMs = atoi(this->_apTimeoutStr) * 1000;
 
   // -- Setup mdns
-#ifdef ESP8266
-  WiFi.hostname(this->_thingName);
-#elif defined(ESP32)
-  WiFi.setHostname(this->_thingName);
-#endif
 #ifdef IOTWEBCONF_CONFIG_USE_MDNS
-  //TODO: What is _thingName set to at this point??
   MDNS.begin(this->_thingName);
   MDNS.addService("http", "tcp", IOTWEBCONF_CONFIG_USE_MDNS);
 #endif
@@ -729,6 +723,9 @@ void IotWebConf::stateChanged(NetworkState oldState, NetworkState newState)
 #endif
       this->_wifiConnectionStart = millis();
       WiFi.mode(WIFI_STA);
+       // Hostname must be set right before WiFi.begin in order to work reliably,
+       // and will be set only once WiFi.begin has been called.
+      WiFi.setHostname(this->_thingName);
       this->_wifiConnectionHandler(
           this->_wifiAuthInfo.ssid, this->_wifiAuthInfo.password);
       break;
-- 
GitLab