Jupyter notebook kernel for remote execution on a Micropython system.
Project description
Jupyter kernel to interact with a MicroPython board over its REPL interface.
Typically used with micropython boards over the USB / Serial interface, however it should also work through the WEBREPL (available on ESP8266 only). Also includes a few advanced features for micorpython project management; running mpy-cross, uploading files, syncing local libs to micropython etc.
Micropython
This kernel requires support in micropython from https://github.com/micropython/micropython/pull/3836 At the time of publishing, this is not in the current release, 1.9.4, so will require a master / daily snapshot until 1.9.5
Installation
Ensure you have a new enough micropython installed on your board (see above).
You also need python 3.6 or above, ensuring it’s available from your current command line. Optionally (recommended) use your favourite pipenv / virtualenv to set up a clean environment to run jupyter from.
Then install this module:
pip install jupyter_micropython_remote
Install the kernel into jupyter itself using the shell command:
python -m mpy_kernel.install
This registers the kernel with Jupyter so it can be selected for use in notebooks
Running
Now run Jupyter notebooks:
jupyter notebook
In the notebook click the New notebook button in the upper right, you should see your MicroPython kernel display name listed.
The first cell will need to be something like:
%connect <device> --baudrate=115200 --user='micro' --password='python' --wait=0
eg:
%connect "USB-SERIAL CH340""
or something that matches the serial port that you connect to your MicroPython/ESP8266 with.
The <device> and args matches the command used to run the standard pyboard.py:
device can be serial port device or name device can start with "exec:" "Execute a process and emulate serial connection using its stdin/stdout." device can start with "execpty:" Execute a process which creates a PTY and prints slave PTY as first line of its output, and emulate serial connection using this PTY device can be an ip address for webrepl communication
You should now be able to execute MicroPython commands by running the cells.
There is a micropythondemo.ipynb file in the directory you could look at with some of the features shown.
If a cell is taking too long to interrupt, it may respond to a “Kernel” -> “Interrupt” command.
Alternatively hit Escape and then ‘i’ twice.
To do a soft reboot (when you need to clear out the modules and recover some memory) type:
%reboot
You can list all the functions with:
%lsmagic
mprepl
The communications interface to the micropython module is based on mprepl and pyboard. mprepl was originally sourced from https://github.com/micropython/micropython/pull/3034
This module utilises the virtual filesystem within micropython ( > 1.9.4 required ) to mount the local pc’s working directory jupyter was run from in the actual micropython environment at the directory /remote/
This allows you to view, open, read, write and copy files to and from micropython to your pc with ease.
import os print(os.listdir("/remote/")
There is also an injected Util class with some extra file handling tools, culminating with a sync(source, target, delete=True, include=None, exclude=None) which will copy all files/folders from source to target, optionally with include or exclude regex filters.
Util.sync("/remote/src", "/lib/", delete=True, include=".*\.mpy")
See the file mpy_kernel/mprepl_utils.py for more details
%local
Individual cells can also be run on the local pc instead of the remote kernel by starting a cell with %local
This can be useful to work directly with local files, use ipywidgets, etc. Commands here will be run by the standard ipython kernel.
In %local cells, a special global function remote() is also available which will pass a single string argument to the micropython board to be run, returning any stdout from the command. Eg:
micropython cell
from machine import Pin import neopixel pixels = neopixel.NeoPixel(Pin(4, Pin.OUT), 1) def set_colour(r, g, b): pixels[0] = (r, g, b) pixels.write() set_colour(0xff, 0xff, 0xff)
local cell
%local import colorsys from ipywidgets import interact, Layout, FloatSlider def set_hue(hue): r, g, b = (int(p*255) for p in colorsys.hsv_to_rgb(hue, 1.0, 1.0)) remote(f"set_colour({r}, {g}, {b})") slider = FloatSlider(min=0,max=1.0,step=0.01, layout=Layout(width='80%', height='80px')) interact(set_hue, hue=slider)
Contributing
Please use and improve this kernel any way you see fit!
I’d prefer pull requests against the main repo: https://gitlab.com/alelec/jupyter_micropython_remote I’ll happily review and accept anything on the legacy github if you are aren’t already on gitlab: https://github.com/andrewleech/jupyter_micropython_remote
Background
This Jupyter MicroPython Kernel was originally based on the amazing work done on https://github.com/goatchurchprime/jupyter_micropython_remote.git
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file jupyter_micropython_remote-1.2-py3-none-any.whl
.
File metadata
- Download URL: jupyter_micropython_remote-1.2-py3-none-any.whl
- Upload date:
- Size: 24.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | baa441cd5b801b0ac3642ff027b27ca498cbfe9f4112b825e5a0aedd4c44c42d |
|
MD5 | 4898c7d869f108bcfb8bbceed02bd940 |
|
BLAKE2b-256 | e1254eb2b6f9aed7416160dd0b5f3781e35505de2e64491be65d320e731af449 |