From 120c0b5ca2159e82daa43ff8b1671098931f7d14 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Thu, 31 Aug 2023 08:09:54 +0200 Subject: [PATCH] update espMqttClient, add own mqtt limit, fix queue display, 3.7.0-dev1d --- lib/espMqttClient/src/Config.h | 4 ++-- lib/espMqttClient/src/Helpers.h | 3 +-- lib/espMqttClient/src/MqttClient.h | 2 +- src/mqtt.cpp | 14 ++++++++++++-- src/version.h | 2 +- 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/espMqttClient/src/Config.h b/lib/espMqttClient/src/Config.h index 3c3e5cc36..940c2dea8 100644 --- a/lib/espMqttClient/src/Config.h +++ b/lib/espMqttClient/src/Config.h @@ -29,7 +29,7 @@ the LICENSE file. #endif #ifndef EMC_MIN_FREE_MEMORY -#define EMC_MIN_FREE_MEMORY 61440 +#define EMC_MIN_FREE_MEMORY 16384 #endif #ifndef EMC_ESP8266_MULTITHREADING @@ -37,7 +37,7 @@ the LICENSE file. #endif #ifndef EMC_ALLOW_NOT_CONNECTED_PUBLISH -#define EMC_ALLOW_NOT_CONNECTED_PUBLISH 0 +#define EMC_ALLOW_NOT_CONNECTED_PUBLISH 1 #endif #ifndef EMC_WAIT_FOR_CONNACK diff --git a/lib/espMqttClient/src/Helpers.h b/lib/espMqttClient/src/Helpers.h index ca12b8052..05ab136d9 100644 --- a/lib/espMqttClient/src/Helpers.h +++ b/lib/espMqttClient/src/Helpers.h @@ -15,8 +15,7 @@ the LICENSE file. #include "esp_task_wdt.h" #define EMC_SEMAPHORE_TAKE() xSemaphoreTake(_xSemaphore, portMAX_DELAY) #define EMC_SEMAPHORE_GIVE() xSemaphoreGive(_xSemaphore) - // #define EMC_GET_FREE_MEMORY() std::max(ESP.getMaxAllocHeap(), ESP.getMaxAllocPsram()) - #define EMC_GET_FREE_MEMORY() ESP.getFreeHeap() + #define EMC_GET_FREE_MEMORY() std::max(ESP.getMaxAllocHeap(), ESP.getMaxAllocPsram()) #define EMC_YIELD() vTaskDelay(1) #define EMC_GENERATE_CLIENTID(x) snprintf(x, EMC_CLIENTID_LENGTH, "esp32%06llx", ESP.getEfuseMac()); #elif defined(ARDUINO_ARCH_ESP8266) diff --git a/lib/espMqttClient/src/MqttClient.h b/lib/espMqttClient/src/MqttClient.h index d09db68c5..a357c5779 100644 --- a/lib/espMqttClient/src/MqttClient.h +++ b/lib/espMqttClient/src/MqttClient.h @@ -131,7 +131,7 @@ class MqttClient { uint32_t timeSent; espMqttClientInternals::Packet packet; template - OutgoingPacket(uint32_t t, espMqttClientTypes::Error error, Args &&... args) + OutgoingPacket(uint32_t t, espMqttClientTypes::Error & error, Args &&... args) : timeSent(t) , packet(error, std::forward(args)...) { } diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 5c294e01a..e0ddda2c7 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -124,6 +124,8 @@ void Mqtt::resubscribe() { // Main MQTT loop - sends out top item on publish queue void Mqtt::loop() { + queuecount_ = mqttClient_->getQueue(); + // exit if MQTT is not enabled or if there is no network connection if (!connected()) { return; @@ -142,7 +144,7 @@ void Mqtt::loop() { EMSESP::publish_sensor_values(false); } - queuecount_ = mqttClient_->getQueue(); + // wait for empty queue before sending scheduled device messages if (queuecount_ > 0) { return; } @@ -482,7 +484,7 @@ void Mqtt::on_connect() { connecting_ = true; connectcount_++; // count # reconnects. not currently used. - queuecount_ = 0; + queuecount_ = mqttClient_->getQueue(); load_settings(); // reload MQTT settings - in case they have changes @@ -590,6 +592,14 @@ bool Mqtt::queue_message(const uint8_t operation, const std::string & topic, con if (!mqtt_enabled_ || topic.empty()) { return false; // quit, not using MQTT } + // check free mem + if (ESP.getFreeHeap() < 60 * 1204) { + if (operation == Operation::PUBLISH) { + mqtt_message_id_++; + mqtt_publish_fails_++; + } + return false; // quit, not using MQTT + } uint16_t packet_id = 0; char fulltopic[MQTT_TOPIC_MAX_SIZE]; diff --git a/src/version.h b/src/version.h index b6cc182cf..69d734367 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define EMSESP_APP_VERSION "3.7.0-dev.1" +#define EMSESP_APP_VERSION "3.7.0-dev.1d"