| name | micropython-repl |
| description | MicroPython REPL usage, package management, module inspection, and interactive debugging for Universe 2025 (Tufty) Badge. Use when installing MicroPython packages, testing code interactively, checking installed modules, or using the REPL for development. |
MicroPython REPL and Package Management
Master the MicroPython REPL (Read-Eval-Print Loop) for interactive development, package management, and quick testing on the Universe 2025 (Tufty) Badge.
Connecting to REPL
Using screen (macOS/Linux)
ls /dev/tty.usb*
screen /dev/tty.usbmodem* 115200
Using mpremote
pip install mpremote
mpremote connect /dev/tty.usbmodem*
mpremote
Using Thonny IDE
- Open Thonny
- Tools → Options → Interpreter
- Select "MicroPython (RP2040)"
- Choose correct port
- Shell window shows REPL
REPL Basics
Special Commands
help()
help(modules)
help(badgeware)
import sys
sys.implementation
sys.platform
Interactive Testing
>>> from badgeware import screen, display, brushes
>>> screen.brush = brushes.color(255, 255, 255)
>>> screen.text("Test", 10, 10, 2)
>>> display.update()
>>> temp = 23.5
>>> temp_f = temp * 9/5 + 32
>>> print(f"{temp}C = {temp_f}F")
>>> from machine import Pin
>>> led = Pin(25, Pin.OUT)
>>> led.toggle()
Paste Mode for Multi-line Code
def calculate_distance(x1, y1, x2, y2):
import math
dx = x2 - x1
dy = y2 - y1
return math.sqrt(dx*dx + dy*dy)
print(calculate_distance(0, 0, 3, 4))
Package Management
Using mip (MicroPython Package Installer)
import mip
mip.install("urequests")
mip.install("logging")
mip.install("umqtt.simple")
mip.install("github:org/repo/package.py")
mip.install("urequests", target="/lib")
Using upip (older method)
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('SSID', 'password')
while not wlan.isconnected():
pass
import upip
upip.install("micropython-logging")
upip.install("picoweb")
Manual Package Installation
mpremote cp mymodule.py :/lib/mymodule.py
ampy --port /dev/tty.usbmodem* put mymodule.py /lib/mymodule.py
>>> import mymodule
Module Inspection
List Available Modules
help('modules')
import sys
'badgeware' in sys.modules
import badgeware
'badgeware' in sys.modules
Inspect Module Contents
import badgeware
dir(badgeware)
hasattr(badgeware, 'screen')
hasattr(badgeware, 'brushes')
help(badgeware.screen)
from badgeware import shapes
dir(shapes)
import inspect
inspect.getsource(mymodule.myfunction)
Check Module Location
import badgeware
badgeware.__file__
import os
os.listdir('/system')
os.listdir('/system/apps')
Interactive Debugging
Quick Variable Inspection
>>> x = [1, 2, 3, 4, 5]
>>> len(x)
5
>>> type(x)
<class 'list'>
>>> sum(x)
15
>>> import badgeware
>>> type(badgeware.screen)
<class 'Screen'>
>>> dir(badgeware.screen)
>>> dir(badgeware.brushes)
>>> dir(badgeware.shapes)
Print Debugging
def process_data(data):
print(f"Input: {data}")
result = data * 2
print(f"Result: {result}")
return result
>>> process_data(5)
Input: 5
Result: 10
10
Exception Handling
>>> try:
... 1 / 0
... except ZeroDivisionError as e:
... print(f"Error: {e}")
Error: division by zero
import sys
try:
buggy_function()
except Exception as e:
sys.print_exception(e)
Memory Debugging
import gc
gc.collect()
gc.mem_free()
gc.mem_alloc()
before = gc.mem_free()
after = gc.mem_free()
print(f"Memory used: {before - after} bytes")
Useful REPL Helpers
Create a helpers.py file
import gc
import sys
import os
from machine import Pin, freq
def info():
"""Display system information"""
print(f"Platform: {sys.platform}")
print(f"Version: {sys.version}")
print(f"CPU Frequency: {freq()} Hz")
print(f"Free Memory: {gc.mem_free()} bytes")
def ls(path='/'):
"""List files in directory"""
try:
files = os.listdir(path)
for f in files:
print(f)
except:
print(f"Error listing {path}")
def cat(filename):
"""Display file contents"""
try:
with open(filename, 'r') as f:
print(f.read())
except Exception as e:
print(f"Error: {e}")
def rm(filename):
"""Remove file"""
try:
os.remove(filename)
print(f"Removed {filename}")
except Exception as e:
print(f"Error: {e}")
def blink(pin=25, times=3):
"""Blink LED for testing"""
import time
led = Pin(pin, Pin.OUT)
for i in range(times):
led.toggle()
time.sleep(0.5)
led.value(0)
Auto-run at REPL start
Create boot.py to run code on startup:
import gc
gc.collect()
print("Universe 2025 Badge Ready!")
print(f"Free memory: {gc.mem_free()} bytes")
Quick Testing Workflows
Test Hardware Function
from machine import I2C, Pin
i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=400000)
devices = i2c.scan()
print(f"Found devices: {[hex(d) for d in devices]}")
from badgeware import screen, display, brushes
screen.brush = brushes.color(0, 0, 0)
screen.clear()
screen.brush = brushes.color(255, 255, 255)
screen.text("REPL Test", 10, 10, 2)
display.update()
Test Network Connection
import network
import time
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
print("Connecting to WiFi...")
wlan.connect('YOUR_SSID', 'YOUR_PASSWORD')
timeout = 10
while not wlan.isconnected() and timeout > 0:
print(".", end="")
time.sleep(1)
timeout -= 1
if wlan.isconnected():
print("\nConnected!")
print(f"IP: {wlan.ifconfig()[0]}")
else:
print("\nConnection failed")
Test API Call
import urequests
import json
response = urequests.get('https://api.github.com/zen')
print(response.text)
response.close()
response = urequests.get('https://api.example.com/data')
data = response.json()
print(data)
response.close()
File System Operations
List and Navigate Files
import os
os.getcwd()
os.listdir()
os.listdir('/lib')
os.stat('main.py')
try:
os.stat('main.py')
print("File exists")
except:
print("File not found")
Read/Write Files
with open('test.txt', 'w') as f:
f.write('Hello from REPL!\n')
with open('test.txt', 'r') as f:
content = f.read()
print(content)
with open('log.txt', 'a') as f:
f.write(f'Log entry: {time.time()}\n')
Remove Files
import os
os.remove('test.txt')
os.rmdir('mydir')
Checking Installed Packages
Verify Package Installation
try:
import urequests
print("urequests is installed")
print(f"Location: {urequests.__file__}")
except ImportError:
print("urequests not installed")
import os
lib_files = os.listdir('/lib')
print("Installed in /lib:")
for f in lib_files:
print(f" {f}")
import sys
print("Module search paths:")
for path in sys.path:
print(f" {path}")
Package Version Info
import urequests
if hasattr(urequests, '__version__'):
print(f"urequests version: {urequests.__version__}")
import sys
print(sys.version)
print(sys.implementation)
REPL Tips and Tricks
History Navigation
- Up/Down arrows: Navigate command history
- Tab: Auto-completion (limited support)
Copy Output from REPL
>>> result = []
>>> for i in range(10):
... result.append(i * 2)
>>> print(result)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Run Script from REPL
import myapp
myapp.main()
import sys
if 'myapp' in sys.modules:
del sys.modules['myapp']
import myapp
Timing Code
import time
start = time.ticks_ms()
for i in range(1000):
x = i * 2
end = time.ticks_ms()
print(f"Execution time: {time.ticks_diff(end, start)}ms")
Common REPL Issues
REPL not responding: Press Ctrl+C to interrupt, or Ctrl+D to soft reset
Can't import module: Check sys.path, verify file is in /lib or root directory
Out of memory: Run gc.collect(), reduce variable usage, delete large objects
Module changes not reflected: Delete from sys.modules and re-import
Connection lost: Reconnect with screen or mpremote, check USB cable
Using mpremote for Quick Operations
mpremote exec "import machine; print(machine.freq())"
mpremote run test.py
mpremote cp test.py :main.py
mpremote cp :main.py local.py
mpremote mount .
mpremote ls
mpremote mkdir /data
mpremote rm test.txt
mpremote connect /dev/tty.usbmodem* cp main.py :main.py exec "import main"
REPL Best Practices
- Save work frequently - REPL state is lost on reboot
- Use paste mode for multi-line code
- Run
gc.collect() before memory-intensive operations
- Test incrementally - Build up complex code piece by piece
- Create helper functions in a dedicated module
- Use
print() liberally for debugging
- Soft reset (Ctrl+D) to clear state between tests
- Keep REPL sessions short - Move working code to files
Integration with Development Workflow
- Prototype in REPL - Test ideas interactively
- Save to file - Once code works, save to .py file
- Test from file - Import and run from REPL
- Iterate - Make changes, reload, test
- Deploy - Upload final version to badge
The REPL is your best friend for rapid experimentation and debugging on the Badger 2350!