diff --git a/src/mqtt.cpp b/src/mqtt.cpp index f3e2901ef..4965be618 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -594,12 +594,20 @@ bool Mqtt::queue_message(const uint8_t operation, const std::string & topic, con } // check free mem #ifndef EMSESP_STANDALONE - if (ESP.getFreeHeap() < 60 * 1204) { + if (ESP.getFreeHeap() < 60 * 1204 || ESP.getMaxAllocHeap() < 40 * 1024) { if (operation == Operation::PUBLISH) { mqtt_message_id_++; mqtt_publish_fails_++; } - LOG_DEBUG("%s failed: low memory", operation == Operation::PUBLISH ? "Publish" : operation == Operation::SUBSCRIBE ? "Subscribe" : "Unsubscribe"); + LOG_WARNING("%s failed: low memory", operation == Operation::PUBLISH ? "Publish" : operation == Operation::SUBSCRIBE ? "Subscribe" : "Unsubscribe"); + return false; // quit + } + if (queuecount_ >= MQTT_QUEUE_MAX_SIZE) { + if (operation == Operation::PUBLISH) { + mqtt_message_id_++; + mqtt_publish_fails_++; + } + LOG_WARNING("%s failed: queue full", operation == Operation::PUBLISH ? "Publish" : operation == Operation::SUBSCRIBE ? "Subscribe" : "Unsubscribe"); return false; // quit } #endif diff --git a/src/mqtt.h b/src/mqtt.h index 0048da7fb..d02fff23c 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -55,7 +55,8 @@ class Mqtt { enum Operation : uint8_t { PUBLISH, SUBSCRIBE, UNSUBSCRIBE }; enum NestedFormat : uint8_t { NESTED = 1, SINGLE }; - static constexpr uint8_t MQTT_TOPIC_MAX_SIZE = 128; // fixed, not a user setting anymore + static constexpr uint8_t MQTT_TOPIC_MAX_SIZE = 128; // fixed, not a user setting anymore + static constexpr uint16_t MQTT_QUEUE_MAX_SIZE = 300; static void on_connect(); static void on_disconnect(espMqttClientTypes::DisconnectReason reason);