From 08d3ab8754a314f8072114a433013585c7c3c08c Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Fri, 8 Oct 2021 20:12:01 +0800 Subject: [PATCH] json 6 version --- data_esp/config.json | 2 +- include/Consts.h | 5 +++-- src/Utils/JsonUtils.cpp | 38 +++++++++++++++++++++++++++----------- src/main.cpp | 14 ++++++++++++-- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/data_esp/config.json b/data_esp/config.json index d8a730c6..9d038a2d 100644 --- a/data_esp/config.json +++ b/data_esp/config.json @@ -26,7 +26,7 @@ "webpass": "admin", "MqttIn": "0", "MqttOut": "0", - "blink": "0", + "blink": "1", "oneWirePin": "2", "serverip": "http://206.189.49.244", "uart": "0", diff --git a/include/Consts.h b/include/Consts.h index 3ca3b744..e7c9780f 100644 --- a/include/Consts.h +++ b/include/Consts.h @@ -46,7 +46,7 @@ #define MYSENSORS #endif -#define JSON_BUFFER_SIZE 1024 +#define JSON_BUFFER_SIZE 4096 #define NUM_BUTTONS 6 #define MQTT_RECONNECT_INTERVAL 20000 #define CHANGE_BROKER_AFTER 5 @@ -104,7 +104,8 @@ enum TimerTask_t { WIFI_SCAN, UPTIME, UDP, SYGNAL, - TIMES }; + TIMES, + MYTEST }; enum NotAsyncActions { do_ZERO, diff --git a/src/Utils/JsonUtils.cpp b/src/Utils/JsonUtils.cpp index 6c0167d6..feb3681b 100644 --- a/src/Utils/JsonUtils.cpp +++ b/src/Utils/JsonUtils.cpp @@ -64,47 +64,63 @@ String jsonReadStr(String& json, String name) { DynamicJsonDocument doc(JSON_BUFFER_SIZE); - deserializeJson(doc, json); + DeserializationError error = deserializeJson(doc, json); + if (error) SerialPrint("E", F("jsonRead"), error.f_str()); return doc[name].as(); } boolean jsonReadBool(String& json, String name) { DynamicJsonDocument doc(JSON_BUFFER_SIZE); - deserializeJson(doc, json); + DeserializationError error = deserializeJson(doc, json); + if (error) SerialPrint("E", F("jsonRead"), error.f_str()); return doc[name].as(); } int jsonReadInt(String& json, String name) { DynamicJsonDocument doc(JSON_BUFFER_SIZE); - deserializeJson(doc, json); + DeserializationError error = deserializeJson(doc, json); + if (error) SerialPrint("E", F("jsonRead"), error.f_str()); return doc[name].as(); } +//================================================================================= String jsonWriteStr(String& json, String name, String value) { - DynamicJsonDocument doc(JSON_BUFFER_SIZE); - deserializeJson(doc, json); + DynamicJsonDocument doc(json.length()); + DeserializationError error = deserializeJson(doc, json); + if (error) SerialPrint("E", F("jsonWrite"), error.f_str()); doc[name] = value; + json = ""; + serializeJson(doc, json); return json; } String jsonWriteBool(String& json, String name, boolean value) { - DynamicJsonDocument doc(JSON_BUFFER_SIZE); - deserializeJson(doc, json); + DynamicJsonDocument doc(json.length()); + DeserializationError error = deserializeJson(doc, json); + if (error) SerialPrint("E", F("jsonWrite"), error.f_str()); doc[name] = value; + json = ""; + serializeJson(doc, json); return json; } String jsonWriteInt(String& json, String name, int value) { - DynamicJsonDocument doc(JSON_BUFFER_SIZE); - deserializeJson(doc, json); + DynamicJsonDocument doc(json.length()); + DeserializationError error = deserializeJson(doc, json); + if (error) SerialPrint("E", F("jsonWrite"), error.f_str()); doc[name] = value; + json = ""; + serializeJson(doc, json); return json; } String jsonWriteFloat(String& json, String name, float value) { - DynamicJsonDocument doc(JSON_BUFFER_SIZE); - deserializeJson(doc, json); + DynamicJsonDocument doc(json.length()); + DeserializationError error = deserializeJson(doc, json); + if (error) SerialPrint("E", F("jsonWrite"), error.f_str()); doc[name] = value; + json = ""; + serializeJson(doc, json); return json; } diff --git a/src/main.cpp b/src/main.cpp index 158435c2..cb04031f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,7 @@ #include "Global.h" #include "Init.h" #include "ItemsList.h" +#include "MySensorsDataParse.h" #include "RemoteOrdersUdp.h" #include "SoftUART.h" #include "Telegram.h" @@ -18,7 +19,6 @@ #include "Utils/StatUtils.h" #include "Utils/Timings.h" #include "Utils/WebUtils.h" -#include "MySensorsDataParse.h" #include "items/ButtonInClass.h" #include "items/vCountDown.h" #include "items/vImpulsOut.h" @@ -29,10 +29,10 @@ #include "items/vSensorCcs811.h" #include "items/vSensorDallas.h" #include "items/vSensorDht.h" +#include "items/vSensorNode.h" #include "items/vSensorPzem.h" #include "items/vSensorUltrasonic.h" #include "items/vSensorUptime.h" -#include "items/vSensorNode.h" void not_async_actions(); @@ -83,6 +83,16 @@ void setup() { just_load = false; initialized = true; + + const int capacity = JSON_OBJECT_SIZE(35); + SerialPrint("I", F("Test"), String(capacity)); + + ts.add( + MYTEST, 10000, [&](void*) { + Serial.println(configSetupJson.length()); + }, + nullptr, true); + SerialPrint("I", F("System"), F("✔ Initialization completed")); }