I have created two functions to parse two different JSON inputs using the ArduinoJSON library. Both functions run fine when run alone. But when I parse the station data in loadStationData followed by BinData in loadBinData. The station data become corrupted. I have placed Serial.println(station.msg); before and after the parseObject() call. Here is the Serial output ...
ln0001/ws00001. Message: {"tackid":1,"principal":"FA20307","msg":"start"}
JSONString parsed
ln0001/ws00001/sb000001. Message: {"part_number":"FA21208","amount":1,"state":"pick"}
Before: 8","amount":1,"state":"pick"}
After: ount
JSONString parsed
Lines 4 and 5 contain the before and after values of station.msg. This value should be "start" but it is part of the JSON string currently being parsed. Why is this happening? How can I fix it?
I have included the functions below...
struct StationData {
int tackid;
const char* principal;
const char* msg; //waiting, start, stopped
};
struct BinData {
const char* part_number;
int amount;
const char* state; //guard, pick, done, off
};
struct BinData bin = {
0, // part_number
0, // amount
"off", // state
};
struct StationData station = {
0, // tackid
"", // principal
"stopped", // msg
};
boolean loadStationData(StationData& data, char* json){
StaticJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
if (root.success()){
data.tackid= root["tackid"]|0;
data.principal = root["principal"]|"";
data.msg = root["msg"]|"";
return true;
} else {
return false;
}
}
boolean loadBinData(BinData& data, char* json){
Serial.print("Before ");
Serial.println(station.msg);
StaticJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
Serial.print("After ");
Serial.println(station.msg);
if (root.success()){
data.part_number = root["part_number"]|"";
data.state = root["state"]|"";
data.amount = root["amount"]|0;
return true;
} else {
return false;
}
}