esp32 + MicroPython

Jul 23, 2020

安裝 MicroPython 到 esp32 上

安裝 Python3

  • 下載 Python 3 - https://www.python.org/downloads/windows/
  • 安裝下載的檔案
  • 下載 MicroPython Firmware

    http://micropython.org/download#esp32

    使用 esptool 刷 MicroPython Firmware

    # 安裝 esptool pip install esptool # 燒錄 micropython esptool.py --chip esp32 --port <port> \ --baud 460800 write_flash -z 0x1000 \ <MicroPython Firmware 的路徑>

    <port> 的值在 Windows 上會是類似 COM5 之類的,而在 Linux 上則是類似 /dev/ttyUSB0

    使用 serial terminal 連到 esp32

    刷完之後可以使用 serial terminal 連到 esp32 驗證是否運作正常

    # 安裝

    與 MicroPython 溝通的工具

    rshell - remote shell for MicroPython

    # 安裝 pip install rshell # 啟動 rshell rshell

    ampy - Adafruit MicroPython tool

    # 安裝 pip install adafruit-ampy # 測試是否有安裝成功 ampy --port /dev/ttyUSB0 ls --help

    可用指令

    Usage: ampy [OPTIONS] COMMAND [ARGS]... ampy - Adafruit MicroPython Tool Ampy is a tool to control MicroPython boards over a serial connection. Using ampy you can manipulate files on the board's internal filesystem and even run scripts. Options: -p, --port PORT Name of serial port for connected board. Can optionally specify with AMPY_PORT environment variable. [required] -b, --baud BAUD Baud rate for the serial connection (default 115200). Can optionally specify with AMPY_BAUD environment variable. -d, --delay DELAY Delay in seconds before entering RAW MODE (default 0). Can optionally specify with AMPY_DELAY environment variable. --version Show the version and exit. --help Show this message and exit. Commands: get Retrieve a file from the board. ls List contents of a directory on the board. mkdir Create a directory on the board. put Put a file or folder and its contents on the board. reset Perform soft reset/reboot of the board. rm Remove a file from the board. rmdir Forcefully remove a folder and all its children from the board. run Run a script and print its output.

    使用 VSCode 進行開發

    有人開發了一套還蠻好用的 cli micropy-cli 可以協助你下載 micropython 的 stubs 檔案。

    以及進行 VSCode 的設定

    GitHub - BradenM/micropy-cli: Micropython Project Management Tool with VSCode support, Linting, Intellisense, Dependency Management, and more!
    Micropython Project Management Tool with VSCode support, Linting, Intellisense, Dependency Management, and more! - BradenM/micropy-cli
    favicon
    https://github.com/BradenM/micropy-cli
    GitHub - BradenM/micropy-cli: Micropython Project Management Tool with VSCode support, Linting, Intellisense, Dependency Management, and more!

    MicroPython 的編譯環境設定與安裝

    目前藍芽功能還在開發測試中 Github 上的 Pull Request,所以如果目前要使用藍芽的話,需要自行編譯。

    安裝 esp-idf

    目前是使用 esp-idf release 3.3 的版本,因為 MicroPython 只能在特定的版本下編譯。

    特定的版本可以查詢 micropython/ports/esp32/Makefile

    內的 ESPIDF_SUPHASH

    # 假設 esp-idf 與 xtensa 都是解壓縮到 $HOME/esp32 底下 ESP_HOME=$HOME/esp32 # Clone esp-idf 與安裝需要的 python package cd $ESP_HOME git clone -b release/v3.3 --recursive https://github.com/espressif/esp-idf.git cd esp-idf git submodule update --init --recursive python -m pip install --user -r $ESP_HOME/esp-idf/requirements.txt

    esp32 Toolchain: xtensa

    並且要注意 esp-idf 也需要特定版的 toolchain

    https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz

    解縮縮之後,需要將 bin 的路徑加入環境變數 PATH 中。

    順便設定 IDF_PATHESPIDF

    export ESP_HOME="$HOME/esp32" export IDF_PATH="$ESP_HOME/esp-idf" export ESPIDF="$ESP_HOME/esp-idf" export PATH="$PATH:$ESP_HOME/xtensa-esp32-elf/bin:$IDF_PATH/tools"

    編譯

    # 取得原始碼 git clone https://github.com/micropython/micropython.git cd micropython # 安裝需要的 python packages pip install pyserial pyparsing # 編譯 micropython 的 cross compile 工具 make -C mpy-cross # ESP32 的版本需要用到 Berkeley DB git submodule init lib/berkeley-db-1.xx git submodule update cd ports/esp32 make

    ← Go home