Tool for interacting remotely with MicroPython devices
—
Device connection and management functionality for establishing, maintaining, and controlling connections to MicroPython devices over serial interfaces.
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']})())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
"""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.
"""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.
"""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.
"""# 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# 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')"Connection operations may raise the following exceptions:
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}")mpremote automatically detects MicroPython devices by:
Supported device types include:
Install with Tessl CLI
npx tessl i tessl/pypi-mpremote