From e58929dfc0310866481a2dcc9e37e4f70247c1e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Tue, 5 Dec 2017 15:47:27 +0100 Subject: [PATCH] Limit size of the MQTT queue to 10 messages when useJson is enabled --- code/espurna/config/general.h | 2 ++ code/espurna/mqtt.ino | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 48644f5b..912a7ff0 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -419,6 +419,8 @@ PROGMEM const char* const custom_reset_string[] = { #define MQTT_USE_JSON 0 // Group messages in a JSON body #define MQTT_USE_JSON_DELAY 100 // Wait this many ms before grouping messages +#define MQTT_QUEUE_MAX_SIZE 10 // Size of the MQTT queue when MQTT_USE_JSON is enabled + // These particles will be concatenated to the MQTT_TOPIC base to form the actual topic #define MQTT_TOPIC_JSON "data" diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index bec70f2b..d6e69b5f 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -140,11 +140,15 @@ void _mqttFlush() { void mqttSend(const char * topic, const char * message, bool force) { bool useJson = force ? false : _mqtt_use_json; if (useJson) { + + if (_mqtt_queue.size() >= MQTT_QUEUE_MAX_SIZE) _mqttFlush(); + mqtt_message_t element; element.topic = strdup(topic); element.message = strdup(message); _mqtt_queue.push_back(element); _mqtt_flush_ticker.once_ms(MQTT_USE_JSON_DELAY, _mqttFlush); + } else { String path = _mqtt_topic + String(topic) + _mqtt_getter; mqttSendRaw(path.c_str(), message);