Skip to main content
2 of 7
added 9 characters in body; edited title
dda
  • 1.6k
  • 1
  • 13
  • 18

Convert JSON into string for MQTT

I am trying to publish a JSON object as a string with the following snippet:

JSONVar data;
data["chipid"] = chipId;
data["co2_equivalent"] = co2_equivalent.c_str();
data["tvoc"] = tvoc.c_str();
data["h2_signal"] = h2_signal;
data["ethanol_signal"] = ethanol_signal;
data["humidity"] = humidity;
data["temperature"] = temperature;
data["absolute_hum"] = v.absolute_hum;
data["co2_equivalent_baseline"] = co2_equivalent_baseline;
data["tvoc_baseline"] = tvoc_baseline;

String data_json = JSON.stringify(data);
Serial.print(data_json);
Serial.println();
client.publish("/devices/data", data_json);

This data doesn't get published to MQTT at all. Serial.print(data_json) does print it in the serial monitor. I don't know why it's not getting published to MQTT. Any thoughts on this?

rp346
  • 113
  • 4
  • 13