Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Send data as JSON objects over to MQTT broker

Tags:

json

mqtt

paho

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.

like image 363
user3690081 Avatar asked May 30 '14 05:05

user3690081


People also ask

Can I send JSON in MQTT?

IMPORTANT: The JSON data sent to each MQTT topic must be 75 bytes or less to be transferred over the wireless network to the sensor platform. and the quality limit. For a description of the values, see Quality Codes.

Can you send JSON directly over a network just write yes or no?

Short answer, yes that is the proper way to send the JSON. You should not be placing anything other than a string inside of quotes.


Video Answer


1 Answers

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);
like image 108
hardillb Avatar answered Nov 02 '22 11:11

hardillb