How to Send data as JSON objects over to MQTT broker

user3690081 picture user3690081 · May 30, 2014 · Viewed 41.8k times · Source

I'm using eclipse paho client on ubuntu and trying to send latitude, longitude and timestamp information as JSON format to the MQTT broker. How do I do that?

I found this article, But its not complete.

Answer

hardillb picture hardillb · May 30, 2014

You just need to create your JSON object as a string then call getBytes() on that string to get the byte array to use as your payload in the message.

 MqttMessage message = new MqttMessage();
 message.setPayload("{foo: bar, lat: 0.23443, long: 12.3453245}".getBytes());
 client.publish("foo", message);