A tiny web framework for MicroPython.
Thimble is similar in spirit to Flask, but scaled way back to fit on a microcontroller. Basically, you can create a web application using functions and routes. It's more robust than the typical MicroPython web server example code. But, there's a lot of features it lacks when compared to a full-featured web application framework. Still, for something that runs on a five dollar microcontroller, it's pretty sweet.
Thimble is a class with a route() method and a run() method. You add routes similar to the way you would with Flask and then call a run() method to start listening.
Copy thimble.py to your MicroPython powered microcontroller (or install with MIP). Use main.py to test. Customize with your own functions and routes.
Contents of main.py:
from thimble import Thimble
app = Thimble()
@app.route("/")
def hello(req):
return "Hello World"
@app.route("/gpio/<digit>")
def gpio(req, num):
return f"Hello GPIO {num}"
app.run(debug=True)
Now, point your web browser to the IP of your device on the default port of 80 and you should see 'Hello World'. Using the URL path of /gpio/2 should return 'Hello GPIO 2'.
There are more complex examples in the examples subdirectory.
See the wiki for examples in a step by step tutorial format.
As we say here in Wisconsin: "Oh yah, you betcha!" You can put your static files in /static
on your flash filesystem and they will be served up just like any other web server, though a bit slower.
You can save space by compressing static files with GZIP and adding a .gzip
extension. These files will be sent with a 'Content-Encoding: gzip' header that will tell the web browser to decompress upon receipt. For example, something like script-library.js could be compressed and stored as script-library.js.gzip. Then, when a request is made for script-library.js, Thimble will send the contents of script-library.js.gzip and add the Content-Encoding header.
Using the example main.py assumes that networking is already configured by a boot.py that you supply. Thimble won't work without it. If you need help with wifi connections, see my example under lolin32oled
Thimble has been around for a while, but may have a few bugs lurking. If you find one, add a Github issue and I'll see if I can fix it.
Code is being developed and tested using an ESP32 Devkit V1 clone with MicroPython 1.24. Occasionally, I will run it on an ESP32-C3 or ESP8266. It may or may not work with other devices.
Using mpremote on Windows, do this:
py -m mpremote connect PORT mip install github:DavesCodeMusings/thimble
where PORT is something like COM4 (or whatever shows up in Device Manager for your microcontroller.)
For Linux mpremote, do this:
mpremote connect PORT mip install github:DavesCodeMusings/thimble
where PORT is something like /dev/ttyUSB0 (or whatever shows up in your dmesg
output when you plug the device in.)
Or just download directly from https://raw.githubusercontent.com/DavesCodeMusings/thimble/main/thimble.py and place it in your device's /lib directory.