0

I am trying to publish jason to mqtt. Following is just a snippet of a code.When i try to add data["uuid"] to json data doesn't get published to MQTT, if I omit data["uuid"] everything works. UUID is of this format 24:0A:D4:37:AB:B7

#include <WiFi.h>
#include <Arduino_JSON.h>

String UUID()
{
  return WiFi.macAddress();
}

void setup() {
   ....
}

void loop() {
    JSONVar data;

    String uuid = String(UUID());
    data["uuid"] = uuid.c_str();
    data["timestamp"] = TimeStamp.c_str();
    ...
    String data_json = JSON.stringify(data);
    String cTopicName = String(mqtt_TopicName);

    Serial.print(data_json.c_str());

    client.publish(cTopicName.c_str(), data_json.c_str());

}

With Serial.print(data_json.c_str()); i can see the output on serial monitor, so I assume it's json is fine.

I am expecting it to publish {"uuid":"24:0A:D4:37:AB:B7","timestamp":"1589802371",...} to MQTT.

What I am doing wrong here ?

  • what is the actual data that is being published? – jsotola May 18 at 2:30
  • What happens if you replace data["uuid"] = uuid.c_str(); by data["uuid"] = "24:0A:D4:37:AB:B7";? – Edgar Bonet May 18 at 7:40
  • with data["uuid"] = "24:0A:D4:37:AB:B7"; doesnt get published to mqtt. – roy May 18 at 11:46
  • Sounds like either an out-of-memory condition, or you have exceeded the capacity of your JSONVar. – Edgar Bonet May 18 at 12:36
  • I am able to print data_json.c_str() to console. – roy May 18 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.