micropython / micropython/micropython

mpremote option to wait until device connected and/or reconnect

Open
#16,360 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement tools
Dominant language
C
Stars
22.1k
Forks
9k
Avg merge
6d 4h
Merged PRs (30d)
16

Description

Description

For devices that provide a REPL over USB/VCP, it can be tricky to get the initial set of data send over the terminal using mpremote (or other serial terminal), as the serial device doesn't exist until the device is plugged in (as compared to a terminal over UART, which can be constant connected to the host computer and monitored across reboots).

If mpremote can have an option to monitor the USB system, waiting for a device connection/appearing, then immediately connect the serial terminal, it could make debugging/developing early-boot situations. Could also set up a re-connect option to persist an mpremote session across hard reboots.

The following code works for this purpose, and could be adapted relatively easily (for linux hosts at least) to include more specific USB identifiers.

# borrowed from **https://unix.stackexchange.com/questions/65891/how-to-execute-a-shellscript-when-i-plug-in-a-usb-device
import pyudev
import subprocess
import sys
from functools import partial

def main():
    context = pyudev.Context()
    monitor = pyudev.Monitor.from_netlink(context)
    monitor.filter_by(subsystem='usb')
    monitor.start()

    while True:
        # avoid too many invocations of the external command by waiting until
        # no other events occur and we timeout
        device = monitor.poll(timeout=None)
        for device in iter(partial(monitor.poll, 1), None):
            pass
        subprocess.call(['mpremote'])
Code Size

No response

Implementation

I intend to implement this feature and would submit a Pull Request if desirable

Code of Conduct

Yes, I agree

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names mpremote as the entry point but no repository files or tests. Start by locating mpremote's connection and terminal handling, then review the supplied pyudev example; define the wait and reconnect behavior, supported hosts, and tests needed to show that a session connects after a device appears and survives reboots.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli, embedded-iot
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.