Git Product home page Git Product logo

micropython-noggin's Introduction

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 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.

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('<strong>This</strong> is a test',
                    content_type='text/html')

Examples

The demo app

Install the demo app. I like to use 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.

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/<path> -- write a file to the filesystem
  • POST /file/<path> -- rename a file (new filename is POST body)
  • DELETE /file/<path> -- 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()

micropython-noggin's People

Contributors

larsks avatar spotlightkid avatar

Stargazers

Takehiro Ogura avatar Mohammad Reza Ebadollah avatar Zeev Rotshtein avatar Pader avatar  avatar Ahmad Rifa'i avatar  avatar  avatar Eli McRae avatar Derek Payton avatar P T Nguyen avatar Jelivor Jin avatar Gabriel Melo avatar Apoorva Singh avatar  avatar Vladislav Bauer avatar Mordy Ovits avatar  avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

jfly rifai716

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.