The UWebServer
is a minimalistic web server for handling basic HTTP requests, implemented in MicroPython. Currently it handles GET, HEAD, OPTIONS and POST requests. This project was made to work with Raspberry Pi Pico W but should work with any similar device that supports MicroPython.
Before you start, save the uwebserver.py in lib
directory on your Pico. Then, import the necessary modules and the UWebServer
class. Make sure to also install picozero library.
import network
import socket
import ujson
import os
from uwebserver import UWebServer
To start, you'll need to create an instance of UWebServer. You can specify the port, the request size limit, and the static directory.
server = UWebServer(port=80, static_dir='static')
Define routes using the route decorator, providing the path and the method as arguments. The function decorated will handle requests to that path.
@server.route('/', 'GET')
def handle_root():
return 'Welcome to my web server!', 'text/html', "200 OK"
@server.route("/api/read", "GET")
def api_read():
response = readings
return ujson.dumps(response), "application/json", "200 OK"
@server.route("/api/write", "POST")
def api_write(data):
response = {"data_received": data}
return ujson.dumps(response), "application/json", "200 OK"
Start the server using the start method. This will keep the server running indefinitely.
server.start()
If the static_dir attribute is set when initializing UWebServer, GET requests to paths that match a file in the directory will respond with that file's contents.
For example, if static_dir is set to 'static' and there is a file 'static/index.html', a GET request to /index.html or / will respond with the contents of the file.
See example.py
that allows you to control the internal LED via browser.
- Fast & lightweight web server written in C++ - https://github.com/krzmaz/pico-w-webserver-example
- An HTTP server for the Raspberry Pi PicoW microcontroller, built on the Pico C SDK and using the LWIP raw TCP API. The server optionally supports TLS - https://gitlab.com/slimhazard/picow_http