Embedded 101: VSCode + Linux Containerized MicroPython Dev Env | mpremote Practice
It introduces constructing MicroPython environments on Linux via VSCode Dev Containers and local setups, utilizing Docker and mpremote for board debugging.
【Preliminary Note】The original hardware example in this article was written based on the RP2040. The actual hardware used in this hands-on demonstration features the W55RP20 as the main controller chip. The circuit logic and UF2 flashing operation principles are universally applicable, with only the main controller model differing. The original chip model mentioned in the circuit descriptions below is provided for reference purposes only.
Building MicroPython Development Environment Based on VSCode in Linux Environment
1. Construction of MicroPython Development Environment on Linux (Container-based)
VSCode with Dev Container (a containerized Development Environment) for MicroPython development. The container communicates with physical boards (such as /dev/ttyACM0, /dev/ttyUSB0, etc.) via mpremote, and the code remains synchronized with the host machine's folder.Docker + VSCode Dev Containers, and perform joint debug with the device via mpremote, which enables a reproducible, consistent and low-maintenance workflow: team members do not need to install complex dependencies on their respective hosts, and can directly start the container to obtain the same toolchain (mpremote, Python, mpy-cross, etc.);Containerization also isolates device access, permissions, and dependencies to prevent contamination of the host system. Meanwhile, it enables real-time synchronization between code and devices by mounting the workspace, greatly accelerating iteration and debugging between the host machine and the target board, and facilitating quick onboarding for beginners and stable delivery.Docker provides a runtime environment for containers; by default, containers are isolated from the host, so you need to explicitly expose devices (such as /dev/ttyACM0) to the container or configure them in devcontainer. json.dialout group; adding a user to this group grants direct access to the serial port.linux group, you usually need to log out/reboot or use newgrp to activate the group permissions.mpremote (recommended) is a modern tool for interacting with MicroPython devices, supporting common operations such as REPL, file upload, and mount.sudo apt update
sudo apt install -y docker.io git
# 可选:如果没有 VSCode,请先到官网安装 .deb 后用 apt 安装,或用你的发行版包管理器安装ls -l /dev/ttyACM0
# 例如输出:crw-rw---- 1 root dialout 166, 0 9月 3日 15:52 /dev/ttyACM0docker and dialout groups:sudo usermod -aG docker $USER
sudo usermod -aG dialout $USERnewgrp for temporary activation (valid for the current shell):newgrp docker
newgrp dialoutVSCode: Dev Containers (search for and install it from the Marketplace), then place the https://codeberg.org/eoelab/cenv/src/branch/main/Denv/upython directory. devcontainer into your workspace.VSCode menu, select: Remote-Containers: Open Folder in Container. ..(or select "Open in Container" in the lower right corner):Dev Containers:VSCode editing window just as you would on the host machine. Common mpremote commands:# 显示 mpremote 版本,检查是否可用
mpremote --version
# 连接到设备并打开 REPL(替换为你的设备)
mpremote connect /dev/ttyACM0 repl
# 在设备上运行单个文件(无须上传)
mpremote connect /dev/ttyACM0 run main.py
# 直接上传文件到设备
mpremote connect /dev/ttyACM0 fs put main.py :/main.py
# 挂载主机目录到设备(适合快速迭代)
mpremote connect /dev/ttyACM0 mount . run main.py
# 终止当前任务:Ctrl+C(在命令行中)devcontainer. json has already mapped the device to the container, mpremote can directly access the device by using /dev/ttyACM0 inside the container. If no mapping is configured, mpremote will prompt a device not found error or a permission error.mpremote > Run file on remote device to execute it:mpremote mount. run main. py to get started:Ctrl+ C to terminate the current task. You can switch the file to be executed at any time simply by running the command again, no extra copying is required, and it stays in sync with the host files, making it ideal for development scenarios.