From 6c6174fdb2edd8313f4c3eb65ffe4a4a00995dc7 Mon Sep 17 00:00:00 2001 From: technyon Date: Tue, 30 Jan 2024 14:26:17 +0100 Subject: [PATCH] implement feature to disable wifi / wifi config portal fallback --- Config.h | 2 +- Network.cpp | 8 ++++++++ PreferencesKeys.h | 1 + RestartReason.h | 3 +++ WebCfgServer.cpp | 6 ++++++ main.cpp | 2 ++ networkDevices/WifiDevice.cpp | 2 ++ networkDevices/WifiDevice.h | 1 + 8 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Config.h b/Config.h index c5e7a79..8740e0a 100644 --- a/Config.h +++ b/Config.h @@ -1,6 +1,6 @@ #pragma once -#define NUKI_HUB_VERSION "8.30" +#define NUKI_HUB_VERSION "8.31-pre-1" #define MQTT_QOS_LEVEL 1 #define MQTT_CLEAN_SESSIONS false diff --git a/Network.cpp b/Network.cpp index e342506..6224498 100644 --- a/Network.cpp +++ b/Network.cpp @@ -65,6 +65,14 @@ void Network::setupDevice() if(strcmp(WiFi_fallbackDetect, "wifi_fallback") == 0) { + if(_preferences->getBool(preference_network_wifi_fallback_disabled)) + { + Log->println(F("Failed to connect to network. Wifi fallback is disable, rebooting.")); + memset(WiFi_fallbackDetect, 0, sizeof(WiFi_fallbackDetect)); + sleep(5); + restartEsp(RestartReason::NetworkDeviceCriticalFailureNoWifiFallback); + } + Log->println(F("Switching to WiFi device as fallback.")); _networkDeviceType = NetworkDeviceType::WiFi; } diff --git a/PreferencesKeys.h b/PreferencesKeys.h index 9bd0c94..1fc1188 100644 --- a/PreferencesKeys.h +++ b/PreferencesKeys.h @@ -27,6 +27,7 @@ #define preference_ip_dns_server "dnssrv" #define preference_network_hardware "nwhw" #define preference_network_hardware_gpio "nwhwdt" // obsolete +#define preference_network_wifi_fallback_disabled "nwwififb" #define preference_rssi_publish_interval "rssipb" #define preference_hostname "hostname" #define preference_network_timeout "nettmout" diff --git a/RestartReason.h b/RestartReason.h index 9871c3f..aacb5aa 100644 --- a/RestartReason.h +++ b/RestartReason.h @@ -11,6 +11,7 @@ enum class RestartReason ReconfigureWifi, ReconfigureLAN8720, NetworkDeviceCriticalFailure, + NetworkDeviceCriticalFailureNoWifiFallback, ConfigurationUpdated, GpioConfigurationUpdated, RestartTimer, @@ -81,6 +82,8 @@ inline static String getRestartReason() return "ReconfigureLAN8720"; case RestartReason::NetworkDeviceCriticalFailure: return "NetworkDeviceCriticalFailure"; + case RestartReason::NetworkDeviceCriticalFailureNoWifiFallback: + return "NetworkDeviceCriticalFailureNoWifiFallback"; case RestartReason::ConfigurationUpdated: return "ConfigurationUpdated"; case RestartReason::GpioConfigurationUpdated: diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index 25202a3..b65aef9 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -325,6 +325,11 @@ bool WebCfgServer::processArgs(String& message) _preferences->putInt(preference_network_hardware, value.toInt()); configChanged = true; } + else if(key == "NWHWWIFIFB") + { + _preferences->putBool(preference_network_wifi_fallback_disabled, (value == "1")); + configChanged = true; + } else if(key == "RSSI") { _preferences->putInt(preference_rssi_publish_interval, value.toInt()); @@ -763,6 +768,7 @@ void WebCfgServer::buildMqttConfigHtml(String &response) printTextarea(response, "MQTTCRT", "MQTT SSL Client Certificate (*, optional)", _preferences->getString(preference_mqtt_crt).c_str(), TLS_CERT_MAX_SIZE, _network->encryptionSupported(), true); printTextarea(response, "MQTTKEY", "MQTT SSL Client Key (*, optional)", _preferences->getString(preference_mqtt_key).c_str(), TLS_KEY_MAX_SIZE, _network->encryptionSupported(), true); printDropDown(response, "NWHW", "Network hardware", String(_preferences->getInt(preference_network_hardware)), getNetworkDetectionOptions()); + printCheckBox(response, "NWHWWIFIFB", "Disable fallback to Wifi / WiFi config portal", _preferences->getBool(preference_network_wifi_fallback_disabled)); printInputField(response, "RSSI", "RSSI Publish interval (seconds; -1 to disable)", _preferences->getInt(preference_rssi_publish_interval), 6); printInputField(response, "NETTIMEOUT", "Network Timeout until restart (seconds; -1 to disable)", _preferences->getInt(preference_network_timeout), 5); printCheckBox(response, "RSTDISC", "Restart on disconnect", _preferences->getBool(preference_restart_on_disconnect)); diff --git a/main.cpp b/main.cpp index ac712f2..b9362d9 100644 --- a/main.cpp +++ b/main.cpp @@ -138,6 +138,8 @@ bool initPreferences() preferences = new Preferences(); preferences->begin("nukihub", false); +// preferences->putBool(preference_network_wifi_fallback_disabled, false); + bool firstStart = !preferences->getBool(preference_started_before); if(firstStart) diff --git a/networkDevices/WifiDevice.cpp b/networkDevices/WifiDevice.cpp index 9720eaf..ced1a72 100644 --- a/networkDevices/WifiDevice.cpp +++ b/networkDevices/WifiDevice.cpp @@ -10,6 +10,7 @@ RTC_NOINIT_ATTR char WiFiDevice_reconfdetect[17]; WifiDevice::WifiDevice(const String& hostname, Preferences* preferences, const IPConfiguration* ipConfiguration) : NetworkDevice(hostname, ipConfiguration), + _preferences(preferences), _wm(preferences->getString(preference_cred_user).c_str(), preferences->getString(preference_cred_password).c_str()) { _startAp = strcmp(WiFiDevice_reconfdetect, "reconfigure_wifi") == 0; @@ -64,6 +65,7 @@ void WifiDevice::initialize() std::vector wm_menu; wm_menu.push_back("wifi"); wm_menu.push_back("exit"); + _wm.setEnableConfigPortal(_startAp || !_preferences->getBool(preference_network_wifi_fallback_disabled)); // reduced tieout if ESP is set to restart on disconnect _wm.setConfigPortalTimeout(_restartOnDisconnect ? 60 * 3 : 60 * 30); _wm.setShowInfoUpdate(false); diff --git a/networkDevices/WifiDevice.h b/networkDevices/WifiDevice.h index 8413b43..d45b30e 100644 --- a/networkDevices/WifiDevice.h +++ b/networkDevices/WifiDevice.h @@ -65,6 +65,7 @@ private: WiFiManager _wm; espMqttClient* _mqttClient = nullptr; espMqttClientSecure* _mqttClientSecure = nullptr; + Preferences* _preferences = nullptr; bool _restartOnDisconnect = false; bool _startAp = false;