I have an application where I am getting some 9 values over a serial link. I am parsing the serial data and converting to a json formatted string for posting to a server using the POST method. But before posting I would like to know if the resultant string from the function below is a valid json string ( it does not seem to be as the server reports NULL value posting ). Is the way that I have formed the json string OK ? I get the following as the string when debugging using the serial monitor :
{"machine_id":"ABCD_TR","date":"2019-07-16","time":"16:44:12","rig_power_status":"POWER ON","rig_operation_status":"POWER ON","rig_kwh_value":"ABCD-1234","rig_dut_model":"10","rig_number_tested":"1001","rig_number_passed":"951"}
And the function used to create the json string from data received via serial is as below :
void parseSerialData () {
int index;
if (newData == true)
{
// Serial.print("Data string from PC test ");
// Serial.println(receivedChars);
String receivedChars1 = receivedChars;
index = receivedChars1.indexOf(',');
jsonData = "{\"machine_id\":";
jsonData += "\"" + (String)receivedChars1.substring(0, index) + "\"";
jsonData += ",\"date\":";
receivedChars1 = receivedChars1.substring(index + 1);
index = receivedChars1.indexOf(',');
jsonData += "\"" + (String)receivedChars1.substring(0, index) + "\"";
jsonData += ",\"time\":";
receivedChars1 = receivedChars1.substring(index + 1);
index = receivedChars1.indexOf(',');
jsonData += "\"" + (String)receivedChars1.substring(0, index) + "\"";
jsonData += ",\"rig_power_status\":"; // On or Off
receivedChars1 = receivedChars1.substring(index + 1);
index = receivedChars1.indexOf(',');
jsonData += "\"" + (String)receivedChars1.substring(0, index) + "\"";
jsonData += ",\"rig_operation_status\":"; // Testing / Standby / Breakdown
receivedChars1 = receivedChars1.substring(index + 1);
index = receivedChars1.indexOf(',');
jsonData += "\"" + (String)receivedChars1.substring(0, index) + "\"";
jsonData += ",\"rig_kwh_value\":";
receivedChars1 = receivedChars1.substring(index + 1);
index = receivedChars1.indexOf(',');
jsonData += "\"" + (String)receivedChars1.substring(0, index) + "\"";
jsonData += ",\"rig_dut_model\":";
receivedChars1 = receivedChars1.substring(index + 1);
index = receivedChars1.indexOf(',');
jsonData += "\"" + (String)receivedChars1.substring(0, index) + "\"";
jsonData += ",\"rig_number_tested\":";
receivedChars1 = receivedChars1.substring(index + 1);
index = receivedChars1.indexOf(',');
jsonData += "\"" + (String)receivedChars1.substring(0, index) + "\"";
jsonData += ",\"rig_number_passed\":";
receivedChars1 = receivedChars1.substring(index + 1);
index = receivedChars1.indexOf(',');
jsonData += "\"" + (String)receivedChars1.substring(0, index) + "\"";
jsonData += "}";
newData = false;
}
}
Or is there a way to form the Json data with char variables without using the String type ?