I'm working on a arduino mini/leonardo project to set up a USB connected Leonardo on a pi to monitor it's power source (a solar charged battery pack).
One of the things I want to do is have running averages set up for each reading so I can see the average for the last minute/hour/day. I planned on having the values passed back using a json string over serial so I added ArduinoJson to the project. I have a line monitoring the voltage of the battery, the current being drawn from the battery, the voltage of the solar and the current coming in on the solar.
I've already worked out all of the read code and related math, and I've already built and tested my running averages code. Now I'm trying to clean it up and refactor and one thing that occurred to me was that I already had what amount to arrays in the ArduinoJson object so rather than creating a temporary array for each to house the averages only to transfer them back and forth to the json object for writing over the serial, I was trying to figure out how to just pass a reference to the json 'branch' itself.
Each 'branch' has 5 keys: "val", "sAv", "mAv", "hAv", "dAv" for value (the last value read), sAv (average since the top of the minute), mAv (average since the top of the hour), and so on
So for example, I set up the objects at the top with:
#include <ArduinoJson.h>
StaticJsonBuffer<256> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
JsonObject& battery = root.createNestedObject("bat");
JsonObject& batCur = battery.createNestedObject("amps");
JsonObject& batVol = battery.createNestedObject("volts");
JsonObject& solar = root.createNestedObject("sol");
JsonObject& solCur = solar.createNestedObject("amps");
JsonObject& solVol = battery.createNestedObject("volts");
Then initialize them with a single read in setup with:
void setup() {
float temp;
// battery current setup
batCur["pin"] = BATAPIN;
bat[LAST] = readAmps(BATAPIN);
temp = readAmps(BATAPIN); // read again to set initial values
batCur["val"].set(temp, 4);
batCur["sAv"].set(temp, 4);
batCur["mAv"].set(temp, 4);
batCur["hAv"].set(temp, 4);
batCur["dAv"].set(temp, 4);
// solar current setup
solCur["pin"] = SOLAPIN;
sol[LAST] = readAmps(SOLAPIN);
temp = readAmps(SOLAPIN); // read again to set initial values
solCur["val"].set(temp, 4);
solCur["sAv"].set(temp, 4);
solCur["mAv"].set(temp, 4);
solCur["hAv"].set(temp, 4);
solCur["dAv"].set(temp, 4);
// etc. with the other two
}
But regardless how I've tried to figure out how C++ passes things like that I can't seem to get it to work getting any number of errors on invalid types when trying to reference or assign the various keys. (I've tried even passing the root keys in but can't figure out the syntax for using .get() and .set() when accessing the branches like root[branchName][twigName] where for example I pass in "bat" for branchName and "volts" for the twigName)
A basic example of my function call attempt (trying to figure out the syntax for the referencing) looks like:
aveVals(&solCur);
// for function declaration:
void aveVals(JsonObject* jObj) {
}
Doing it that way gives me 'invalid types 'ArduinoJson::JsonObject*[const char [4]]' for array subscript' when I try to do something like:
jObj["dAv"].set(jObj.get("hAv"), 4);
or even
jObj["dAv"] = jObj.get["hAv"];
I've never really been a C/C++ guy but I'm trying to figure this stuff out! Any help is appreciated!