MicroPython Development Environment Setup
Recently, I got a MicroPython-ESP32 development board. The first thing to do is set up the development environment. I use Manjaro Linux, so here I record the process.
Mu Editor
The board manufacturer recommends Mu Editor as the development tool. So first, I tried to install Mu Editor.
Official website: https://codewith.mu/
Windows installers are provided; just download and install. For Linux, there is no installer; you need to install via Python's pip tool according to the official guide.
To avoid dependency conflicts with the host Python environment, it is recommended to install Mu Editor in a virtual environment. Use virtualenv to create and manage Python virtual environments. If not installed, use pip install virtualenv or pip3 install virtualenv. A virtual environment provides an isolated directory for Python interpreter and third-party packages, avoiding version conflicts.
Installation
First, specify the location for the virtual environment. In this example, it's in the ~/venv directory.
mkdir -p ~/venv
cd ~/venv
Create a virtual environment named mueditor with Python 3.7.
virtualenv mueditor --python=python3.7
Note:
- If the default Python version is higher than 3.7, use 3.7. Higher versions may cause installation errors.
- Ensure
python3.7is in your PATH before creating the virtual environment.
Activate the virtual environment:
source ~/venv/mueditor/bin/activate
After activation, the terminal prompt will show (mueditor), indicating the virtual environment is active (only for the current terminal).
To exit, run
deactivate; the(mueditor)indicator will disappear.
Install mu-editor in the virtual environment using pip. Use the -i flag to specify a domestic mirror (Tsinghua) for faster download, and --default-timeout to avoid timeout issues.
pip install mu-editor -i https://pypi.tuna.tsinghua.edu.cn/simple --default-timeout=100000
Wait for the installation to finish.
Using Python 3.7 is necesary; higher versions may fail to install some dependencies like pygame (requiring version <2.0, >=1.9.2). With Python 3.7, there is no such issue.
After installation, run mu-editor in the activated terminal to launch.
Screenshot:
![mueditor]
Usage
While using Mu Editor, I encountered an unexpected issue: Mu Editor could not detect the MicroPython board (on Windows, when a MicroPython board is connected, the Mode selection window shows a MicroPython option, but not on Linux). So I decided to switch to command-line tools.
Command-Line Tools
Key command-line tools for MicroPython development:
picocom: Serial debug software to access the REPL terminal.adafruit-ampy: Access the MicroPython board's file system.rshell: Remote Shell for MicroPython.
Installation
picocom is a common Linux tool. Install via package manager:
# Ubuntu / Debian
sudo apt install picocom
# Arch / Manjaro
sudo pacman -S picocom
adafruit-ampy and rshell are Python packages. Install via pip (preferably in a virtual environment):
pip install adafruit-ampy rshell -i https://pypi.tuna.tsinghua.edu.cn/simple --default-timeout=100000
picocom Serial Debug
Connect to the MicroPython board's serial port. The baud rate is typically 115200 (check your board's documentation). In this example, the serial port is /dev/ttyUSB0.
View help: picocom -h
Start picocom:
picocom /dev/ttyUSB0 -b 115200
While in picocom, press Ctrl+A Ctrl+H for help, Ctrl+A Ctrl+Q to quit.
Press the reset button on the board. If the firmware boots normally, you may see output like:
ets Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
...
MicroPython v1.11 on 2019-05-29; ESP32 module with ESP32
Type "help()" for more information.
>>>
If the user program main.py is running, press Ctrl+C to stop it and get the REPL prompt.
ampy File System Access
The MicroPython board has a small file system. On some boards, it cannot be accessed as a removable drive; instead, use the adafruit-ampy tool.
Run ampy --help for help. Use -p for the device path and -b for baud rate, then file operation commands.
Examples:
$ ampy -p /dev/ttyUSB0 -b 115200 ls
/boot.py
/main.py
$ ampy -p /dev/ttyUSB0 -b 115200 get main.py
from machine import Pin
import time
led_control = Pin(2, Pin.OUT)
while True:
led_control.value(1)
time.sleep_ms(1000)
led_control.value(0)
time.sleep_ms(1000)
$ ampy -p /dev/ttyUSB0 -b 115200 put main.py
$ ampy -p /dev/ttyUSB0 -b 115200 rm boot.py
Note: ampy cannot access the file system while a user program (e.g., main.py) is running. If it hangs, use picocom to stop the program (Ctrl+C), get the REPL, then exit picocom and run ampy.
rshell
Use rshell to access the REPL and manage files.
rshell -p /dev/ttyUSB0
Run rshell -h for help.
VSCode Plugin
Visual Studio Code can also be used for MicroPython development. For example, the MicroPython IDE plugin (requires the command-line tools mentioned above) can upload programs.
Note: When writing code in VSCode, you may see highlighting warnings for MicroPython-specific libraries like machine. Ignore them, as these are not run on the host Python environment.
Thonny IDE
Thonny is another MicroPython IDE. It can detect the MicroPython board on Linux for REPL debugging and supports autocompletion for MicroPython libraries (unlike Mu Editor on Linux, which fails to detect the board, and VSCode which lacks autocompletion for MicroPython-specific libraries).