mpremote — MicroPython latest documentation (original) (raw)

MicroPython remote control: mpremote

The mpremote command line tool provides an integrated set of utilities to remotely interact with, manage the filesystem on, and automate a MicroPython device over a serial connection.

To use mpremote, first install it via pip:

$ pip install --user mpremote

Or via pipx:

The simplest way to use this tool is just by invoking it without any arguments:

This command automatically detects and connects to the first available USB serial device and provides an interactive terminal that you can use to access the REPL and your program’s output. Serial ports are opened in exclusive mode, so running a second (or third, etc) instance of mpremote will connect to subsequent serial devices, if any are available.

Additionally pipx also allows you to directly run mpremote without installing first:

$ pipx run mpremote ...args

Commands

mpremote supports being given a series of commands given at the command line which will perform various actions in sequence on a remote MicroPython device. See the examples section below to get an idea of how this works and for some common combinations of commands.

Each command is of the form <command name> [--options] [args...]. For commands that support multiple arguments (e.g. a list of files), the argument list can be terminated with +.

If no command is specified, the default command is repl. Additionally, if any command needs to access the device, and no earlier connect has been specified, then an implicit connect auto is added.

In order to get the device into a known state for any action command (except repl), once connected mpremote will stop any running program and soft-reset the device before running the first command. You can control this behavior using the resume and soft-reset commands. See auto-connection and auto-soft-reset for more details.

Multiple commands can be specified and they will be run sequentially.

The full list of supported commands are:

Auto connection and soft-reset

Connection and disconnection will be done automatically at the start and end of the execution of the tool, if such commands are not explicitly given. Automatic connection will search for the first available USB serial device.

Once connected to a device, mpremote will automatically soft-reset the device if needed. This clears the Python heap and restarts the interpreter, making sure that subsequent Python code executes in a fresh environment. Auto soft-reset is performed the first time one of the following commands are executed: mount, eval, exec, run, fs. After doing a soft-reset for the first time, it will not be done again automatically, until adisconnect command is issued.

Auto-soft-reset behaviour can be controlled by the resume command. This might be useful to use the eval command to inspect the state of of the device. The soft-reset command can be used to perform an explicit soft reset in the middle of a sequence of commands.

Shortcuts

Shortcuts can be defined using the macro system. Built-in shortcuts are:

Additional shortcuts can be defined by in user-configuration files, which is located at .config/mpremote/config.py. This file should define a dictionary named commands. The keys of this dictionary are the shortcuts and the values are either a string or a list-of-strings:

"c33": "connect id:334D335C3138",

The command c33 is replaced by connect id:334D335C3138.

"test": ["mount", ".", "exec", "import test"],

The command test is replaced by mount . exec "import test".

Shortcuts can also accept arguments. For example:

"multiply x=4 y=7": "eval x*y",

Running mpremote times 3 7 will set x and y as variables on the device, then evaluate the expression x*y.

An example config.py might look like:

commands = { "c33": "connect id:334D335C3138", # Connect to a specific device by ID. "bl": "bootloader", # Shorter alias for bootloader. "double x=4": "eval x*2", # x is an argument, with default 4 "wl_scan": ["exec", """ import network wl = network.WLAN() wl.active(1) for ap in wl.scan(): print(ap) """,], # Print out nearby WiFi networks. "wl_ipconfig": [ "exec", "import network; sta_if = network.WLAN(network.WLAN.IF_STA); print(sta_if.ipconfig('addr4'))", """,], # Print ip address of station interface. "test": ["mount", ".", "exec", "import test"], # Mount current directory and run test.py. "demo": ["run", "path/to/demo.py"], # Execute demo.py on the device. }

Examples

Connect to the first available device and implicitly run the repl command.

Connect to the device at /dev/ttyACM1 (Linux) and implicitly run therepl command. See shortcuts above.

Connect to the device at COM1 (Windows) and implicitly run the replcommand. See shortcuts above.

mpremote connect /dev/ttyUSB0

Explicitly specify which device to connect to, and as above, implicitly run therepl command.

Connect to the device at /dev/ttyACM0 and then run the ls command.

It is equivalent to mpremote connect /dev/ttyACM1 fs ls.

mpremote exec "import micropython; micropython.mem_info()"

Run the specified Python command and display any output. This is equivalent to typing the command at the REPL prompt.

mpremote eval 1/2 eval 3/4

Evaluate each expression in turn and print the results.

mpremote a0 eval 1/2 a1 eval 3/4

Evaluate 1/2 on the device at /dev/ttyACM0, then 3/4 on the device at /dev/ttyACM1, printing each result.

mpremote resume exec "print_state_info()" soft-reset

Connect to the device without triggering a soft reset and execute the print_state_info() function (e.g. to find out information about the current program state), then trigger a soft reset.

mpremote reset sleep 0.5 bootloader

Hard-reset the device, wait 500ms for it to become available, then enter the bootloader.

mpremote cp utils/driver.py :utils/driver.py + run test.py

Update the copy of utils/driver.py on the device, then execute the localtest.py script on the device. test.py is never copied to the device filesystem, rather it is run from RAM.

mpremote cp utils/driver.py :utils/driver.py + exec "import app"

Update the copy of utils/driver.py on the device, then execute app.py on the device.

This is a common development workflow to update a single file and then re-start your program. In this scenario, your main.py on the device would also doimport app.

mpremote cp utils/driver.py :utils/driver.py + soft-reset repl

Update the copy of utils/driver.py on the device, then trigger a soft-reset to restart your program, and then monitor the output via the repl command.

mpremote cp -r utils/ :utils/ + soft-reset repl

Same as above, but update the entire utils directory first.

Mount the current local directory at /remote on the device and starts arepl session which will use /remote as the working directory.

mpremote mount . exec "import demo"

After mounting the current local directory, executes demo.py from the mounted directory.

mpremote mount app run test.py

After mounting the local directory app as /remote on the device, executes the local test.py from the host’s current directory without copying it to the filesystem.

mpremote mount . repl --inject-code "import demo"

After mounting the current local directory, executes demo.py from the mounted directory each time Ctrl-J is pressed.

You will first need to press Ctrl-D to reset the interpreter state (which will preserve the mount) before pressing Ctrl-J to re-importdemo.py.

mpremote mount app repl --inject-file demo.py

Same as above, but executes the contents of the local file demo.py at the REPL every time Ctrl-K is pressed. As above, use Ctrl-D to reset the interpreter state first.

Displays the contents of boot.py on the device.

mpremote edit utils/driver.py

Edit utils/driver.py on the device using your local $EDITOR.

Copy main.py from the device to the local directory.

Copy main.py from the local directory to the device.

Copy a.py on the device to b.py on the device.

Recursively copy the local directory dir to the remote device.

mpremote cp a.py b.py : + repl

Copy a.py and b.py from the local directory to the device, then run therepl command.

mpremote mip install aioble

Install the aioble package from micropython-lib to the device. See Package management.

mpremote mip install github:org/repo@branch

Install the package from the specified branch at org/repo on GitHub to the device. See Package management.

mpremote mip install gitlab:org/repo@branch

Install the package from the specified branch at org/repo on GitLab to the device. See Package management.

mpremote mip install --target /flash/third-party functools

Install the functools package from micropython-lib to the/flash/third-party directory on the device. See Package management.