diff --git a/src/ESP32React/MqttSettingsService.cpp b/src/ESP32React/MqttSettingsService.cpp index 5514af3b4..395f5a421 100644 --- a/src/ESP32React/MqttSettingsService.cpp +++ b/src/ESP32React/MqttSettingsService.cpp @@ -23,6 +23,7 @@ static String generateClientId() { MqttSettingsService::~MqttSettingsService() { delete _mqttClient; + _mqttClient = nullptr; } void MqttSettingsService::begin() { @@ -34,7 +35,7 @@ void MqttSettingsService::startClient() { static bool isSecure = false; if (_mqttClient != nullptr) { // do we need to change the client? - if ((isSecure && _state.enableTLS) || (!isSecure && !_state.enableTLS)) { + if (_state.enabled && ((isSecure && _state.enableTLS) || (!isSecure && !_state.enableTLS))) { return; } delete _mqttClient; @@ -43,10 +44,10 @@ void MqttSettingsService::startClient() { #ifndef TASMOTA_SDK if (_state.enableTLS) { isSecure = true; - if (emsesp::EMSESP::system_.PSram() > 0) { - _mqttClient = new espMqttClientSecure(EMSESP_MQTT_PRIORITY, EMSESP_MQTT_RUNNING_CORE); - } else { + if (emsesp::EMSESP::system_.PSram() == 0) { _mqttClient = new espMqttClientSecure(espMqttClientTypes::UseInternalTask::NO); + } else { + _mqttClient = new espMqttClientSecure(EMSESP_MQTT_PRIORITY, EMSESP_MQTT_RUNNING_CORE); } if (!_mqttClient) { emsesp::EMSESP::logger().warning("MQTT Client alloc failed"); @@ -63,10 +64,10 @@ void MqttSettingsService::startClient() { } #endif isSecure = false; - if (emsesp::EMSESP::system_.PSram() > 0) { - _mqttClient = new espMqttClient(EMSESP_MQTT_PRIORITY, EMSESP_MQTT_RUNNING_CORE); - } else { + if (emsesp::EMSESP::system_.PSram() == 0) { _mqttClient = new espMqttClient(espMqttClientTypes::UseInternalTask::NO); + } else { + _mqttClient = new espMqttClient(EMSESP_MQTT_PRIORITY, EMSESP_MQTT_RUNNING_CORE); } static_cast(_mqttClient)->onConnect([this](bool sessionPresent) { onMqttConnect(sessionPresent); }); static_cast(_mqttClient)->onDisconnect([this](espMqttClientTypes::DisconnectReason reason) { onMqttDisconnect(reason); }); @@ -78,6 +79,9 @@ void MqttSettingsService::startClient() { } void MqttSettingsService::loop() { + if (!_state.enabled || _mqttClient == nullptr || emsesp::EMSESP::system_.systemStatus() != 0) { + return; + } if (_reconfigureMqtt || (_disconnectedAt && static_cast(uuid::get_uptime() - _disconnectedAt) >= MQTT_RECONNECTION_DELAY)) { // reconfigure MQTT client _disconnectedAt = configureMqtt() ? 0 : uuid::get_uptime(); @@ -93,11 +97,11 @@ bool MqttSettingsService::isEnabled() { } bool MqttSettingsService::isConnected() { - return _mqttClient->connected(); + return _mqttClient ? _mqttClient->connected() : false; } const char * MqttSettingsService::getClientId() { - return _mqttClient->getClientId(); + return _mqttClient ? _mqttClient->getClientId() : ""; } void MqttSettingsService::onMqttMessage(const espMqttClientTypes::MessageProperties & properties, diff --git a/src/ESP32React/NTPSettingsService.cpp b/src/ESP32React/NTPSettingsService.cpp index 337e1649e..81e5cdf82 100644 --- a/src/ESP32React/NTPSettingsService.cpp +++ b/src/ESP32React/NTPSettingsService.cpp @@ -25,7 +25,7 @@ void NTPSettingsService::WiFiEvent(WiFiEvent_t event) { switch (event) { case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: case ARDUINO_EVENT_ETH_DISCONNECTED: - if (_connected) { + if (_connected && emsesp::EMSESP::system_.ntp_connected()) { emsesp::EMSESP::logger().info("WiFi connection dropped, stopping NTP"); _connected = false; configureNTP(); diff --git a/src/core/default_settings.h b/src/core/default_settings.h index 5a32df6af..47f991256 100644 --- a/src/core/default_settings.h +++ b/src/core/default_settings.h @@ -293,7 +293,7 @@ enum { #ifndef STRINGIZE #define STRINGIZE(s) #s #endif -#if TASMOTA_SDK +#ifdef TASMOTA_SDK #define ARDUINO_VERSION_STR(major, minor, patch) "Tasmota Arduino v" STRINGIZE(major) "." STRINGIZE(minor) "." STRINGIZE(patch) #else #define ARDUINO_VERSION_STR(major, minor, patch) "ESP32 Arduino v" STRINGIZE(major) "." STRINGIZE(minor) "." STRINGIZE(patch)