CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-mpremote

Tool for interacting remotely with MicroPython devices

Pending
Overview
Eval results
Files

device-connection.mddocs/

Device Connection and Management

Device connection and management functionality for establishing, maintaining, and controlling connections to MicroPython devices over serial interfaces.

Capabilities

Device Connection

Connect to MicroPython devices with automatic port detection, device enumeration, and connection establishment.

def do_connect(state, args=None):
    """
    Connect to MicroPython device.
    
    Parameters:
    - state: State object containing transport and connection info
    - args: Connection arguments with device specification
    
    Device specifications:
    - "auto": Auto-detect first available USB serial device
    - "list": List all available serial ports
    - "id:SERIAL": Connect to device with specific serial number
    - "port:PATH": Connect to specific port path
    - "/dev/ttyUSB0", "COM3", etc.: Direct device path
    """

Usage examples:

from mpremote.main import State
from mpremote.commands import do_connect

# Auto-connect to first available device
state = State()
do_connect(state, type('Args', (), {'device': ['auto']})())

# Connect to specific port
do_connect(state, type('Args', (), {'device': ['/dev/ttyUSB0']})())

# List available devices
do_connect(state, type('Args', (), {'device': ['list']})())

Device Disconnection

Safely disconnect from the current MicroPython device and clean up transport resources.

def do_disconnect(state, _args=None):
    """
    Disconnect from current device.
    
    Parameters:
    - state: State object containing active transport
    """

Device Reset Operations

Perform device reset operations to restart MicroPython without power cycling.

def do_soft_reset(state, _args=None):
    """
    Perform soft reset of the MicroPython device.
    
    Parameters:
    - state: State object with active transport connection
    
    Sends Ctrl-C + Ctrl-D sequence to restart MicroPython interpreter.
    """

Command Timing Control

Control timing between command execution with programmable delays.

def do_sleep(state, args):
    """
    Sleep/delay before executing next command.
    
    Parameters:
    - state: State object (not used for sleep)
    - args: Sleep duration arguments
    
    Args attributes:
    - ms: List containing sleep duration in seconds (float)
    
    Useful for timing control in automated scripts.
    """

Session Management

Manage connection sessions and resume previous connections without automatic resets.

def do_resume(state, _args=None):
    """
    Resume previous mpremote session without auto soft-reset.
    
    Parameters:
    - state: State object for session management
    
    Useful for continuing work on a device without interrupting running code.
    """

Command-Line Interface

Connection Commands

# Auto-connect to first available device
mpremote connect auto
mpremote  # auto is default

# List available serial devices
mpremote connect list
mpremote devs  # shortcut

# Connect to specific device
mpremote connect /dev/ttyUSB0
mpremote connect COM3
mpremote connect id:334D335C3138
mpremote connect port:/dev/ttyACM0

# Device shortcuts (built-in)
mpremote a0  # /dev/ttyACM0
mpremote u0  # /dev/ttyUSB0  
mpremote c0  # COM0

Session Management Commands

# Soft reset device
mpremote soft-reset

# Resume session without reset
mpremote resume

# Disconnect
mpremote disconnect

# Add delays between commands
mpremote connect auto sleep 2.5 exec "print('After delay')"

Error Handling

Connection operations may raise the following exceptions:

  • TransportError: Device connection failures, permission issues, device not found
  • CommandError: Invalid device specifications or connection parameters

Common error scenarios:

from mpremote.transport import TransportError
from mpremote.commands import CommandError

try:
    do_connect(state, args)
except TransportError as e:
    if "no device found" in str(e):
        print("No MicroPython devices detected")
    elif "failed to access" in str(e):
        print("Permission denied - check device permissions")
    else:
        print(f"Connection failed: {e}")
except CommandError as e:
    print(f"Invalid connection parameters: {e}")

Device Detection

mpremote automatically detects MicroPython devices by:

  1. Enumerating all available serial ports
  2. Filtering for USB devices with valid VID/PID combinations
  3. Attempting connection with standard MicroPython baud rates
  4. Verifying MicroPython REPL response

Supported device types include:

  • Raspberry Pi Pico and Pico W
  • ESP32 and ESP8266 boards
  • PyBoard and PyBoard D
  • Generic MicroPython devices over USB serial

Install with Tessl CLI

npx tessl i tessl/pypi-mpremote

docs

cli-reference.md

code-execution.md

device-config.md

device-connection.md

filesystem.md

index.md

mounting.md

package-management.md

repl.md

romfs.md

tile.json