Search code examples
jsonmqttpaho

How to Send data as JSON objects over to MQTT broker


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.


Solution

  • 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);