0

我正在使用 esp8266 和 Arduino IDE 进行编程。当我尝试通过 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");
}

4

1 回答 1

1

正如评论中所讨论的那样

有效载荷太大。解决方法是使用publish_p()方法。

于 2020-03-04T12:28:52.267 回答