Raspberry Pi Pico HTTP Admin Dashboard

, updated 12 September 2024 🔖 iot ⏲️ 1 minute to read

A simple way to deploy code to a Raspberry Pi Pico involves plugging the device into a USB port and uploading the code using a tool like Thonny. However, when the Pico is set up somewhere, in an enclosure with peripherals attached to it, a remote management solution is better.

Usage

The library is encapsulated in a single python file with no dependencies as management.py, and can be deployed with as-is with the following code in a separate file (e.g. main.py):

import management
import machine
import network
import utime

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.config(hostname = "hostname")
wlan.connect("ssid", "password")

while not wlan.isconnected():
    print(wlan)
    utime.sleep(1)

server = management.ManagementServer()

while True:
    server.update()
    machine.idle() # Idle, or perform other work

This is all you need to bootstrap a new Raspberry Pi Pico - you can attach devices to the pins, set it up, and deploy additional code remotely (this is what I plan to do moving forwards).

Sensor

This management tool is also integrated into the code I use to manage sensors around the house, available on GitHub.

The underlying TCP socket is set up as non-blocking, meaning that the socket I/O is interleaved with other tasks the Pico is performing.

Extending

The server itself can be extended with additional "controllers". This is a Python object which decides whether it can respond to a particular request, and a method to generate the response:

class HelloWorldController:
    def route(self, method, path):
        return method == b'GET' and path == b'/hello'
    
    def serve(self, method, path, headers, connection):
        connection.write(b'HTTP/1.0 200 OK\r\n')
        connection.write(b'Content-Type: text/html\r\n')
        connection.write(b'\r\n')
        connection.write(b'<p>Hello world!</p>')

# When setting up the server:
server = management.ManagementServer()
server.controllers.append(HelloWorldController())

For more advanced examples, including how to process form data, see management.py.

🏷️ pico code management raspberry pi deploy tool python file additional sensor socket http admin dashboard

⬅️ Previous post: Raspberry Pi Pico Ionising Radiation Sensor

➡️ Next post: Automatically Mounting USB Disks on Ubuntu

🎲 Random post: Jenkins Library for Unreal Engine 4

Comments

Please click here to load comments.