Python Send-Receive JSON Data Over MQTT

[featured_image]
Download
Download is available until [expire_date]
  • Version
  • Download 7344
  • 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.

Loading

Buy Me A Coffee

pay-pal-buy-now

if you find this useful and would like to make a contribution, then you can do so by clicking here.

13 comments

  1. 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.

    1. Hi
      You need to add this line before you connect
      client.username_pw_set(username=”steve”,password=”password”)
      rgds
      steve

      1. 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
        >>>

        1. 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

          1. 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 ()

          2. Hi
            Don’t see how you create the data_out that needs to be converted to JSON before sending using json.dumps
            rgds
            steve

          3. data_out should be the data in cfos_mqtt/get/M11 topic. It is already json code. How to handle?

          4. 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

          5. 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.

          6. 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

Leave a Reply

Your email address will not be published. Required fields are marked *