I tried for hours to assemble a simple string of a JSON object on arduino to send a raspberry Pi running node.
I cannot successfully generate a string. I tried to build the string in one go:
"{" + string1 + "," + string2 + "," + string3 + "}" etc...
I also tried using the String.replace function. Every time I finish a little of my line or nothing at all. The code below shows what happens:
String msg = "{ \"message\" : \"statusUpdate\", "; String active = " \"active\" : TOKEN, "; String intakeTemp = " \"intakeTemp\" : TOKEN, "; String intakeHumid = " \"intakeHumid\" : TOKEN, "; String exhaustTemp = " \"exhaustTemp\" : TOKEN, "; String exhaustHumid = " \"exhaustHumid\" : TOKEN, "; String targetHumid = " \"targetHumid\" : TOKEN, "; String completed = " \"taskCompleted\" : TOKEN }"; if(isActive) active.replace("TOKEN","true"); else active.replace("TOKEN","false"); intakeTemp.replace("TOKEN",floatToString(intakeTemperature,0)); intakeHumid.replace("TOKEN",floatToString(intakeHumidity,0)); exhaustTemp.replace("TOKEN",floatToString(exhaustTemperature,0)); exhaustHumid.replace("TOKEN",floatToString(exhaustHumidity,0)); targetHumid.replace("TOKEN",floatToString(targetHumidity,0)); if(taskFinished) taskCompleted.replace("TOKEN","true"); else taskCompleted.replace("TOKEN","false"); String body = msg; Serial.println(body); body += active; Serial.println(body); body += intakeTemp; Serial.println(body); body += intakeHumid; Serial.println(body); body += exhaustTemp; Serial.println(body); body += exhaustHumid; Serial.println(body); body += targetHumid; Serial.println(body); body += taskCompleted; Serial.println(body);
You can see from the last bit of code above that when building a line, I spit it out on a serial monitor. However, here is my serial output:
{ "message" : "statusUpdate", { "message" : "statusUpdate", "active" : false, { "message" : "statusUpdate", "active" : false, "intakeTemp" : 0.0, { "message" : "statusUpdate", "active" : false, "intakeTemp" : 0.0, "intakeHumid" : 0.0, { "message" : "statusUpdate", "active" : false, "intakeTemp" : 0.0, "intakeHumid" : 0.0, "exhaustTemp" : 0.0, { "message" : "statusUpdate", "active" : false, "intakeTemp" : 0.0, "intakeHumid" : 0.0, "exhaustTemp" : 0.0, { "message" : "statusUpdate", "active" : false, "intakeTemp" : 0.0, "intakeHumid" : 0.0, "exhaustTemp" : 0.0, { "message" : "statusUpdate", "active" : false, "intakeTemp" : 0.0, "intakeHumid" : 0.0, "exhaustTemp" : 0.0,
Is there a string length limit? I did not find mention of such restrictions in the documents. There is nothing special in the sketch, except for the standard Ethernet library and code for sending via an HTTP request (from an example project).
Any idea what could happen?
EDIT: So, I shortened my line like this:
String msg = "{ \"m\" : \"status\", "; String active = " \"a\" : TOKEN, "; String intakeTemp = " \"iT\" : TOKEN, "; String intakeHumid = " \"iH\" : TOKEN, "; String exhaustTemp = " \"eT\" : TOKEN, "; String exhaustHumid = " \"eH\" : TOKEN, "; String targetHumid = " \"tH\" : TOKEN, "; String dryerJustFinished = " \"f\" : TOKEN }";
and, of course, it started working:
{ "m" : "status", { "m" : "status", "a" : false, { "m" : "status", "a" : false, "iT" : 0.0, { "m" : "status", "a" : false, "iT" : 0.0, "iH" : 0.0, { "m" : "status", "a" : false, "iT" : 0.0, "iH" : 0.0, "eT" : 0.0, { "m" : "status", "a" : false, "iT" : 0.0, "iH" : 0.0, "eT" : 0.0, "eH" : 0.0, { "m" : "status", "a" : false, "iT" : 0.0, "iH" : 0.0, "eT" : 0.0, "eH" : 0.0, "tH" : 0.0, { "m" : "status", "a" : false, "iT" : 0.0, "iH" : 0.0, "eT" : 0.0, "eH" : 0.0, "tH" : 0.0, "f" : false }
This means that there is a limitation. Is this a memory limit?
By the way, the hardware of the Arduino Uno R3