From a1c8852b5398696118ed2b5ceacbe9bd63009c05 Mon Sep 17 00:00:00 2001 From: Mauro De Lucca Date: Sat, 27 Apr 2019 13:50:07 -0400 Subject: [PATCH 1/2] [ESP8266] Problem setting hostname while on dynamic ip. While using a dynamic ip the system doesn't take into account the hostname setting. Calling setting hostname after begin solved the issue. This only applies to esp8266, haven't tried on esp32. --- src/WiFiSettingsService.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/WiFiSettingsService.cpp b/src/WiFiSettingsService.cpp index 05292c88..0a26f68b 100644 --- a/src/WiFiSettingsService.cpp +++ b/src/WiFiSettingsService.cpp @@ -67,13 +67,15 @@ void WiFiSettingsService::reconfigureWiFiConnection() { } // connect to the network -#if defined(ESP8266) - WiFi.hostname(_hostname); -#elif defined(ESP_PLATFORM) +#if defined(ESP_PLATFORM) WiFi.setHostname(_hostname.c_str()); #endif WiFi.begin(_ssid.c_str(), _password.c_str()); + +#if defined(ESP8266) + WiFi.hostname(_hostname); +#endif } void WiFiSettingsService::readIP(JsonObject& root, String key, IPAddress& _ip){ From 2a65ed4207f95df75a75b74153fa9e53a217519d Mon Sep 17 00:00:00 2001 From: Mauro De Lucca Date: Sun, 28 Apr 2019 00:00:30 -0400 Subject: [PATCH 2/2] Solved hostname issue and static to dynamic issue Calling WiFi.config() with INADDR_ANY/INADDR_NONE to set dynamic ip / enable dhcp. This solution solved issue on setting hostname for esp8266 and esp32. Also it solved issues moving from static ip to dynamic ip. --- src/WiFiSettingsService.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/WiFiSettingsService.cpp b/src/WiFiSettingsService.cpp index 0a26f68b..a9c1296e 100644 --- a/src/WiFiSettingsService.cpp +++ b/src/WiFiSettingsService.cpp @@ -64,18 +64,18 @@ void WiFiSettingsService::reconfigureWiFiConnection() { // configure static ip config for station mode (if set) if (_staticIPConfig) { WiFi.config(_localIP, _gatewayIP, _subnetMask, _dnsIP1, _dnsIP2); + } else { // else setting dynamic ip config and hostname +#if defined(ESP8266) + WiFi.config(INADDR_ANY, INADDR_ANY, INADDR_ANY); + WiFi.hostname(_hostname); +#elif defined(ESP_PLATFORM) + WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE); + WiFi.setHostname(_hostname.c_str()); +#endif } // connect to the network -#if defined(ESP_PLATFORM) - WiFi.setHostname(_hostname.c_str()); -#endif - WiFi.begin(_ssid.c_str(), _password.c_str()); - -#if defined(ESP8266) - WiFi.hostname(_hostname); -#endif } void WiFiSettingsService::readIP(JsonObject& root, String key, IPAddress& _ip){