CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-mpremote

Tool for interacting remotely with MicroPython devices

Pending
Overview
Eval results
Files

device-config.mddocs/

Device Configuration

Configure MicroPython device settings including real-time clock synchronization and system-level device configuration for optimal operation.

Capabilities

Real-Time Clock Management

Get and set the device real-time clock to synchronize with host system time.

def do_rtc(state, args):
    """
    Get or set device real-time clock.
    
    Parameters:
    - state: State object with active transport
    - args: RTC command arguments
    
    Args attributes:
    - set: Boolean flag to set RTC to current local time
    
    Without --set flag: displays current device RTC time
    With --set flag: synchronizes device RTC with host system time
    """

Command-Line Interface

Real-Time Clock Operations

# Get current device RTC time
mpremote rtc

# Set device RTC to current system time
mpremote rtc --set

# RTC operations with device connection
mpremote connect /dev/ttyUSB0 rtc --set

Usage Examples

RTC Synchronization

from mpremote.main import State
from mpremote.commands import do_rtc

# Set up connected device
state = State()
# ... connect to device ...

# Get current RTC time
args = type('Args', (), {'set': False})()
do_rtc(state, args)

# Set RTC to system time
args = type('Args', (), {'set': True})()
do_rtc(state, args)

System Time Synchronization

# Check current device time
mpremote exec "
import time
print('Device time:', time.localtime())
"

# Synchronize with host system
mpremote rtc --set

# Verify synchronization
mpremote exec "
import time
print('Synchronized time:', time.localtime())
"

Advanced Configuration

Automated Time Synchronization

# Synchronize time at device startup
mpremote rtc --set exec "
import time
print('Device time synchronized:', time.localtime())
"

# Verify time synchronization with fallback
mpremote exec "
import time
try:
    import ntptime  # if available
    ntptime.settime()
    print('NTP sync successful')
except:
    print('Using RTC time:', time.localtime())
"

Time-Based Operations

# Set up scheduled tasks based on RTC
mpremote rtc --set exec "
import time
import machine

def scheduled_task():
    current_time = time.localtime()
    print(f'Task executed at: {current_time}')
    # Perform scheduled operations

# Set up timer for periodic execution
timer = machine.Timer(-1)
timer.init(period=60000, mode=machine.Timer.PERIODIC, callback=lambda t: scheduled_task())
"

Error Handling

Device configuration operations may encounter various errors:

from mpremote.transport import TransportError, TransportExecError

try:
    do_rtc(state, args)
except TransportExecError as e:
    if "RTC not supported" in e.error_output:
        print("Device does not support RTC")
    elif "time sync failed" in e.error_output:
        print("Time synchronization failed")
    else:
        print(f"RTC operation failed: {e.error_output}")
except TransportError as e:
    print(f"Communication error: {e}")

Common Error Scenarios

# RTC not supported
mpremote rtc --set
# Error: RTC functionality not available on this device

# Communication timeout
mpremote rtc
# Error: Device communication timeout

Best Practices

RTC Management

# Always synchronize time after connecting to a new device
mpremote connect auto rtc --set

# Verify time synchronization before time-critical operations
mpremote rtc exec "
import time
current_time = time.localtime()
if current_time[0] < 2020:  # Check if year seems valid
    print('Warning: RTC may not be synchronized')
else:
    print('RTC appears synchronized')
"

Development Workflow

# Include RTC synchronization in device setup scripts
#!/bin/bash
echo "Setting up MicroPython device..."
mpremote connect auto rtc --set
echo "Device time synchronized"

# Verify device configuration
mpremote exec "
import time
import sys
print('Device time:', time.localtime())
print('Python version:', sys.version)
print('Platform:', sys.platform)
"

Time Zone Considerations

# Note: mpremote sets RTC to host system local time
# For UTC operations, consider timezone handling in device code
mpremote exec "
import time

# Convert local time to UTC offset
local_time = time.localtime()
# Note: MicroPython may not have timezone info
# Handle timezone conversion in application code as needed
print('Local time:', local_time)
"

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