From 0712ab51524cfa5103951f34a1e26332053f4064 Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 17 Feb 2020 11:13:41 +0100 Subject: [PATCH] check for max sensors - #327 --- src/ems-esp.cpp | 12 +++++++++--- src/my_config.h | 3 ++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/ems-esp.cpp b/src/ems-esp.cpp index 68a57ae77..b324aea35 100644 --- a/src/ems-esp.cpp +++ b/src/ems-esp.cpp @@ -19,6 +19,7 @@ // Dallas external temp sensors #include "ds18.h" DS18 ds18; +#define DS18_MQTT_PAYLOAD_MAXSIZE 400 // public libraries #include // https://github.com/bblanchon/ArduinoJson @@ -565,8 +566,13 @@ void publishSensorValues() { return; // no sensors attached } - StaticJsonDocument<400> doc; - JsonObject sensors = doc.to(); + // each payload per sensor is 30 bytes so calculate if we have enough space + if ((EMSESP_Settings.dallas_sensors * 30) > DS18_MQTT_PAYLOAD_MAXSIZE) { + myDebug("Error: too many Dallas sensors for MQTT payload"); + } + + StaticJsonDocument doc; + JsonObject sensors = doc.to(); bool hasdata = false; char buffer[128] = {0}; @@ -583,7 +589,7 @@ void publishSensorValues() { return; // nothing to send } - char data[400] = {0}; + char data[DS18_MQTT_PAYLOAD_MAXSIZE] = {0}; serializeJson(doc, data, sizeof(data)); myDebugLog("Publishing external sensor data via MQTT"); myESP.mqttPublish(TOPIC_EXTERNAL_SENSORS, data); diff --git a/src/my_config.h b/src/my_config.h index d59c0a537..c55414c16 100644 --- a/src/my_config.h +++ b/src/my_config.h @@ -10,8 +10,9 @@ #include "ems.h" -// TOPICS with _CMD_ are used for receiving commands from an MQTT Broker +// TOPICS with *_CMD_* are used for receiving commands from an MQTT Broker // EMS-ESP will subscribe to these topics + #define TOPIC_GENERIC_CMD "generic_cmd" // for receiving generic system commands via MQTT // MQTT for thermostat