add option to restart ESP when disconnected
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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("</table>");
|
||||
response.concat("* If no encryption is configured for the MQTT broker, leave empty.<br>");
|
||||
|
||||
|
||||
@@ -2705,6 +2705,15 @@ void WiFiManager::setPreOtaUpdateCallback( std::function<void()> 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<void()> 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
|
||||
|
||||
@@ -253,6 +253,9 @@ class WiFiManager
|
||||
//called just before doing OTA update
|
||||
void setPreOtaUpdateCallback( std::function<void()> func );
|
||||
|
||||
//called when WiFi has disconnected
|
||||
void setDisconnectedCallback( std::function<void()> 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<void()> _saveparamscallback;
|
||||
std::function<void()> _resetcallback;
|
||||
std::function<void()> _preotaupdatecallback;
|
||||
std::function<void()> _disconnectedcallback = nullptr;
|
||||
|
||||
template <class T>
|
||||
auto optionalIPFromString(T *obj, const char *s) -> decltype( obj->fromString(s) ) {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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];
|
||||
|
||||
Reference in New Issue
Block a user