'Python脚本卡在json.loads()'

2024-10-05 15:18:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试为一个类运行一个脚本,该类使用JSON负载来打开或关闭led。我可以使用print()获取有效负载并打印出主题和负载。但是,当运行脚本时,它会挂起json.loads(). 任何帮助都将不胜感激。在

这是一个RaspberryPi3,Python3.5.3,带有grovepi板和led。我运行了grovepi附带的led淡出脚本,所以我知道硬件的工作原理。在

import time
import grovepi
import paho.mqtt.client as mqtt
import json


# Connect the LED to digital port D5
led = 5

# Set the blue LED pin to output mode
grovepi.pinMode(led,"OUTPUT")
time.sleep(1)

def on_connect(client, userdata, flags, rc):
"""Called each time the client connects to the message broker
:param client: The client object making the connection
:param userdata: Arbitrary context specified by the user program
:param flags: Response flags sent by the message broker
:param rc: the connection result
:return: None
"""
# subscribe to the LEDs topic when connected
client.subscribe("SNHU/IT697/leds")


def on_message(client, userdata, msg):
"""Called for each message received
:param client: The client object making the connection
:param userdata: Arbitrary context specified by the user program
:param msg: The message from the MQTT broker
:return: None
"""
print(msg.topic, msg.payload)
payload = json.loads(msg.payload)
# the legal values for analogWrite are 0-255
grovepi.analogWrite(led, payload['blue'])
^{pr2}$

Tags: thetoimport脚本clientjsonmessageled
1条回答
网友
1楼 · 发布于 2024-10-05 15:18:58

所以我安装了simplejson并用 将simplejson导入为json

现在它起作用了!在

不知道Python附带的json有什么问题,但至少我可以完成我的任务。我在想,要解决这个问题,我必须重新安装Python??在

相关问题 更多 >