我有树莓派。我从中读取数据。我通过远程服务器上的mqtt发送数据并将数据发送到客户端。我希望该数据始终发送到mqtt主机,现在仅当我从websocket刷新数据时才发送数据吗?
import paho.mqtt.client as mqtt
import configparser
import asyncio
import websockets
import os
import time
from random import uniform, normalvariate
broker_address="mqtthost.org"
client = mqtt.Client("P1") #create new instance
client.connect(broker_address) #connect to brok
async def send(websocket, path):
while True:
val = round(uniform(0,10), 2)
await client.publish("sensor/temperature", val)#publish
await websocket.send(val)
#await asyncio.sleep(1)
start_server = websockets.serve(send, '127.0.0.1', 5678)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()