33 lines
1.1 KiB
Python
33 lines
1.1 KiB
Python
import asyncio
|
|
import websockets
|
|
import json
|
|
import random
|
|
import time
|
|
|
|
async def send_data(uri):
|
|
async with websockets.connect(uri) as websocket:
|
|
print("Connected to WebSocket server")
|
|
|
|
while True:
|
|
data = {
|
|
"node": "69:42:08:F5:00:00",
|
|
"Temp": str(round(random.uniform(0, 25), 2)),
|
|
"Humi": str(round(random.uniform(0, 90), 2)),
|
|
"eCO2": str(round(random.uniform(350, 2000), 0)),
|
|
"TVOC": str(round(random.uniform(100, 1200), 0))
|
|
}
|
|
await websocket.send(json.dumps(data))
|
|
print("Data sent")
|
|
|
|
response = await websocket.recv()
|
|
print("Received message:", response)
|
|
|
|
await asyncio.sleep(2) # Wait a bit before sending the next message
|
|
|
|
# Start the WebSocket connection
|
|
while True:
|
|
try:
|
|
asyncio.get_event_loop().run_until_complete(send_data("ws://145.92.8.114/ws"))
|
|
except Exception as e:
|
|
print("Exception:", e)
|
|
time.sleep(1) # Wait a bit before trying to reconnect |