From c524f04d1f90beecce2ea1d09f68c30a558e6131 Mon Sep 17 00:00:00 2001 From: Florian <1technophile@users.noreply.github.com> Date: Thu, 2 Jan 2020 22:30:20 +0100 Subject: [PATCH] Replace fixed allocation of json buffer (#520) * Replace fixed allocation of json buffer by a dynamic one, and simplify trace function of json objects * differentiate the change depending on board memory size to make it works with UNO --- main/main.ino | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/main/main.ino b/main/main.ino index c09e8b8..a71c799 100644 --- a/main/main.ino +++ b/main/main.ino @@ -288,9 +288,7 @@ void trc(float msg) void trc(JsonObject &data) { - char JSONmessageBuffer[JSON_MSG_BUFFER]; - data.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer)); - trc(JSONmessageBuffer); + data.printTo(Serial); } void pub(char *topicori, char *payload, bool retainFlag) @@ -314,7 +312,11 @@ void pub(char *topicori, JsonObject &data) #endif #ifdef jsonPublishing + #if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__) + char JSONmessageBuffer[data.measureLength() + 1]; + #else char JSONmessageBuffer[JSON_MSG_BUFFER]; + #endif trc(F("Pub json into:")); trc(topic); data.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer)); @@ -389,7 +391,11 @@ void pub_custom_topic(char *topicori, JsonObject &data, boolean retain) { if (client.connected()) { + #if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__) + char JSONmessageBuffer[data.measureLength() + 1]; + #else char JSONmessageBuffer[JSON_MSG_BUFFER]; + #endif trc(F("Pub json discovery into:")); trc(topicori); data.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));