lighting-controller/networking.py

30 lines
808 B
Python

import asyncio
import websockets
import json
async def send_to_server(data):
"""
Send WebSocket data to the server.
"""
try:
# Connect to the WebSocket server
async with websockets.connect("ws://192.168.4.1:80/ws") as websocket:
# Serialize data to JSON and send it
await websocket.send(json.dumps(data))
except (ConnectionError, websockets.exceptions.ConnectionClosed) as e:
print(f"Error sending to {e}")
if __name__ == "__main__":
# Define the data to be sent
data = {
"settings": {
"color": "#00ff00"
}
}
# Server details
server = ("192.168.4.1", 80) # Example WebSocket server port
# Run the asynchronous function using asyncio.run
asyncio.run(send_to_server(data, server))