【发布时间】:2020-07-05 23:43:24
【问题描述】:
我正在使用 Arduino 编写 ESP32,并使用标准 pubsubclient 库发布 MQTT 消息。
在下面的循环中,我注意到我的“内部循环”已发布,但是变量jsonObjChar 的第二条消息似乎从未发布到主题?串行监视器中没有任何错误。
void loop(){
String temperature = String(readDHTTemperature());
String humidity = String(readDHTHumidity());
String light = String(readLDRLight());
String jsonObj = "{";
jsonObj.concat("\"deviceId\":\"123456\"");
jsonObj.concat(",");
jsonObj.concat("\"messageType\":\"ambientSensorReading\"");
jsonObj.concat(",");
jsonObj.concat("\"temperature\":\"");
jsonObj.concat(temperature);
jsonObj.concat("\"");
jsonObj.concat(",");
jsonObj.concat("\"humidity\":\"");
jsonObj.concat(humidity);
jsonObj.concat("\"");
jsonObj.concat(",");
jsonObj.concat("\"light\":\"");
jsonObj.concat(light);
jsonObj.concat("\"}");
delay(1000);
int jsonObjCharLength = jsonObj.length() + 1;
char jsonObjChar[jsonObjCharLength];
jsonObj.toCharArray(jsonObjChar, jsonObjCharLength);
Serial.println("PREPARED");
Serial.println(jsonObjChar);
const char topic[13] = "prototype001";
client.publish(topic, "inside loop");
client.publish(topic, jsonObjChar);
// ... and resubscribe
client.subscribe("prototype001");
delay(5000);
}
变量jsobObjChar好像没问题,当我把它打印到串口监视器上时,它看起来像一个普通的字符串(我已经以JSON格式结构化,在服务器端处理)
20:13:41.494 -> PREPARED
20:13:41.494 -> {"deviceId":"123456","messageType":"ambientSensorReading","temperature":"25.10","humidity":"49.90","light":"2592"}
20:13:46.521 ->
如果有帮助,我正在使用 cloudmqtt。
任何帮助将不胜感激!!!
【问题讨论】:
-
你在哪里打电话
client.loop()?
标签: arduino mqtt publish-subscribe esp32