• 起因是因为 订阅的时候,获取到的 MQTT 信息时,第一条信息好像是连接信息,所以需要过滤他。

  • 接收到的数据如下

    Python MQTT订阅获取发布信息字典过滤

  • 必须要过滤这个 name : 1 的字典,操作如下:

    def on_message(client, userdata, msg):
        print(msg.topic+" " + ":" + str(msg.payload))
        print(type(msg.payload))
        print(json.loads(msg.payload.decode('utf8')))
        print(type(json.loads(msg.payload.decode('utf8'))))
        js_code = json.loads(msg.payload.decode('utf8'))
        print(js_code);

        if "name" in js_code :
            print(js_code["name"]);
        elif "gateway_id" in js_code :
            print(js_code["gateway_id"]);
            print(js_code["funcode"]);
            print(js_code["device_id"]);
            print(js_code["value"]);
        else :
            print("dict error");

相关文章:

  • 2022-12-23
  • 2021-08-04
  • 2022-12-23
  • 2022-01-20
  • 2021-10-18
  • 2021-12-06
  • 2021-12-18
猜你喜欢
  • 2021-07-30
  • 2021-10-26
  • 2021-12-19
  • 2021-11-18
  • 2022-12-23
  • 2021-12-12
相关资源
相似解决方案