har*_*llb 15
您只需要将JSON对象创建为字符串,然后在该字符串上调用getBytes()以获取字节数组以用作消息中的有效负载.
MqttMessage message = new MqttMessage();
message.setPayload("{foo: bar, lat: 0.23443, long: 12.3453245}".getBytes());
client.publish("foo", message);
Run Code Online (Sandbox Code Playgroud)
我对此一无所知,但我使用他的:
#!/usr/bin/python
import json
import paho.mqtt.client as mqtt
send_msg = {
'data_to_send': variable1,
'also_send_this': variable2
}
client.publish("topic", payload=json.dumps(send_msg), qos=2, retain=False)
Run Code Online (Sandbox Code Playgroud)