几个小时以来,我一直在尝试在 arduino 上组合一个简单的 JSON 对象字符串,以发送到 Raspberry Pi 运行节点。
我似乎无法成功构建字符串。我曾尝试一次性构建字符串:
"{" + string1 + "," + string2 + "," + string3 + "}" etc...
我也尝试过使用 String.replace 函数。每次我都会得到一点我的字符串,或者根本没有。下面的代码显示了正在发生的事情:
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);
您可以从上面的最后一段代码中看到,在构建字符串时,我将它吐出到串行监视器上。但是,这是我的串行输出:
{ "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,
字符串长度有限制吗?我没有在文档中找到任何提及此类限制的内容。除了标准Ethernet
库和通过 HTTP 请求(来自示例项目)发送它的代码之外,草图没有太多其他内容。
知道会发生什么吗?
编辑:好的,我已经像这样缩短了我的字符串:
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 }";
果然,它开始起作用了:
{ "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 }
这意味着存在限制。这是内存限制吗?
顺便说一句,硬件是 Arduino Uno R3