ESP8266 无法通过 mqtt 主题发布更长的数据
ESP8266 cannot publishing longer data over mqtt topic
我正在使用 esp8266 和 Arduino IDE programming.when 我正在尝试通过 wifi 网络从 esp8266 通过 MQTT 主题发布 JSON 数据,它适用于以下代码.
void reportToServer(){
String payload = "{\"DeviceID\":"+String(devices[0].getDeviceID())+","
"\"DeviceName\":"+"\""+String(devices[0].getDeviceName())+"\""+","
"\"Counter\":"+String(devices[0].getSwitchOnTime())+","
"\"Status\":"+String(devices[0].getStatus())+"};
Serial.println(payload.c_str());
client.publish("esp/test",payload.c_str());
Serial.println("Reported to server");
}
但是当我尝试如下发布代码时,MQTT.Fx GUI 客户端未收到数据 -
void reportToServer(){
String payload = String payload = "{{\"DeviceID\":"+String(devices[0].getDeviceID())+","
"\"DeviceName\":"+"\""+String(devices[0].getDeviceName())+"\""+","
"\"Counter\":"+String(devices[0].getSwitchOnTime())+","
"\"Status\":"+String(devices[0].getStatus())+"},"+
"{\"DeviceID\":"+String(devices[1].getDeviceID())+","
"\"DeviceName\":"+"\""+String(devices[1].getDeviceName())+"\""+","
"\"Counter\":"+String(devices[1].getSwitchOnTime())+","
"\"Status\":"+String(devices[1].getStatus())+"}}";
+"{\"DeviceID\":"+String(devices[2].getDeviceID())+","
"\"DeviceName\":"+"\""+String(devices[2].getDeviceName())+"\""+","
"\"Counter\":"+String(devices[2].getSwitchOnTime())+","
"\"Status\":"+String(devices[2].getStatus())+"},"
+"{\"DeviceID\":"+String(devices[3].getDeviceID())+","
"\"DeviceName\":"+"\""+String(devices[3].getDeviceName())+"\""+","
"\"Counter\":"+String(devices[3].getSwitchOnTime())+","
"\"Status\":"+String(devices[3].getStatus())+"},"
+"{\"DeviceID\":"+String(devices[4].getDeviceID())+","
"\"DeviceName\":"+"\""+String(devices[4].getDeviceName())+"\""+","
"\"Counter\":"+String(devices[4].getSwitchOnTime())+","
"\"Status\":"+String(devices[4].getStatus())+"},"
+"{\"DeviceID\":"+String(devices[5].getDeviceID())+","
"\"DeviceName\":"+"\""+String(devices[5].getDeviceName())+"\""+","
"\"Counter\":"+String(devices[5].getSwitchOnTime())+","
"\"Status\":"+String(devices[5].getStatus())+"}}";
Serial.println(payload.c_str());
client.publish("esp/test",payload.c_str());
Serial.println("Reported to server");
}
正如评论中所讨论的那样
负载太大。解决方案是使用publish_p()
方法。
我正在使用 esp8266 和 Arduino IDE programming.when 我正在尝试通过 wifi 网络从 esp8266 通过 MQTT 主题发布 JSON 数据,它适用于以下代码.
void reportToServer(){
String payload = "{\"DeviceID\":"+String(devices[0].getDeviceID())+","
"\"DeviceName\":"+"\""+String(devices[0].getDeviceName())+"\""+","
"\"Counter\":"+String(devices[0].getSwitchOnTime())+","
"\"Status\":"+String(devices[0].getStatus())+"};
Serial.println(payload.c_str());
client.publish("esp/test",payload.c_str());
Serial.println("Reported to server");
}
但是当我尝试如下发布代码时,MQTT.Fx GUI 客户端未收到数据 -
void reportToServer(){
String payload = String payload = "{{\"DeviceID\":"+String(devices[0].getDeviceID())+","
"\"DeviceName\":"+"\""+String(devices[0].getDeviceName())+"\""+","
"\"Counter\":"+String(devices[0].getSwitchOnTime())+","
"\"Status\":"+String(devices[0].getStatus())+"},"+
"{\"DeviceID\":"+String(devices[1].getDeviceID())+","
"\"DeviceName\":"+"\""+String(devices[1].getDeviceName())+"\""+","
"\"Counter\":"+String(devices[1].getSwitchOnTime())+","
"\"Status\":"+String(devices[1].getStatus())+"}}";
+"{\"DeviceID\":"+String(devices[2].getDeviceID())+","
"\"DeviceName\":"+"\""+String(devices[2].getDeviceName())+"\""+","
"\"Counter\":"+String(devices[2].getSwitchOnTime())+","
"\"Status\":"+String(devices[2].getStatus())+"},"
+"{\"DeviceID\":"+String(devices[3].getDeviceID())+","
"\"DeviceName\":"+"\""+String(devices[3].getDeviceName())+"\""+","
"\"Counter\":"+String(devices[3].getSwitchOnTime())+","
"\"Status\":"+String(devices[3].getStatus())+"},"
+"{\"DeviceID\":"+String(devices[4].getDeviceID())+","
"\"DeviceName\":"+"\""+String(devices[4].getDeviceName())+"\""+","
"\"Counter\":"+String(devices[4].getSwitchOnTime())+","
"\"Status\":"+String(devices[4].getStatus())+"},"
+"{\"DeviceID\":"+String(devices[5].getDeviceID())+","
"\"DeviceName\":"+"\""+String(devices[5].getDeviceName())+"\""+","
"\"Counter\":"+String(devices[5].getSwitchOnTime())+","
"\"Status\":"+String(devices[5].getStatus())+"}}";
Serial.println(payload.c_str());
client.publish("esp/test",payload.c_str());
Serial.println("Reported to server");
}
正如评论中所讨论的那样
负载太大。解决方案是使用publish_p()
方法。