gcogco10 Ответов: 0

Как опубликовать данные с помощью json в Python-paho.mqtt?


Hi Team

I am struggling to find a way to publish a data as Json format using paho.mqtt in python. The program publish message when its connected and disconnect only, is there any mate who can help me on this regard? Meaning i want it to <pre>gets X, Y, Z translations and rotations from a 3D connexion space mouse and publishes the data to a MQTT topic on a specific broker. 


Что я уже пробовал:

<pre>#!python3
import paho.mqtt.client as mqtt # import the client1
import time
def on_log(client, userdata, level, buf):
        print("log: "+buf)
def on_connect(client, userdata, flags, rc):
    if rc==0:
        print("connected OK")
    else:
        print("Bad connection Returned code=", rc)
def on_disconnect(client, userdata, flags, rc=0):
     print("Disconnected result code "+str(rc))
#broker = "iot.eclipse.org"

broker = "192.168.1.206"
client = mqtt.Client("python1") # create new instance

client.on_connect=on_connect #bind call back function
client.on.disconnect=on_disconnect
client.on_log=on_log

print("Connecting to broker", broker)
client.connect(broker) # this connect to broker
client.loop_start() # stop loop

time.sleep(4)
client.loop_stop()
client.disconnect() # disconnect

0 Ответов