我正在尝试使用MQTT来接收消息,同时使用流HTTP服务器将我的picamera流到同一文件中的5000端口上。这导致我的MQTT相当不稳定,并且无法及时接收消息。我可以知道有人对此有解决方案吗?谢谢。
MQTT代码
def on_connect(client,rc):
if rc == 0:
global Connected
Connected = True
client.subscribe("picam/show")
def on_message(client, userdata, message):
global capture_image
data = message.payload.decode("utf-8").replace("'", '"')
d = json.loads(data)
if d["action"] == "take pic":
try:
capture_image()
except Exception as e:
print(e)
client = mqttClient.Client("picam")
client.on_connect= on_connect
client.connect(broker_address, port=1884)
# edited here to change loop_forever to loop_start
client.loop_start()
流HTTP代码
output = StreamingOutput()
camera.start_recording(output, format='mjpeg')
try:
address = ('', 5000)
server = StreamingServer(address, StreamingHandler)
server.serve_forever()
finally:
camera.stop_recording()
答案 0 :(得分:0)
设法通过使用线程来解决这个问题:)!万一有人也遇到这个问题