سؤال

Am using eclipse paho 'C' client in my ubuntu and trying to send latitude, longitude and timestamp information as JSON format to the MQTT broker. How do i do that ?

reference: http://www.eclipse.org/paho/clients/c/

هل كانت مفيدة؟

المحلول

You define the JSON payload you need and build a string using one of the JSON libraries and simply publish that payload string from your application.

Say you have

float lat = 10.001;
float lon = 20.002;
long timestamp = 1400000;

you would, using one of the JSON libs, end up with a JSON string like

{
  "lat" : 10.001,
  "lon" : 20.002,
  "timestamp" : 140000
}

which is a perfectly valid string to publish over MQTT.

Whether you use float/double variables or convert those to strings first is a matter of taste and requirements. (Strings may be safer during conversion.)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top