fix#99, mqtt reconnect for IPv4 and IPv6

This commit is contained in:
MichaelDvP
2021-08-19 15:27:41 +02:00
parent 165dd3b418
commit 74cdb610d8
2 changed files with 17 additions and 4 deletions

View File

@@ -101,18 +101,25 @@ void MqttSettingsService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
switch (event) { switch (event) {
case SYSTEM_EVENT_STA_GOT_IP: case SYSTEM_EVENT_STA_GOT_IP:
case SYSTEM_EVENT_ETH_GOT_IP: case SYSTEM_EVENT_ETH_GOT_IP:
emsesp::EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) {
if (!networkSettings.enableIPv6 && _state.enabled) {
// emsesp::EMSESP::logger().info(F("IPv4 Network connection found, starting MQTT client"));
onConfigUpdated();
}
});
break;
case SYSTEM_EVENT_GOT_IP6: case SYSTEM_EVENT_GOT_IP6:
if (_state.enabled) { if (_state.enabled) {
// emsesp::EMSESP::logger().info(F("Network connection found, starting MQTT client")); // emsesp::EMSESP::logger().info(F("IPv6 Network connection found, starting MQTT client"));
onConfigUpdated(); onConfigUpdated();
} }
break; break;
case SYSTEM_EVENT_STA_DISCONNECTED: case SYSTEM_EVENT_STA_DISCONNECTED:
case SYSTEM_EVENT_ETH_DISCONNECTED: case SYSTEM_EVENT_ETH_DISCONNECTED:
if (_state.enabled) { if (_state.enabled) {
// emsesp::EMSESP::logger().info(F("Network connection dropped, stopping MQTT client")); // emsesp::EMSESP::logger().info(F("Network connection dropped, stopping MQTT client"));
onConfigUpdated(); _mqttClient.disconnect();
// onConfigUpdated();
} }
break; break;
@@ -122,9 +129,11 @@ void MqttSettingsService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
} }
void MqttSettingsService::configureMqtt() { void MqttSettingsService::configureMqtt() {
// disconnect if connected
_mqttClient.disconnect();
// only connect if WiFi is connected and MQTT is enabled // only connect if WiFi is connected and MQTT is enabled
if (_state.enabled && emsesp::EMSESP::system_.network_connected()) { if (_state.enabled && emsesp::EMSESP::system_.network_connected()) {
_mqttClient.disconnect(); // emsesp::EMSESP::logger().info(F("Configuring Mqtt client"));
_mqttClient.setServer(retainCstr(_state.host.c_str(), &_retainedHost), _state.port); _mqttClient.setServer(retainCstr(_state.host.c_str(), &_retainedHost), _state.port);
if (_state.username.length() > 0) { if (_state.username.length() > 0) {
_mqttClient.setCredentials(retainCstr(_state.username.c_str(), &_retainedUsername), _mqttClient.setCredentials(retainCstr(_state.username.c_str(), &_retainedUsername),
@@ -137,6 +146,8 @@ void MqttSettingsService::configureMqtt() {
_mqttClient.setCleanSession(_state.cleanSession); _mqttClient.setCleanSession(_state.cleanSession);
_mqttClient.setMaxTopicLength(_state.maxTopicLength); _mqttClient.setMaxTopicLength(_state.maxTopicLength);
_mqttClient.connect(); _mqttClient.connect();
// } else {
// emsesp::EMSESP::logger().info(F("Error configuring Mqtt client"));
} }
} }

View File

@@ -23,6 +23,7 @@
#include "dallassensor.h" #include "dallassensor.h"
#include "version.h" #include "version.h"
#include "default_settings.h" #include "default_settings.h"
#include <ESP8266React.h>
#include <uuid/log.h> #include <uuid/log.h>
@@ -36,6 +37,7 @@ class EMSESP {
static DallasSensor dallassensor_; static DallasSensor dallassensor_;
static uuid::log::Logger logger(); static uuid::log::Logger logger();
static ESP8266React esp8266React;
}; };
} // namespace emsesp } // namespace emsesp