.. _mpremote:
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``:
.. code-block:: bash
$ pip install --user mpremote
Or via `pipx `_:
.. code-block:: bash
$ pipx install mpremote
The simplest way to use this tool is just by invoking it without any arguments:
.. code-block:: bash
$ mpremote
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:
.. code-block:: bash
$ 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 :ref:`examples section ` below to get an idea of how
this works and for some common combinations of commands.
Each command is of the form `` [--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 :ref:`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:
- `connect `
- `disconnect `
- `resume `
- `soft_reset `
- `repl `
- `eval `
- `exec `
- `run `
- `fs `
- `df `
- `edit `
- `mip `
- `mount `
- `unmount `
- `romfs `
- `rtc `
- `sleep `
- `reset `
- `bootloader `
.. _mpremote_command_connect:
- **connect** -- connect to specified device via name:
.. code-block:: bash
$ mpremote connect
```` may be one of:
- ``list``: list available devices
- ``auto``: connect to the first available USB serial port
- ``id:``: connect to the device with USB serial number
```` (the second column from the ``connect list``
command output)
- ``port:``: connect to the device with the given path (the first column
from the ``connect list`` command output
- ``rfc2217://:``: connect to the device using serial over TCP
(e.g. a networked serial port based on RFC2217)
- any valid device name/path, to connect to that device
**Note:** Instead of using the ``connect`` command, there are several
:ref:`pre-defined shortcuts ` for common device paths. For
example the ``a0`` shortcut command is equivalent to
``connect /dev/ttyACM0`` (Linux), or ``c0`` for ``COM0`` (Windows).
**Note:** The ``auto`` option will only detect USB serial ports, i.e. a serial
port that has an associated USB VID/PID (i.e. CDC/ACM or FTDI-style
devices). Other types of serial ports will not be auto-detected.
.. _mpremote_command_disconnect:
- **disconnect** -- disconnect current device:
.. code-block:: bash
$ mpremote disconnect
After a disconnect, :ref:`auto-soft-reset ` is enabled.
.. _mpremote_command_resume:
- **resume** -- maintain existing interpreter state for subsequent commands:
.. code-block:: bash
$ mpremote resume
This disables :ref:`auto-soft-reset `. This is useful if you
want to run a subsequent command on a board without first soft-resetting it.
.. _mpremote_command_soft_reset:
- **soft-reset** -- perform a soft-reset of the device:
.. code-block:: bash
$ mpremote soft-reset
This will clear out the Python heap and restart the interpreter. It also
prevents the subsequent command from triggering :ref:`auto-soft-reset `.
.. _mpremote_command_repl:
- **repl** -- enter the REPL on the connected device:
.. code-block:: bash
$ mpremote repl [--options]
Options are:
- ``--escape-non-printable``, to print non-printable bytes/characters as their hex code
- ``--capture ``, to capture output of the REPL session to the given
file
- ``--inject-code ``, to specify characters to inject at the REPL when
``Ctrl-J`` is pressed. This allows you to automate a common command.
- ``--inject-file ``, to specify a file to inject at the REPL when
``Ctrl-K`` is pressed. This allows you to run a file (e.g. containing some
useful setup code, or even the program you are currently working on).
While the ``repl`` command running, you can use ``Ctrl-]`` or ``Ctrl-x`` to
exit.
**Note:** The name "REPL" here reflects that the common usage of this command
to access the Read Eval Print Loop that is running on the MicroPython
device. Strictly, the ``repl`` command is just functioning as a terminal
(or "serial monitor") to access the device. Because this command does not
trigger the :ref:`auto-reset behavior `, this means that if
a program is currently running, you will first need to interrupt it with
``Ctrl-C`` to get to the REPL, which will then allow you to access program
state. You can also use ``mpremote soft-reset repl`` to get a "clean" REPL
with all program state cleared.
.. _mpremote_command_eval:
- **eval** -- evaluate and print the result of a Python expression:
.. code-block:: bash
$ mpremote eval
.. _mpremote_command_exec:
- **exec** -- execute the given Python code:
.. code-block:: bash
$ mpremote exec
By default, ``mpremote exec`` will display any output from the expression until it
terminates. The ``--no-follow`` flag can be specified to return immediately and leave
the device running the expression in the background.
.. _mpremote_command_run:
- **run** -- run a script from the local filesystem:
.. code-block:: bash
$ mpremote run
This will execute the file directly from RAM on the device without copying it
to the filesystem. This is a very useful way to iterate on the development of
a single piece of code without having to worry about deploying it to the
filesystem.
By default, ``mpremote run`` will display any output from the script until it
terminates. The ``--no-follow`` flag can be specified to return immediately and leave
the device running the script in the background.
.. _mpremote_command_fs:
- **fs** -- execute filesystem commands on the device:
.. code-block:: bash
$ mpremote fs
```` may be:
- ``cat `` to show the contents of a file or files on the device
- ``ls`` to list the current directory
- ``ls `` to list the given directories
- ``cp [-rf] `` to copy files
- ``rm [-r] `` to remove files or folders on the device
- ``mkdir `` to create directories on the device
- ``rmdir `` to remove directories on the device
- ``touch `` to create the files (if they don't already exist)
- ``sha256sum `` to calculate the SHA256 sum of files
- ``tree [-vsh] `` to print a tree of the given directories
The ``cp`` command uses a convention where a leading ``:`` represents a remote
path. Without a leading ``:`` means a local path. This is based on the
convention used by the `Secure Copy Protocol (scp) client
`_.
So for example, ``mpremote fs cp main.py :main.py`` copies ``main.py`` from
the current local directory to the remote filesystem, whereas
``mpremote fs cp :main.py main.py`` copies ``main.py`` from the device back
to the current directory.
The ``mpremote rm -r`` command accepts both relative and absolute paths.
Use ``:`` to refer to the current remote working directory (cwd) to allow a
directory tree to be removed from the device's default path (eg ``/flash``, ``/``).
Use ``-v/--verbose`` to see the files being removed.
For example:
- ``mpremote rm -r :libs`` will remove the ``libs`` directory and all its
child items from the device.
- ``mpremote rm -rv :/sd`` will remove all files from a mounted SDCard and result
in a non-blocking warning. The mount will be retained.
- ``mpremote rm -rv :/`` will remove all files on the device, including any
located in mounted vfs such as ``/sd`` or ``/flash``. After removing all folders
and files, this will also return an error to mimic unix ``rm -rf /`` behaviour.
.. warning::
There is no supported way to undelete files removed by ``mpremote rm -r :``.
Please use with caution.
The ``tree`` command will print a tree of the given directories.
Using the ``--size/-s`` option will print the size of each file, or use
``--human/-h`` to use a more human readable format.
Note: Directory size is only printed when a non-zero size is reported by the device's filesystem.
The ``-v`` option can be used to include the name of the serial device in
the output.
All other commands implicitly assume the path is a remote path, but the ``:``
can be optionally used for clarity.
All of the filesystem sub-commands take multiple path arguments, so if there
is another command in the sequence, you must use ``+`` to terminate the
arguments, e.g.
.. code-block:: bash
$ mpremote fs cp main.py :main.py + repl
This will copy the file to the device then enter the REPL. The ``+`` prevents
``"repl"`` being interpreted as a path.
The ``cp`` command supports the ``-r`` option to make a recursive copy. By
default ``cp`` will skip copying files to the remote device if the SHA256 hash
of the source and destination file matches. To force a copy regardless of the
hash use the ``-f`` option.
**Note:** For convenience, all of the filesystem sub-commands are also
:ref:`aliased as regular commands `, i.e. you can write
``mpremote cp ...`` instead of ``mpremote fs cp ...``.
.. _mpremote_command_df:
- **df** -- query device free/used space
.. code-block:: bash
$ mpremote df
The ``df`` command will print size/used/free statistics for the device
filesystem, similar to the Unix ``df`` command.
.. _mpremote_command_edit:
- **edit** -- edit a file on the device:
.. code-block:: bash
$ mpremote edit
The ``edit`` command will copy each file from the device to a local temporary
directory and then launch your editor for each file (defined by the environment
variable ``$EDITOR``). If the editor exits successfully, the updated file will
be copied back to the device.
.. _mpremote_command_mip:
- **mip** -- install packages from :term:`micropython-lib` (or GitHub) using the ``mip`` tool:
.. code-block:: bash
$ mpremote mip install
See :ref:`packages` for more information.
.. _mpremote_command_mount:
- **mount** -- mount the local directory on the remote device:
.. code-block:: bash
$ mpremote mount [options]
This allows the remote device to see the local host directory as if it were
its own filesystem. This is useful for development, and avoids the need to
copy files to the device while you are working on them.
The device installs a filesystem driver, which is then mounted in the
:ref:`device VFS ` as ``/remote``, which uses the serial
connection to ``mpremote`` as a side-channel to access files. The device
will have its current working directory (via ``os.chdir``) set to
``/remote`` so that imports and file access will occur there instead of the
default filesystem path while the mount is active.
**Note:** If the ``mount`` command is not followed by another action in the
sequence, a ``repl`` command will be implicitly added to the end of the
sequence.
During usage, Ctrl-D will trigger a soft-reset as normal, but the mount will
automatically be re-connected. If the unit has a main.py running at startup
however the remount cannot occur. In this case a raw mode soft reboot can be
used: Ctrl-A Ctrl-D to reboot, then Ctrl-B to get back to normal repl at
which point the mount will be ready.
Options are:
- ``-l``, ``--unsafe-links``: By default an error will be raised if the device
accesses a file or directory which is outside (up one or more directory levels) the
local directory that is mounted. This option disables this check for symbolic
links, allowing the device to follow symbolic links outside of the local directory.
.. _mpremote_command_unmount:
- **unmount** -- unmount the local directory from the remote device:
.. code-block:: bash
$ mpremote umount
This happens automatically when ``mpremote`` terminates, but it can be used
in a sequence to unmount an earlier mount before subsequent command are run.
.. _mpremote_command_romfs:
- **romfs** -- manage ROMFS partitions on the device:
.. code-block:: bash
$ mpremote romfs
```` may be:
- ``romfs query`` to list all the available ROMFS partitions and their size
- ``romfs [-o