Search code examples
mqttpublish-subscribearduino-idearduino-esp8266arduinojson

ESP8266 cannot publishing longer data over mqtt topic


I am using esp8266 and Arduino IDE for programming.when I am trying to publish JSON data over MQTT topic from esp8266 over a wifi network, it is working for the below code.

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");
}

but when I try to publish code as below,data is not receive at MQTT.Fx GUI client -

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");
}


Solution

  • As thrashed out in the comments

    The payload is too big. The solution is to use the publish_p() method.