Reconnect network on MQTT failure (#435)
This commit is contained in:
@@ -237,6 +237,8 @@ bool NukiNetwork::update()
|
||||
void NukiNetwork::initialize()
|
||||
{
|
||||
_restartOnDisconnect = _preferences->getBool(preference_restart_on_disconnect, false);
|
||||
_checkUpdates = _preferences->getBool(preference_check_updates, false);
|
||||
_reconnectNetworkOnMqttDisconnect = _preferences->getBool(preference_recon_netw_on_mqtt_discon, false);
|
||||
_rssiPublishInterval = _preferences->getInt(preference_rssi_publish_interval) * 1000;
|
||||
|
||||
_hostname = _preferences->getString(preference_hostname);
|
||||
@@ -358,8 +360,10 @@ bool NukiNetwork::update()
|
||||
return true;
|
||||
}
|
||||
|
||||
if(!_device->isConnected())
|
||||
if(!_device->isConnected() || (_mqttConnectCounter > 15 && _reconnectNetworkOnMqttDisconnect && !_firstConnect))
|
||||
{
|
||||
_mqttConnectCounter = 0;
|
||||
|
||||
if(_firstDisconnected) {
|
||||
_firstDisconnected = false;
|
||||
_device->mqttDisconnect(true);
|
||||
@@ -371,7 +375,7 @@ bool NukiNetwork::update()
|
||||
}
|
||||
|
||||
Log->println(F("Network not connected. Trying reconnect."));
|
||||
ReconnectStatus reconnectStatus = _device->reconnect();
|
||||
ReconnectStatus reconnectStatus = _device->reconnect(true);
|
||||
|
||||
switch(reconnectStatus)
|
||||
{
|
||||
@@ -405,8 +409,11 @@ bool NukiNetwork::update()
|
||||
bool success = reconnect();
|
||||
if(!success)
|
||||
{
|
||||
delay(2000);
|
||||
_mqttConnectCounter++;
|
||||
return false;
|
||||
}
|
||||
_mqttConnectCounter = 0;
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
@@ -471,7 +478,7 @@ bool NukiNetwork::update()
|
||||
_lastMaintenanceTs = ts;
|
||||
}
|
||||
|
||||
if(_preferences->getBool(preference_check_updates))
|
||||
if(_checkUpdates)
|
||||
{
|
||||
if(_lastUpdateCheckTs == 0 || (ts - _lastUpdateCheckTs) > 86400000)
|
||||
{
|
||||
|
||||
@@ -159,6 +159,7 @@ private:
|
||||
Gpio* _gpio;
|
||||
|
||||
int _mqttConnectionState = 0;
|
||||
int _mqttConnectCounter = 0;
|
||||
long _mqttConnectedTs = -1;
|
||||
bool _connectReplyReceived = false;
|
||||
bool _firstDisconnected = true;
|
||||
@@ -172,6 +173,8 @@ private:
|
||||
int _networkTimeout = 0;
|
||||
std::vector<MqttReceiver*> _mqttReceivers;
|
||||
bool _restartOnDisconnect = false;
|
||||
bool _checkUpdates = false;
|
||||
bool _reconnectNetworkOnMqttDisconnect = false;
|
||||
bool _firstConnect = true;
|
||||
bool _publishDebugInfo = false;
|
||||
bool _logIp = true;
|
||||
|
||||
@@ -97,6 +97,7 @@
|
||||
#define preference_ota_updater_url (char*)"otaUpdUrl"
|
||||
#define preference_update_from_mqtt (char*)"updMqtt"
|
||||
#define preference_show_secrets (char*)"showSecr"
|
||||
#define preference_recon_netw_on_mqtt_discon (char*)"recNtwMqttDis"
|
||||
|
||||
inline bool initPreferences(Preferences* preferences)
|
||||
{
|
||||
@@ -248,7 +249,8 @@ private:
|
||||
preference_cred_password, preference_disable_non_json, preference_publish_authdata, preference_publish_debug_info, preference_presence_detection_timeout,
|
||||
preference_official_hybrid, preference_query_interval_hybrid_lockstate, preference_official_hybrid_actions, preference_official_hybrid_retry, preference_has_mac_saved,
|
||||
preference_has_mac_byte_0, preference_has_mac_byte_1, preference_has_mac_byte_2, preference_latest_version, preference_task_size_network, preference_task_size_nuki,
|
||||
preference_authlog_max_entries, preference_keypad_max_entries, preference_timecontrol_max_entries, preference_update_from_mqtt, preference_show_secrets
|
||||
preference_authlog_max_entries, preference_keypad_max_entries, preference_timecontrol_max_entries, preference_update_from_mqtt, preference_show_secrets,
|
||||
preference_recon_netw_on_mqtt_discon
|
||||
};
|
||||
std::vector<char*> _redact =
|
||||
{
|
||||
@@ -262,7 +264,8 @@ private:
|
||||
preference_restart_on_disconnect, preference_keypad_control_enabled, preference_keypad_info_enabled, preference_keypad_publish_code, preference_show_secrets,
|
||||
preference_timecontrol_control_enabled, preference_timecontrol_info_enabled, preference_register_as_app, preference_register_opener_as_app, preference_ip_dhcp_enabled,
|
||||
preference_publish_authdata, preference_has_mac_saved, preference_publish_debug_info, preference_network_wifi_fallback_disabled, preference_official_hybrid,
|
||||
preference_official_hybrid_actions, preference_official_hybrid_retry, preference_conf_info_enabled, preference_disable_non_json, preference_update_from_mqtt
|
||||
preference_official_hybrid_actions, preference_official_hybrid_retry, preference_conf_info_enabled, preference_disable_non_json, preference_update_from_mqtt,
|
||||
preference_recon_netw_on_mqtt_discon
|
||||
};
|
||||
std::vector<char*> _bytePrefs =
|
||||
{
|
||||
|
||||
@@ -1013,6 +1013,11 @@ bool WebCfgServer::processArgs(String& message)
|
||||
_preferences->putBool(preference_restart_on_disconnect, (value == "1"));
|
||||
configChanged = true;
|
||||
}
|
||||
else if(key == "RECNWTMQTTDIS")
|
||||
{
|
||||
_preferences->putBool(preference_recon_netw_on_mqtt_discon, (value == "1"));
|
||||
configChanged = true;
|
||||
}
|
||||
else if(key == "MQTTLOG")
|
||||
{
|
||||
_preferences->putBool(preference_mqtt_log_enabled, (value == "1"));
|
||||
@@ -2159,6 +2164,7 @@ void WebCfgServer::buildMqttConfigHtml(String &response)
|
||||
printInputField(response, "RSSI", "RSSI Publish interval (seconds; -1 to disable)", _preferences->getInt(preference_rssi_publish_interval), 6, "");
|
||||
printInputField(response, "NETTIMEOUT", "MQTT 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), "");
|
||||
printCheckBox(response, "RECNWTMQTTDIS", "Reconnect network on MQTT connection failure", _preferences->getBool(preference_recon_netw_on_mqtt_discon), "");
|
||||
printCheckBox(response, "MQTTLOG", "Enable MQTT logging", _preferences->getBool(preference_mqtt_log_enabled), "");
|
||||
printCheckBox(response, "CHECKUPDATE", "Check for Firmware Updates every 24h", _preferences->getBool(preference_check_updates), "");
|
||||
printCheckBox(response, "UPDATEMQTT", "Allow updating using MQTT", _preferences->getBool(preference_update_from_mqtt), "");
|
||||
|
||||
@@ -143,7 +143,7 @@ bool EthLan8720Device::isConnected()
|
||||
return ETH.linkUp();
|
||||
}
|
||||
|
||||
ReconnectStatus EthLan8720Device::reconnect()
|
||||
ReconnectStatus EthLan8720Device::reconnect(bool force)
|
||||
{
|
||||
if(!_hardwareInitialized)
|
||||
{
|
||||
|
||||
@@ -55,7 +55,7 @@ public:
|
||||
|
||||
virtual void initialize();
|
||||
virtual void reconfigure();
|
||||
virtual ReconnectStatus reconnect();
|
||||
virtual ReconnectStatus reconnect(bool force = false);
|
||||
bool supportsEncryption() override;
|
||||
|
||||
virtual bool isConnected();
|
||||
|
||||
@@ -24,7 +24,7 @@ public:
|
||||
virtual const String deviceName() const = 0;
|
||||
|
||||
virtual void initialize() = 0;
|
||||
virtual ReconnectStatus reconnect() = 0;
|
||||
virtual ReconnectStatus reconnect(bool force = false) = 0;
|
||||
virtual void reconfigure() = 0;
|
||||
virtual void printError();
|
||||
virtual bool supportsEncryption() = 0;
|
||||
|
||||
@@ -79,7 +79,7 @@ void W5500Device::initialize()
|
||||
reconnect();
|
||||
}
|
||||
|
||||
ReconnectStatus W5500Device::reconnect()
|
||||
ReconnectStatus W5500Device::reconnect(bool force)
|
||||
{
|
||||
_hasDHCPAddress = false;
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ public:
|
||||
const String deviceName() const override;
|
||||
|
||||
virtual void initialize();
|
||||
virtual ReconnectStatus reconnect();
|
||||
virtual ReconnectStatus reconnect(bool force = false);
|
||||
virtual void reconfigure();
|
||||
|
||||
bool supportsEncryption() override;
|
||||
|
||||
@@ -72,6 +72,7 @@ void WifiDevice::initialize()
|
||||
_wm.setEnableConfigPortal(_startAp || !_preferences->getBool(preference_network_wifi_fallback_disabled));
|
||||
// reduced timeout if ESP is set to restart on disconnect
|
||||
_wm.setFindBestRSSI(_preferences->getBool(preference_find_best_rssi));
|
||||
_wm.setConnectTimeout(5);
|
||||
_wm.setConfigPortalTimeout(_restartOnDisconnect ? 60 * 3 : 60 * 30);
|
||||
_wm.setShowInfoUpdate(false);
|
||||
_wm.setMenu(wm_menu);
|
||||
@@ -141,32 +142,25 @@ bool WifiDevice::isConnected()
|
||||
return WiFi.isConnected();
|
||||
}
|
||||
|
||||
ReconnectStatus WifiDevice::reconnect()
|
||||
ReconnectStatus WifiDevice::reconnect(bool force)
|
||||
{
|
||||
if(!isConnected() && !_isReconnecting)
|
||||
if((!isConnected() || force) && !_isReconnecting)
|
||||
{
|
||||
_isReconnecting = true;
|
||||
WiFi.disconnect();
|
||||
delay(1000);
|
||||
if(!_preferences->getBool(preference_find_best_rssi, false)) WiFi.reconnect();
|
||||
else
|
||||
int loop = 0;
|
||||
|
||||
while(isConnected() && loop <20)
|
||||
{
|
||||
if(WiFi.getMode() & WIFI_STA){
|
||||
WiFi.mode(WIFI_OFF);
|
||||
int timeout = (esp_timer_get_time() / 1000)+1200;
|
||||
while(WiFi.getMode()!= WIFI_OFF && (esp_timer_get_time() / 1000)<timeout){
|
||||
delay(0);
|
||||
}
|
||||
}
|
||||
delay(5000);
|
||||
_wm.WiFi_scanNetworks(true, false);
|
||||
delay(5000);
|
||||
_wm.wifiConnectDefault();
|
||||
delay(100);
|
||||
loop++;
|
||||
}
|
||||
delay(10000);
|
||||
|
||||
_wm.resetScan();
|
||||
_wm.autoConnect();
|
||||
_isReconnecting = false;
|
||||
}
|
||||
|
||||
|
||||
if(!isConnected() && _disconnectTs > (esp_timer_get_time() / 1000) - 120000) _wm.setEnableConfigPortal(_startAp || !_preferences->getBool(preference_network_wifi_fallback_disabled));
|
||||
return isConnected() ? ReconnectStatus::Success : ReconnectStatus::Failure;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public:
|
||||
|
||||
virtual void initialize();
|
||||
virtual void reconfigure();
|
||||
virtual ReconnectStatus reconnect();
|
||||
virtual ReconnectStatus reconnect(bool force = false);
|
||||
bool supportsEncryption() override;
|
||||
|
||||
virtual bool isConnected();
|
||||
|
||||
Reference in New Issue
Block a user