在paho MQTT中接收JSON数据的问题

llycmphe  于 2022-12-24  发布在  其他
关注(0)|答案(1)|浏览(300)

我尝试用paho-mqtt在python中发布和订阅JSON格式的消息。我可以发布消息,但订阅特定的键值时,我会收到很多错误。您能帮我找到错误并提供解决方案吗?我正在尝试打印方法的值。
代码:

def subscribe(client: mqtt_client):
    def on_message(client, userdata, msg):
        #print(f"Received `{msg.payload}` from `{msg.topic}` topic") #.decode()
        #topic = msg.topic

        m_decode = str(msg.payload.decode("utf-8", "ignore"))
        print("data Received type", type(m_decode))
        print("data Received", m_decode)

        m_in = json.loads(m_decode)  # decode json data
        print(type(m_in))
        print("method is = ", m_decode["method"])

    client.subscribe(topic_sub)
    client.on_message = on_message

错误消息如下所示:

Traceback (most recent call last):
  File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\try1.py", line 76, in <module>
    run()
  File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\try1.py", line 73, in run
    client.loop_forever()
  File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 1756, in loop_forever
    rc = self._loop(timeout)
  File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 1164, in _loop
    rc = self.loop_read()
  File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 1556, in loop_read
    rc = self._packet_read()
  File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 2439, in _packet_read
    rc = self._packet_handle()
  File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 3033, in _packet_handle
    return self._handle_publish()
  File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 3327, in _handle_publish
    self._handle_on_message(message)
  File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 3570, in _handle_on_message
    on_message(self, self._userdata, message)
  File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\try1.py", line 35, in on_message
    m_in = json.loads(m_decode)  # decode json data
  File "C:\Users\Vinod Kumar\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Users\Vinod Kumar\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\Vinod Kumar\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
nfs0ujit

nfs0ujit1#

我猜你的代码中有一个错字:)

def subscribe(client: mqtt_client):
    def on_message(client, userdata, msg):
        #print(f"Received `{msg.payload}` from `{msg.topic}` topic") #.decode()
        #topic = msg.topic

        m_decode = str(msg.payload.decode("utf-8", "ignore"))
        print("data Received type", type(m_decode))
        print("data Received", m_decode)

        m_in = json.loads(m_decode)  # decode json data
        print(type(m_in))
        print("method is = ", m_decode["method"])  # <-- shall be m_in["method"]

    client.subscribe(topic_sub)
    client.on_message = on_message

相关问题