I need a little help getting over a programming “hump”. I’m a little lost in a few aspects of C++ but here’s what I’m trying to do. I want my photon devices to communicate by passing JSON messages via particle cloud or UDP. I’m trying to be as efficient as possible with memory space right from the start because JSON messages could be quite large. To that end, I’m trying to use pointers wherever possible to minimize copying large strings multiple times. In my project, I’m limiting the max JSON buffer to 512 bytes at the moment. I keep getting stuck on passing the data from the particle cloud event into the JSON parser.
On the event handler function, I keep getting an error about “invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]” in the CmdSubscribeHandler function on the line for strcpy(jsonString, &data). I get it, i’m not handling the character arrays properly, I just don’t know how to fix. I’ve tinkered with the pointers so much that I’m getting lost in syntax problems. At one point I did have the program working to the point where the event data was being passed to the command processor but the serial output for the raw data would only show 1 character “m”. I figured I had a problem where I was outputting the pointer instead of the real data. I tried making a local variable jsonString so that the message I’m parsing doesn’t get overwritten if a new cloud event comes in before parsing the first message was complete. (I’m not sure if that’s a real concern.)
Would someone please guide me to the best solution (minimal memory duplication) for handling the cloud event data and getting it it to the parser successfully? Is the srtcpy function necessary? Can I just use the pointer for *data without fear of it being overwritten by incoming events?
In the global setup area of the main program:
CmdProcessor cp(&s,&mo);
This is my particle cloud event handler:
void CmdSubscribeHandler(const char *event, const char *data)
{
char jsonString[512];
strcpy(jsonString, *data);
if (s.Debug) { Serial.printlnf("DSBcmd message received: %m", jsonString); }
//if (s.Debug) { Serial.println(jsonString); }
cp.ParseNew(&jsonString);
}
This is my CmdProcessor::ParseNew() function:
#include <SparkJson.h> //Using the SparkJson library in the CmdProcessor Class.
void CmdProcessor::ParseNew(char (*NewCmd)[512])
{
StaticJsonBuffer<512> jsonBuffer;
//Testing string.
//char json[] =
// "{\"id\":\"AAA\",\"setting\":1351824120,\"data\":[48.756080,2.302038]}";
JsonObject& root = jsonBuffer.parseObject(*NewCmd);
if (!root.success()) {
Serial.println("JSON parseObject() failed. Attempted to parse: ");
Serial.println(*NewCmd);
return;
}
for (auto id : root) {
char myKey[3];
strcpy(myKey, id.key);
if (s_cp->Debug) { Serial.printlnf("New cmd targets: %c", myKey); }
if (AppliesToMe(&myKey)) {
if (s_cp->Debug) { Serial.println("Cmd applies to me."); }
//Parsing code here...
} // if (AppliesToMe)
} //for (auto id : root)
} //ParseNew