|
3 | 3 | # SPDX-License-Identifier: Unlicense
|
4 | 4 |
|
5 | 5 | from time import monotonic
|
| 6 | +import board |
| 7 | +import microcontroller |
| 8 | +import neopixel |
6 | 9 | import socketpool
|
7 | 10 | import wifi
|
8 | 11 |
|
|
12 | 15 | pool = socketpool.SocketPool(wifi.radio)
|
13 | 16 | server = Server(pool, debug=True)
|
14 | 17 |
|
| 18 | +pixel = neopixel.NeoPixel(board.NEOPIXEL, 1) |
| 19 | + |
15 | 20 |
|
16 | 21 | websocket: Websocket = None
|
17 | 22 | next_message_time = monotonic()
|
|
22 | 27 | <title>Websocket Client</title>
|
23 | 28 | </head>
|
24 | 29 | <body>
|
25 |
| - <input id="message" type="text" placeholder="Message..."><br> |
26 |
| - <button id="send">Send</button> |
27 |
| -
|
28 |
| -
|
| 30 | + <p>CPU temperature: <strong>-</strong>°C</p> |
| 31 | + <p>NeoPixel Color: <input type="color"></p> |
29 | 32 | <script>
|
30 |
| - const messageInput = document.querySelector('#message'); |
31 |
| - const sendButton = document.querySelector('#send'); |
| 33 | + const cpuTemp = document.querySelector('strong'); |
| 34 | + const colorPicker = document.querySelector('input[type="color"]'); |
32 | 35 |
|
33 | 36 | let ws = new WebSocket('ws://' + location.host + '/connect-websocket');
|
34 | 37 |
|
35 | 38 | ws.onopen = () => console.log('WebSocket connection opened');
|
36 |
| - ws.onerror = error => console.error('WebSocket error:', error); |
37 |
| - ws.onmessage = event => console.log('Received message from server: ', event.data); |
38 |
| -
|
39 |
| - let interval = setInterval(() => ws.send("Hello from client"), 1000); |
40 |
| -
|
41 |
| - ws.onclose = x => { |
42 |
| - console.log('WebSocket connection closed'); |
43 |
| - clearInterval(interval); |
44 |
| - }; |
45 |
| -
|
46 |
| - sendButton.onclick = () => ws.send(messageInput.value); |
| 39 | + ws.onclose = () => console.log('WebSocket connection closed'); |
| 40 | + ws.onmessage = event => cpuTemp.textContent = event.data; |
| 41 | + ws.onerror = error => cpuTemp.textContent = error; |
| 42 | +
|
| 43 | + colorPicker.oninput = debounce(() => ws.send(colorPicker.value), 200); |
| 44 | +
|
| 45 | + function debounce(callback, delay = 1000) { |
| 46 | + let timeout |
| 47 | + return (...args) => { |
| 48 | + clearTimeout(timeout) |
| 49 | + timeout = setTimeout(() => { |
| 50 | + callback(...args) |
| 51 | + }, delay) |
| 52 | + } |
| 53 | + } |
47 | 54 | </script>
|
48 | 55 | </body>
|
49 | 56 | </html>
|
@@ -73,10 +80,12 @@ def connect_client(request: Request):
|
73 | 80 |
|
74 | 81 | # Check for incoming messages from client
|
75 | 82 | if websocket is not None:
|
76 |
| - if (message := websocket.receive(True)) is not None: |
77 |
| - print("Received message from client:", message) |
| 83 | + if (data := websocket.receive(True)) is not None: |
| 84 | + r, g, b = int(data[1:3], 16), int(data[3:5], 16), int(data[5:7], 16) |
| 85 | + pixel.fill((r, g, b)) |
78 | 86 |
|
79 | 87 | # Send a message every second
|
80 | 88 | if websocket is not None and next_message_time < monotonic():
|
81 |
| - websocket.send_message("Hello from server") |
| 89 | + cpu_temp = round(microcontroller.cpu.temperature, 2) |
| 90 | + websocket.send_message(str(cpu_temp)) |
82 | 91 | next_message_time = monotonic() + 1
|
0 commit comments