Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/larsks/micropython-noggin
https://github.com/larsks/micropython-noggin
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/larsks/micropython-noggin
- Owner: larsks
- License: gpl-3.0
- Created: 2017-11-14T15:48:09.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-11-17T01:36:22.000Z (about 7 years ago)
- Last Synced: 2024-04-22T12:33:36.369Z (8 months ago)
- Language: Python
- Size: 68.4 KB
- Stars: 18
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-micropython-lib - Noggin - Web server for MicroPython (Networking)
- awesome-micropython - micropython-noggin - A very simple web server for MicroPython. (Libraries / Communications)
README
# Noggin: A very simple web server for MicroPython
## Installation
You need to get the `noggin` directory onto your MicroPython board. I
use the `ampy` command for this:ampy -p /dev/ttyUSB0 -b 115200 put noggin
There is a [Makefile](Makefile) in the repository that will compile all the
Python to byte code and then install it on your board, but it relies
on a [patched version of ampy][] so I don't recommend you use it at
this time.[patched version of ampy]: https://github.com/adafruit/ampy/pull/33
## Overview
Working with Noggin is very simple. Start by importing a few things
from the module:from noggin import Noggin, Response, HTTPError
Create a new Noggin instance:
app = Noggin()
Define some functions and map them to request paths. Your function
may return a text value to send that text to the client:@app.route('/device/([^/]+)/([^/]+)')
def device_info(req, dev_type, dev_id):
return 'You asked about device type {}, device id {}'.format(
dev_type, dev_id)Or you can return a dictionary or list to return JSON to the cilent:
@app.route('/device/([^/]+)/([^/]+)')
def device_info(req, dev_type, dev_id):
return {'dev_type': dev_type, 'dev_id': dev_id'}To run your app, call the `serve` method. You may optionally provide
a port:app.serve(port=8080)
Use the `HTTPError` exception to return errors to the client:
@app.route('/value/(.*)')
def get_value(req, sensor):
if sensor not in active_sensors:
raise HTTPError(404)Use the `Response` class if you want to set the returned content type
or other headers:@app.route('/')
def index(req):
return Response('This is a test',
content_type='text/html')## Examples
### The demo app
Install the demo app. I like to use [ampy][]:
[ampy]: https://github.com/adafruit/ampy
ampy -p /dev/ttyUSB0 -b 115200 put examples/demo.py demo.py
Now you can run the demo. On your MicroPython board, import the
example application:>>> import demo
>>> demo.app.serve()This will start a web server on port 80. See the docstrings
[in the source][] for more information about available request
methods.[in the source]: examples/demo.py
### The fileops app
The `fileops` app implements a simple web interface to the filesystem.
It supports the following requests:- `GET /disk` -- get information about the filesystem
- `GET /disk/free` -- get available free space (in blocks and bytes)
- `GET /file` -- get a list of files
- `PUT /file/` -- write a file to the filesystem
- `POST /file/` -- rename a file (new filename is `POST` body)
- `DELETE /file/` -- delete a file
- `GET /reset` -- execute `machine.reset()`To install the `fileops` app:
ampy -p /dev/ttyUSB0 -b 115200 put examples/fileops.py fileops.py
To run the `fileops` app:
>>> import fileops
>>> fileops.app.serve()