diff --git a/PreferencesKeys.h b/PreferencesKeys.h index a471121..aa3d331 100644 --- a/PreferencesKeys.h +++ b/PreferencesKeys.h @@ -16,6 +16,7 @@ #define preference_mqtt_hass_discovery "hassdiscovery" #define preference_hostname "hostname" #define preference_network_timeout "nettmout" +#define preference_restart_on_disconnect "restdisc" #define preference_query_interval_lockstate "lockStInterval" #define preference_query_interval_battery "batInterval" #define preference_cred_user "crdusr" diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index ea90aad..7aaf868 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -249,6 +249,11 @@ bool WebCfgServer::processArgs(String& message) _preferences->putInt(preference_network_timeout, value.toInt()); configChanged = true; } + else if(key == "RSTDISC") + { + _preferences->putBool(preference_restart_on_disconnect, (value == "1")); + configChanged = true; + } else if(key == "LSTINT") { _preferences->putInt(preference_query_interval_lockstate, value.toInt()); @@ -509,6 +514,7 @@ void WebCfgServer::buildMqttConfigHtml(String &response) printTextarea(response, "MQTTKEY", "MQTT SSL Client Key (*, optional)", _preferences->getString(preference_mqtt_key).c_str(), TLS_KEY_MAX_SIZE); printInputField(response, "HASSDISCOVERY", "Home Assistant discovery topic (empty to disable)", _preferences->getString(preference_mqtt_hass_discovery).c_str(), 30); printInputField(response, "NETTIMEOUT", "NetworkLock 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)); response.concat(""); response.concat("* If no encryption is configured for the MQTT broker, leave empty.
"); diff --git a/lib/WiFiManager/WiFiManager.cpp b/lib/WiFiManager/WiFiManager.cpp index 1c250ed..e9e839b 100644 --- a/lib/WiFiManager/WiFiManager.cpp +++ b/lib/WiFiManager/WiFiManager.cpp @@ -2705,6 +2705,15 @@ void WiFiManager::setPreOtaUpdateCallback( std::function func ) { _preotaupdatecallback = func; } +/** + * setDisconnectedCallback, set a callback to fire when WiFi is disconnected + * @access public + * @param {[type]} void (*func)(void) + */ +void WiFiManager::setDisconnectedCallback( std::function func ) { + _disconnectedcallback = func; +} + /** * set custom head html * custom element will be added to head, eg. new style tag etc. @@ -3611,6 +3620,12 @@ String WiFiManager::WiFi_psk(bool persistent) const { // DEBUG_WM(DEBUG_VERBOSE,"[EVENT]",event); #endif if(event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED){ + + if(_disconnectedcallback != nullptr) + { + _disconnectedcallback(); + } + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("[EVENT] WIFI_REASON: "),info.wifi_sta_disconnected.reason); #endif diff --git a/lib/WiFiManager/WiFiManager.h b/lib/WiFiManager/WiFiManager.h index 6579307..26c61d1 100644 --- a/lib/WiFiManager/WiFiManager.h +++ b/lib/WiFiManager/WiFiManager.h @@ -253,6 +253,9 @@ class WiFiManager //called just before doing OTA update void setPreOtaUpdateCallback( std::function func ); + //called when WiFi has disconnected + void setDisconnectedCallback( std::function func ); + //sets timeout before AP,webserver loop ends and exits even if there has been no setup. //useful for devices that failed to connect at some point and got stuck in a webserver loop //in seconds setConfigPortalTimeout is a new name for setTimeout, ! not used if setConfigPortalBlocking @@ -720,6 +723,7 @@ class WiFiManager std::function _saveparamscallback; std::function _resetcallback; std::function _preotaupdatecallback; + std::function _disconnectedcallback = nullptr; template auto optionalIPFromString(T *obj, const char *s) -> decltype( obj->fromString(s) ) { diff --git a/networkDevices/WifiDevice.cpp b/networkDevices/WifiDevice.cpp index 68c8cee..7d292c7 100644 --- a/networkDevices/WifiDevice.cpp +++ b/networkDevices/WifiDevice.cpp @@ -5,6 +5,8 @@ WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences) : NetworkDevice(hostname) { + _restartOnDisconnect = _preferences->getBool(preference_restart_on_disconnect); + size_t caLength = _preferences->getString(preference_mqtt_ca,_ca,TLS_CA_MAX_SIZE); size_t crtLength = _preferences->getString(preference_mqtt_crt,_cert,TLS_CERT_MAX_SIZE); size_t keyLength = _preferences->getString(preference_mqtt_key,_key,TLS_KEY_MAX_SIZE); @@ -69,6 +71,14 @@ void WifiDevice::initialize() Serial.println(WiFi.localIP().toString()); } + if(_restartOnDisconnect) + { + _wm.setDisconnectedCallback([&]() + { + onDisconnected(); + }); + } + _mqttClient->setBufferSize(_mqttMaxBufferSize); } @@ -106,3 +116,11 @@ void WifiDevice::update() { } + +void WifiDevice::onDisconnected() +{ + if(millis() > 60000) + { + ESP.restart(); + } +} diff --git a/networkDevices/WifiDevice.h b/networkDevices/WifiDevice.h index 322fc85..5d1c4ea 100644 --- a/networkDevices/WifiDevice.h +++ b/networkDevices/WifiDevice.h @@ -24,11 +24,14 @@ public: virtual PubSubClient *mqttClient(); private: + void onDisconnected(); + WiFiManager _wm; WiFiClient* _wifiClient = nullptr; WiFiClientSecure* _wifiClientSecure = nullptr; PubSubClient* _mqttClient = nullptr; SpiffsCookie _cookie; + bool _restartOnDisconnect = false; char _ca[TLS_CA_MAX_SIZE]; char _cert[TLS_CERT_MAX_SIZE];