From 75e3d03ea4025ac77140d4c63a7909c1ff6302a2 Mon Sep 17 00:00:00 2001 From: Fribur Date: Tue, 16 Jan 2024 19:32:02 -0500 Subject: [PATCH] Revert back to using FirebaseJson instead of ArduinoJson There is no convenient way to access arrays of Json objects and nested keys such as testarray/[2]/myvalue using ArduinoJson. It would at the very least to split the path into a number of individual strings and then access the value like json[testarray][2][myvalue] More details in https://arduinojson.org/v6/doc/deserialization/ . Conclusion: too complicated. --- platformio.ini | 1 + src/HttpPowerMeter.cpp | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/platformio.ini b/platformio.ini index 57bc9a3c..3bd1645f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -47,6 +47,7 @@ lib_deps = https://github.com/coryjfowler/MCP_CAN_lib plerup/EspSoftwareSerial @ ^8.0.1 https://github.com/dok-net/ghostl @ ^1.0.1 + mobizt/FirebaseJson @ ^3.0.6 rweather/Crypto@^0.4.0 extra_scripts = diff --git a/src/HttpPowerMeter.cpp b/src/HttpPowerMeter.cpp index 826abd77..4d6c12f4 100644 --- a/src/HttpPowerMeter.cpp +++ b/src/HttpPowerMeter.cpp @@ -3,7 +3,7 @@ #include "HttpPowerMeter.h" #include "MessageOutput.h" #include -#include +#include #include #include #include @@ -204,13 +204,13 @@ bool HttpPowerMeterClass::tryGetFloatValueForPhase(int phase, int httpCode, cons bool success = false; if (httpCode == HTTP_CODE_OK) { httpResponse = httpClient.getString(); //very unfortunate that we cannot parse WifiClient stream directly - StaticJsonDocument<2048> json; //however creating these allocations on stack should be fine to avoid heap fragmentation - deserializeJson(json, httpResponse); - if(!json.containsKey(jsonPath)) - { + FirebaseJson json; + json.setJsonData(httpResponse); + FirebaseJsonData value; + if (!json.get(value, jsonPath)) { snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("[HttpPowerMeter] Couldn't find a value for phase %i with Json query \"%s\""), phase, jsonPath); }else { - power[phase] = json[jsonPath].as(); + power[phase] = value.to(); //MessageOutput.printf("Power for Phase %i: %5.2fW\r\n", phase, power[phase]); success = true; }