micropython - How to Access a MicroPython Flashed Device

3 min read
micropython - How to Access a MicroPython Flashed Device

1) Preparing a Virtual Environment

Create a folder and name it as desired.

Let's create a virtual environment.

python3 -m venv temp-controller-venv
Creating a virtual environment.
source temp-controller-venv/bin/activate.fish
Activating the virtual environment.

We now need to install the tool ampy to download or upload files to our board.

pip3 install adafruit-ampy

2) Serial Port Configuration

My ESP8266 board has a serial port installed on the board and after connecting it to my Linux PC it is possible to see it listed under the USB devices.

tiago@desktop ~/scripts (main)> lsusb
Bus 002 Device 006: ID 05e3:0743 Genesys Logic, Inc. SDXC and microSDXC CardReader
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 1532:0043 Razer USA, Ltd DeathAdder Chroma
Bus 001 Device 007: ID 1a86:7523 QinHeng Electronics CH340 serial converter
Bus 001 Device 003: ID 1532:0203 Razer USA, Ltd BlackWidow Chroma
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
QinHeng Electronics CH340 serial converter - Attached to the ESP8266 board.

And, we can check which port our serial converter is attached to with the command below.

sudo dmesg | grep -i tty
[    0.076223] printk: console [tty0] enabled
[    0.410913] 00:01: ttyS0 at I/O 0x3f8 (irq = 4, base_baud = 115200) is a 16550A
[227734.859083] usb 1-4: ch341-uart converter now attached to ttyUSB0
[228043.503965] ch341-uart ttyUSB0: ch341-uart converter now disconnected from ttyUSB0
[228044.006867] usb 1-4: ch341-uart converter now attached to ttyUSB0
[228557.181812] ch341-uart ttyUSB0: ch341-uart converter now disconnected from ttyUSB0
[231782.823891] usb 1-4: ch341-uart converter now attached to ttyUSB0

Make sure to grant read and write permissions to the device to your user otherwise ampy will complain.

ll /dev/ttyUSB0
crw-rw---- 1 root dialout 188, 0 Nov  3 20:38 /dev/ttyUSB0
sudo chmod 0666 /dev/ttyUSB0
tiago@desktop ~/scripts (main)> ll /dev/ttyUSB0
crw-rw-rw- 1 root dialout 188, 0 Nov  3 20:38 /dev/ttyUSB0

3) Accessing the ESP8266 Files

Let's list the files currently in our device.

ampy -p /dev/ttyUSB0 -b 115200 ls
/boot.py
/led.py
/main.py
/websocket_server.py

I will backup the files to upload a new project to my board.

ampy -p /dev/ttyUSB0 -b 115200 get /boot.py boot.py
ampy -p /dev/ttyUSB0 -b 115200 get /led.py led.py
ampy -p /dev/ttyUSB0 -b 115200 get /main.py main.py
ampy -p /dev/ttyUSB0 -b 115200 get /websocket_server.py websocket_server.py
(temp-controller-venv) tiago@desktop ~/D/p/p/temp-controller (main)> ll
total 20K
-rw-rw-r-- 1 tiago tiago  353 Nov  3 20:57 boot.py
-rw-rw-r-- 1 tiago tiago  572 Nov  3 20:58 led.py
-rw-rw-r-- 1 tiago tiago 3.7K Nov  3 20:58 main.py
drwxrwxr-x 6 tiago tiago 4.0K Nov  3 18:59 temp-controller-venv
-rw-rw-r-- 1 tiago tiago 2.0K Nov  3 20:58 websocket_server.py

Conclusion

The steps above allowed us to upload or download files from our ESP8266 board flashed with MicroPython.

Resources

MicroPython Basics: Load Files & Run Code
Use command-line tools to send files and execute Python code on a MicroPython board using its serial connection.
GitHub - scientifichackers/ampy: MicroPython Tool - Utility to interact with a MicroPython board over a serial connection.
MicroPython Tool - Utility to interact with a MicroPython board over a serial connection. - GitHub - scientifichackers/ampy: MicroPython Tool - Utility to interact with a MicroPython board over a s...
MicroPython tutorial for ESP8266 — MicroPython 1.17 documentation