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 an eight dollar microcontroller, it's pretty sweet.
Copy thimble.py to your MicroPython powered microcontroller (or install with MIP). Use main.py to test. Customize with your own functions and routes.
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.
from thimble import Thimble
app = Thimble()
@app.route('/')
def hello(req):
return 'Hello World'
app.run()
Now, point your web browser to the IP of your device on the default port of 80 and you should see 'Hello World'.
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 is in the early phases of development and 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-C3 devkit with MicroPython 1.21. Occasionally, I will run it on a Wemos LOLIN32 (ESP32) or a NodeMCU ESP-12E (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.