Skip to main content

MicroPython Magic commands for use with Jupyter notebooks and Jupyter Labs

Project description

micropython-magic

PyPI PyPI - Python Version PyPI - License PyPI - Downloads

These Jupyter magic methods allow MicroPython to be used from within any Jupyter Notebook or JupyterLab (formerly IPython Notebook) The magics make use of the mpremote tool to enable communication with the MCUs

This allows:

  • Mixing of Host and MCU Code ( and languages if you wish)
  • Creating graphs of the data captured by MCU sensors
  • create re-uasable sequences ( download/compile firmware - flash firmware - uploade code - run expiriment - same outcome)
  • Create and execute tests that require orchestration across multiple MCUs and hosts
  • Rapid Prototyping
  • Capturing the results and outputs of your expiriments in a consistent way
  • Mixing documentation with code

A few of the possibilities

Live Plot of the cpu temperature Visualize the memory map of the MCU
Memory allocation of the MCU over time

For the source please refer to the samples folder

Installation

  • create and activate a venv python3 -m venv .venv

  • pip install -U "micropython-magic"

  • or install directly into your notbook environment/kernel using the '%pip' magic by running

    • %pip install -U "micropython-magic"

Recommended : install stubs for your MCU of choice

  • Install stubs for MicroPython syntax checking pip install micropython-rp2-stubs (or your port of choise)

Usage

1) Create a notebook

2) Load the magic

%load_ext micropython_magic

This can also be configured once to always load automatically ( see below)

3) add a cell with some code to run on the MCU

# %%micropython  
from machine import Pin
led = Pin(25, Pin.OUT)
led.value(1)

The %%micropython cell magic will instruct Jupyter to run the code on the connected MCU

4) enable code highlighting for MicroPython

%pip install micropython-esp32-stubs==1.20.0.*
# installs the stubs for MicroPython syntax checking (one time install per environment) 
# %%micropython  
from machine import Pin
led = Pin(25, Pin.OUT)
led.value(1)

This allows for syntax highlighting and code completion of MicroPython code. Tested in VSCode with

More Examples

Please refer to the samples folder for more examples

  1. install - install the magic
  2. board_control - basic board control
  3. board_selection. - list connected boards and loop through them
  4. device_info - Get simple access to port, board and hardware and firmware information
  5. WOKWI - Use MicroPython magic with WOKWI as a simulator (no device needed)
  6. Plot rp2 CPU temp - create a plot of the CPU temperature of a rp2040 MCU(bqplot)
  7. Display Memory Map - Micropython memory map visualizer
  8. Plot Memory Usage - plot the memory usage of a Micropython script running on a MCU over time

Automatically load the magic on startup

In order to automatically load the magic on startup, you can add the following to your ipython_config.py file:

  • create a ipython profile
    • ipython profile create

    • add the following to the configuration file (.../.ipython/profile_default/ipython_config.py)

      c = get_config()
      
      c.InteractiveShellApp.extensions = [
          'micropython_magic'
      ]
      

Configuration options

Configuration can be done via the %config magic

%config MicroPythonMagic

    MicroPythonMagic(Magics) options
    ------------------------------
    MicroPythonMagic.loglevel=<UseEnum>
        Choices: any of ['TRACE', 'DEBUG', 'INFO', 'WARNING', 'ERROR']
        Current: <LogLevel.WARNING: 'WARNING'>
    MicroPythonMagic.timeout=<Float>
        Current: 300.0

# example
%config MicroPythonMagic.loglevel = 'TRACE'
  • loglevel : set the loglevel for the magic ( default WARNING)
  • timeout : set the timeout for the mpremote connection ( default 300 seconds - 5 minutes)

Development and contributions

The most welcome contributions are :

  • Testing on different platforms (OS) but also different Jupyter environments ( Jupyter Notebook, JupyterLab, VSCode)
  • Provide additional sample notebooks
  • Help add documentation (preferably in a notebook or .md file)
  • Share this with other people that may be interested in this.

See current status and on Github

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

micropython_magic-0.10.2.tar.gz (25.9 kB view details)

Uploaded Source

Built Distribution

micropython_magic-0.10.2-py3-none-any.whl (27.0 kB view details)

Uploaded Python 3

File details

Details for the file micropython_magic-0.10.2.tar.gz.

File metadata

  • Download URL: micropython_magic-0.10.2.tar.gz
  • Upload date:
  • Size: 25.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.7 Windows/10

File hashes

Hashes for micropython_magic-0.10.2.tar.gz
Algorithm Hash digest
SHA256 5094a986d056968f92cc768c8b6b9b078b6f64e17c0e4f4e00e560a66fa00e1b
MD5 eff41118b5bba83f4ac48ef428cc2f0a
BLAKE2b-256 fcf0a16667c8869e77ee29470caf959b015da5f9f21246e2529503a804aa7b26

See more details on using hashes here.

File details

Details for the file micropython_magic-0.10.2-py3-none-any.whl.

File metadata

File hashes

Hashes for micropython_magic-0.10.2-py3-none-any.whl
Algorithm Hash digest
SHA256 29c0697cd59ebc35e8e0292f7bb3917861943893b294133c35837fa4158e5217
MD5 52e5e50a931da607c5f37cbb8b150566
BLAKE2b-256 f81b620f5428ed6f857d95890458e4cfb9e4f1088df471612b32d8866573ef8c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page