arduino-libraries / Arduino_JSON

Official JSON Library for Arduino
GNU Lesser General Public License v2.1
151 stars 60 forks source link

JSON Object to MQTT Message Conversion #10

Closed baqwas closed 4 years ago

baqwas commented 4 years ago

Hello,

This is a newbie request for guidance. When I use the following code:


JSONVar _message_;
_message_["status"] = 1;
client.publish(topic, _message_); // publish to MQTT broker

The broker displays message as an empty string. Bottom line, is there some function/method to transform a JSON object to a string before use downstream. (Defeats the purpose of JSON, I know, but the use is external to board's sketch). Thanks.

Kind regards.

baqwas commented 4 years ago

Sorry for logging this. Problem solved with stringify and strcpy. Thanks.

Kind regards.

manchoz commented 4 years ago

@baqwas

JSONVar _message_;
_message_["status"] = 1;
String payload = JSON.stringify(_message_);
client.publish(topic, payload.c_str());