# SPDX-FileCopyrightText: 2023 Michał Pokusa # # SPDX-License-Identifier: Unlicense from asyncio import create_task, gather, run, sleep as async_sleep import board import microcontroller import neopixel import socketpool import wifi from adafruit_httpserver import Server, Request, Response, Websocket, GET pool = socketpool.SocketPool(wifi.radio) server = Server(pool, debug=True) pixel = neopixel.NeoPixel(board.NEOPIXEL, 1) websocket: Websocket = None HTML_TEMPLATE = """ <html lang="en"> <head> <title>Websocket Client</title> </head> <body> <p>CPU temperature: <strong>-</strong>°C</p> <p>NeoPixel Color: <input type="color"></p> <script> const cpuTemp = document.querySelector('strong'); const colorPicker = document.querySelector('input[type="color"]'); let ws = new WebSocket('ws://' + location.host + '/connect-websocket'); ws.onopen = () => console.log('WebSocket connection opened'); ws.onclose = () => console.log('WebSocket connection closed'); ws.onmessage = event => cpuTemp.textContent = event.data; ws.onerror = error => cpuTemp.textContent = error; colorPicker.oninput = debounce(() => ws.send(colorPicker.value), 200); function debounce(callback, delay = 1000) { let timeout return (...args) => { clearTimeout(timeout) timeout = setTimeout(() => { callback(...args) }, delay) } } </script> </body> </html> """ @server.route("/client", GET) def client(request: Request): return Response(request, HTML_TEMPLATE, content_type="text/html") @server.route("/connect-websocket", GET) def connect_client(request: Request): global websocket # pylint: disable=global-statement if websocket is not None: websocket.close() # Close any existing connection websocket = Websocket(request) return websocket server.start(str(wifi.radio.ipv4_address)) async def handle_http_requests(): while True: server.poll() await async_sleep(0) async def handle_websocket_requests(): while True: if websocket is not None: if (data := websocket.receive(fail_silently=True)) is not None: r, g, b = int(data[1:3], 16), int(data[3:5], 16), int(data[5:7], 16) pixel.fill((r, g, b)) await async_sleep(0) async def send_websocket_messages(): while True: if websocket is not None: cpu_temp = round(microcontroller.cpu.temperature, 2) websocket.send_message(str(cpu_temp), fail_silently=True) await async_sleep(1) async def main(): await gather( create_task(handle_http_requests()), create_task(handle_websocket_requests()), create_task(send_websocket_messages()), ) run(main())
# SPDX-FileCopyrightText: 2022 Dan Halbert for Adafruit Industries # # SPDX-License-Identifier: Unlicense import microcontroller import socketpool import wifi from adafruit_httpserver import Server, Request, JSONResponse pool = socketpool.SocketPool(wifi.radio) server = Server(pool, debug=True) # (Optional) Allow cross-origin requests. server.headers = { "Access-Control-Allow-Origin": "*", } @server.route("/cpu-information", append_slash=True) def cpu_information_handler(request: Request): """ Return the current CPU temperature, frequency, and voltage as JSON. """ data = { "temperature": microcontroller.cpu.temperature, "frequency": microcontroller.cpu.frequency, "voltage": microcontroller.cpu.voltage, } return JSONResponse(request, data) server.serve_forever(str(wifi.radio.ipv4_address))
# SPDX-FileCopyrightText: 2024 DJDevon3 # # SPDX-License-Identifier: MIT import wifi from adafruit_connection_manager import get_radio_socketpool from adafruit_httpserver import Server, Request, Response pool = get_radio_socketpool(wifi.radio) server = Server(pool, "/static", debug=True) @server.route("/") def base(request: Request): """ Serve a default static plain text message. """ return Response(request, "Hello from the CircuitPython HTTP Server!") server.serve_forever(str(wifi.radio.ipv4_address))
# SPDX-FileCopyrightText: 2023 Tim C for Adafruit Industries # SPDX-License-Identifier: MIT import board import digitalio from adafruit_httpserver import Server, Request, Response from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K import adafruit_wiznet5k.adafruit_wiznet5k_socketpool as socketpool print("Wiznet5k HTTPServer Test") # For Adafruit Ethernet FeatherWing cs = digitalio.DigitalInOut(board.D10) # For Particle Ethernet FeatherWing # cs = digitalio.DigitalInOut(board.D5) spi_bus = board.SPI() # Initialize ethernet interface with DHCP eth = WIZNET5K(spi_bus, cs) # Create a socket pool pool = socketpool.SocketPool(eth) # initialize the server server = Server(pool, "/static", debug=True) @server.route("/") def base(request: Request): """ Serve a default static plain text message. """ return Response(request, "Hello from the CircuitPython HTTP Server!") server.serve_forever(str(eth.pretty_ip(eth.ip_address)))
Resources
ReadTheDocs
Additional Examples
Text editor powered by tinymce.