- Version
- Download 7409
- File Size 0.94 KB
- File Count 1
- Create Date October 27, 2018
- Last Updated August 31, 2022
Python Send-Receive JSON Data Over MQTT
Newsletter
Receive content update notifications and new demo scripts by signing up for the newsletter using the form below.
Buy Me A Coffee
if you find this useful and would like to make a contribution, then you can do so by clicking here.
Please rate? And use Comments to let me know more
Thank you Steve, I tried it out but what to do if broker1 is password protected? I would like to read data from pw protected broker and write decoded date to another topic of the same broker. Don’t need 3 brokers.
Hi
You need to add this line before you connect
client.username_pw_set(username=”steve”,password=”password”)
rgds
steve
Thank you for the information, Steve. Now the Python script is running without errors but no data appears on the new topic.
>>> %Run json-data.pyl
{‘broker1’: ‘192.168.1.12’, ‘broker2’: ‘test.mosquitto.org’, ‘broker3’: ‘iot.eclipse.org’} brokers_out is a ‹class ‘dict’>
broker 1 address = 192.168.1.12
Converting to JSON
data -type
data out = {“broker1”: “192.168.1.12”, “broker2”: “test.mosquitto.org”, “broker3”: “iot.eclipse.org”}
Received Data
data in-type
data in= {“broker1”: “192.168.1.12”, “broker2”: “test.mosquitto.org”, “broker3”: “iot.eclipse.org”} brokers_in is a ‹class ‘dict’>
broker 1 address = 192.168.1.12
enter to Continue
Connecting to broker 192.168.1.12
sending data
>>>
bad format, it looked nice before sending
It looks like you need to convert it from JSOn to a dict. If you show me the on_message code on the receive side I will take a look.
Rgds
Steve
def on_message(client, userdata, msg) 😐
topic=msg. topic
m_decode=str(msg.payload.decode(“utf-8”, “ignore”))
print(“data Received type”, type(m_decode) )
print(“data Received”, m_decode)
print (“Converting from Json to Object”) m_in=json.loads(m_decode)
print (type(m_in))
print (“broker 1 address = “,m_in[“brokerl”])
topic=”cos_mqtt/get/M11/#”
client=mqtt.Client (“YUN-Sensor”)
client.on_message=on_message
client.username_pw_set(username=”YUN”, password=”xxxxxxxxxxxx”)
print (“Connecting to broker “,brokers_out [“broker]”])
client. connect (brokers_out [“broker1”]) client.loop_start()
client. subscribe(topic)
time.sleep (3)
print(“sending data”)
client.publish(“test/json_test”,data_out)
time.sleep (10)
_test”
client.loop_stop()
client.disconnect ()
Hi
Don’t see how you create the data_out that needs to be converted to JSON before sending using json.dumps
rgds
steve
data_out should be the data in cfos_mqtt/get/M11 topic. It is already json code. How to handle?
So you receive the data and then send it out? If that is the case you need to send the received data out before converting.
Probably easier to send me your entire code .
I’ve emailed you
rgds
steve
The goal is to take json data on different topics on my local broker extract data and send it to another topic on the same broker.
the data comes in as json so you convert it and then create a new message with the extracted data and then you need to turn it to json before sending.
Does that make sense
rgds
steve
I seems like I found what I have been looking for but unfortunately I am unable to extract the code file.
Hi
Added link to tutorial. Here it is
http://www.steves-internet-guide.com/download/python-send-receive-json-data-over-mqtt/