adafruit / Adafruit_MQTT_Library

Arduino library for MQTT support
MIT License
572 stars 291 forks source link

Sending JSON with Adafruit Publish #78

Closed timkoers closed 7 years ago

timkoers commented 7 years ago

I am trying to send JSON trough to my adafruit feed. I feel like this is blocked.

components_feed.publish(output.c_str()); // Some valid json string components_feed.publish("Hi"); components_feed.publish("{ \ "hi\ ":\ "test\ "}");

OUTPUT:

2017-05-08 05:36:05 pm components: Hi 2017-05-08 05:36:05 pm errors: failed to publish value: data missing required value 2017-05-08 05:36:06 pm errors: failed to publish value: data missing required value

Am I doing anything wrong?

dniklaus commented 7 years ago

You should escape the inner double quotes using a backslash: components_feed.publish("{ \"hi\":\"test\"}"); I suggest to use the ArduinoJson library by @bblanchon (https://github.com/bblanchon/ArduinoJson) to encode the JSON data to be published. The same library also helps with the decoding of the received JSON data.

jwcooper commented 7 years ago

Thank you for the reply @dniklaus.

Also, best place for general questions using libraries would be at our forums at https://forums.adafruit.com.

Thanks!

timkoers commented 7 years ago

I am using the ArduinoJson library from bblanchon. Is there any length restriction to the data? EDIT: I did escape the quotes in my code, GitHub formats them away.