fix MQTT when only on Ethernet

This commit is contained in:
proddy
2021-03-28 21:34:40 +02:00
parent a920e89ea2
commit 494827299c
2 changed files with 29 additions and 47 deletions

View File

@@ -2,6 +2,8 @@
#include "../../src/emsesp_stub.hpp" // proddy added #include "../../src/emsesp_stub.hpp" // proddy added
using namespace std::placeholders; // for `_1` etc
/** /**
* Retains a copy of the cstr provided in the pointer provided using dynamic allocation. * Retains a copy of the cstr provided in the pointer provided using dynamic allocation.
* *
@@ -33,10 +35,9 @@ MqttSettingsService::MqttSettingsService(AsyncWebServer * server, FS * fs, Secur
, _disconnectedAt(0) , _disconnectedAt(0)
, _disconnectReason(AsyncMqttClientDisconnectReason::TCP_DISCONNECTED) , _disconnectReason(AsyncMqttClientDisconnectReason::TCP_DISCONNECTED)
, _mqttClient() { , _mqttClient() {
WiFi.onEvent(std::bind(&MqttSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED); WiFi.onEvent(std::bind(&MqttSettingsService::WiFiEvent, this, _1, _2));
WiFi.onEvent(std::bind(&MqttSettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP); _mqttClient.onConnect(std::bind(&MqttSettingsService::onMqttConnect, this, _1));
_mqttClient.onConnect(std::bind(&MqttSettingsService::onMqttConnect, this, std::placeholders::_1)); _mqttClient.onDisconnect(std::bind(&MqttSettingsService::onMqttDisconnect, this, _1));
_mqttClient.onDisconnect(std::bind(&MqttSettingsService::onMqttDisconnect, this, std::placeholders::_1));
addUpdateHandler([&](const String & originId) { onConfigUpdated(); }, false); addUpdateHandler([&](const String & originId) { onConfigUpdated(); }, false);
} }
@@ -79,17 +80,11 @@ AsyncMqttClient * MqttSettingsService::getMqttClient() {
} }
void MqttSettingsService::onMqttConnect(bool sessionPresent) { void MqttSettingsService::onMqttConnect(bool sessionPresent) {
// Serial.print(F("Connected to MQTT, ")); // emsesp::EMSESP::logger().info(F("Connected to MQTT, %s"), (sessionPresent) ? F("with persistent session") : F("without persistent session"));
// if (sessionPresent) {
// Serial.println(F("with persistent session"));
// } else {
// Serial.println(F("without persistent session"));
// }
} }
void MqttSettingsService::onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { void MqttSettingsService::onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
// Serial.print(F("Disconnected from MQTT reason: ")); // emsesp::EMSESP::logger().info(F("Disconnected from MQTT reason: %s"), (uint8_t)reason);
// Serial.println((uint8_t)reason);
_disconnectReason = reason; _disconnectReason = reason;
_disconnectedAt = uuid::get_uptime(); _disconnectedAt = uuid::get_uptime();
} }
@@ -99,46 +94,38 @@ void MqttSettingsService::onConfigUpdated() {
_disconnectedAt = 0; _disconnectedAt = 0;
// added by proddy // added by proddy
// reload EMS-ESP MQTT settings emsesp::EMSESP::mqtt_.start(); // reload EMS-ESP MQTT settings
emsesp::EMSESP::mqtt_.start();
} }
#ifdef ESP32 void MqttSettingsService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
void MqttSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) { switch (event) {
case SYSTEM_EVENT_STA_GOT_IP:
case SYSTEM_EVENT_ETH_GOT_IP:
if (_state.enabled) { if (_state.enabled) {
// Serial.println(F("WiFi connection dropped, starting MQTT client.")); // emsesp::EMSESP::logger().info(F("Network connection found, starting MQTT client"));
onConfigUpdated(); onConfigUpdated();
} }
} break;
void MqttSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { case SYSTEM_EVENT_STA_DISCONNECTED:
case SYSTEM_EVENT_ETH_DISCONNECTED:
if (_state.enabled) { if (_state.enabled) {
// Serial.println(F("WiFi connection dropped, stopping MQTT client.")); // emsesp::EMSESP::logger().info(F("Network connection dropped, stopping MQTT client"));
onConfigUpdated(); onConfigUpdated();
} }
} break;
#elif defined(ESP8266)
void MqttSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP & event) {
if (_state.enabled) {
// Serial.println(F("WiFi connection dropped, starting MQTT client."));
onConfigUpdated();
}
}
void MqttSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected & event) { default:
if (_state.enabled) { break;
// Serial.println(F("WiFi connection dropped, stopping MQTT client."));
onConfigUpdated();
} }
} }
#endif
void MqttSettingsService::configureMqtt() { void MqttSettingsService::configureMqtt() {
// disconnect if currently connected // disconnect if currently connected
_mqttClient.disconnect(); _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 && WiFi.isConnected()) { if (_state.enabled && (WiFi.isConnected() || ETH.linkUp())) {
// Serial.println(F("Connecting to MQTT...")); // Serial.println(F("Connecting to MQTT..."));
_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) {
@@ -153,7 +140,7 @@ void MqttSettingsService::configureMqtt() {
_mqttClient.connect(); _mqttClient.connect();
} }
emsesp::EMSESP::dallassensor_.reload(); emsesp::EMSESP::dallassensor_.reload(); // added by Proddy for EMS-ESP
} }
void MqttSettings::read(MqttSettings & settings, JsonObject & root) { void MqttSettings::read(MqttSettings & settings, JsonObject & root) {

View File

@@ -41,11 +41,7 @@
#ifndef FACTORY_MQTT_CLIENT_ID #ifndef FACTORY_MQTT_CLIENT_ID
#define FACTORY_MQTT_CLIENT_ID generateClientId() #define FACTORY_MQTT_CLIENT_ID generateClientId()
static String generateClientId() { static String generateClientId() {
#ifdef ESP32
return ESPUtils::defaultDeviceValue("esp32-"); return ESPUtils::defaultDeviceValue("esp32-");
#elif defined(ESP8266)
return ESPUtils::defaultDeviceValue("esp8266-");
#endif
} }
#endif #endif
@@ -148,8 +144,7 @@ class MqttSettingsService : public StatefulService<MqttSettings> {
// the MQTT client instance // the MQTT client instance
AsyncMqttClient _mqttClient; AsyncMqttClient _mqttClient;
void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info); void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info);
void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
void onMqttConnect(bool sessionPresent); void onMqttConnect(bool sessionPresent);
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason); void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
void configureMqtt(); void configureMqtt();