目次
ESP32 MicroPython
インストール
Raspberry Pi Zero で ESP32-WROVER-B に MicroPython をフラッシュする。
Fedora で ESP32-WROOM-32 をフラッシュする手順も追記。
参考: 1. ESP32 での MicroPython の始め方 — MicroPython 1.18 ドキュメント
Python 仮想環境の作成
$ python -m venv pyMicroPython $ . pyMicroPython/bin/activate (pyMicroPython) $
esptool のインストール
(pyMicroPython) $ pip install esptool
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple/ Collecting esptool Downloading https://www.piwheels.org/simple/esptool/esptool-3.3-py3-none-any.whl (345 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 345.0/345.0 KB 282.0 kB/s eta 0:00:00 Collecting pyserial>=3.0 Downloading https://www.piwheels.org/simple/pyserial/pyserial-3.5-py2.py3-none-any.whl (90 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 90.6/90.6 KB 109.1 kB/s eta 0:00:00 Collecting reedsolo<=1.5.4,>=1.5.3 Downloading https://www.piwheels.org/simple/reedsolo/reedsolo-1.5.4-cp39-cp39-linux_armv6l.whl (921 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 921.3/921.3 KB 536.7 kB/s eta 0:00:00 Collecting cryptography>=2.1.4 Downloading https://www.piwheels.org/simple/cryptography/cryptography-36.0.2-cp39-cp39-linux_armv6l.whl (2.7 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.7/2.7 MB 453.0 kB/s eta 0:00:00 Collecting bitstring>=3.1.6 Downloading https://www.piwheels.org/simple/bitstring/bitstring-3.1.9-py3-none-any.whl (39 kB) Collecting ecdsa>=0.16.0 Downloading https://www.piwheels.org/simple/ecdsa/ecdsa-0.17.0-py2.py3-none-any.whl (119 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 119.3/119.3 KB 186.5 kB/s eta 0:00:00 Requirement already satisfied: cffi>=1.12 in ./pyJupyterLab/lib/python3.9/site-packages (from cryptography>=2.1.4->esptool) (1.15.0) Requirement already satisfied: six>=1.9.0 in ./pyJupyterLab/lib/python3.9/site-packages (from ecdsa>=0.16.0->esptool) (1.16.0) Requirement already satisfied: pycparser in ./pyJupyterLab/lib/python3.9/site-packages (from cffi>=1.12->cryptography>=2.1.4->esptool) (2.21) Installing collected packages: reedsolo, pyserial, bitstring, ecdsa, cryptography, esptool Successfully installed bitstring-3.1.9 cryptography-36.0.2 ecdsa-0.17.0 esptool-3.3 pyserial-3.5 reedsolo-1.5.4
ESP32 の接続ポートを確認
ESP32-WROVER-B
(pyMicroPython) $ lsusb
Bus 001 Device 004: ID 056e:0063 Elecom Co., Ltd Laser mouse M-SODL Bus 001 Device 003: ID 05af:0908 Jing-Mold Enterprise Co., Ltd Wireless Keyboard Bus 001 Device 009: ID 1a86:55d4 QinHeng Electronics USB Single Serial Bus 001 Device 002: ID 1a40:0101 Terminus Technology Inc. Hub Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
(pyMicroPython) $ lsusb -t
/: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=dwc_otg/1p, 480M |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M |__ Port 1: Dev 9, If 1, Class=CDC Data, Driver=cdc_acm, 12M |__ Port 1: Dev 9, If 0, Class=Communications, Driver=cdc_acm, 12M |__ Port 2: Dev 3, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M |__ Port 2: Dev 3, If 1, Class=Human Interface Device, Driver=usbhid, 1.5M |__ Port 3: Dev 4, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
(pyMicroPython) $ ls -l /dev/serial/by-path/
total 0 lrwxrwxrwx 1 root root 13 Apr 22 14:02 platform-20980000.usb-usb-0:1.1:1.0 -> ../../ttyACM0
ESP32-WROOM-32 (cp210x)
(pyMicroPython) $ lsusb -t
/: Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/3p, 480M |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/8p, 480M |__ Port 1: Dev 3, If 0, Class=Hub, Driver=hub/4p, 480M |__ Port 2: Dev 5, If 0, Class=Human Interface Device, Driver=usbhid, 12M |__ Port 2: Dev 5, If 1, Class=Human Interface Device, Driver=usbhid, 12M |__ Port 2: Dev 5, If 2, Class=Human Interface Device, Driver=usbhid, 12M |__ Port 6: Dev 4, If 0, Class=Video, Driver=uvcvideo, 480M |__ Port 6: Dev 4, If 1, Class=Video, Driver=uvcvideo, 480M /: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/3p, 480M |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/6p, 480M (pyMicroPython) [tomoyan@highway-x ~]$ lsusb -t /: Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/3p, 480M |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/8p, 480M |__ Port 1: Dev 3, If 0, Class=Hub, Driver=hub/4p, 480M |__ Port 2: Dev 5, If 0, Class=Human Interface Device, Driver=usbhid, 12M |__ Port 2: Dev 5, If 1, Class=Human Interface Device, Driver=usbhid, 12M |__ Port 2: Dev 5, If 2, Class=Human Interface Device, Driver=usbhid, 12M |__ Port 3: Dev 6, If 0, Class=Vendor Specific Class, Driver=cp210x, 12M |__ Port 6: Dev 4, If 0, Class=Video, Driver=uvcvideo, 480M |__ Port 6: Dev 4, If 1, Class=Video, Driver=uvcvideo, 480M /: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/3p, 480M |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/6p, 480M
(pyMicroPython) $ ls -l /dev/serial/by-path/
合計 0 lrwxrwxrwx 1 root root 13 10月 18 15:37 pci-0000:00:1d.0-usb-0:1.1.3:1.0-port0 -> ../../ttyUSB0
ESP32 のフラッシュを消去
ESP32-WROVER-B
(pyMicroPython) $ esptool.py --port /dev/ttyACM0 erase_flash
esptool.py v3.3 Serial port /dev/ttyACM0 Connecting.... Detecting chip type... Unsupported detection protocol, switching and trying again... Connecting..... Detecting chip type... ESP32 Chip is ESP32-D0WD (revision 1) Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None Crystal is 40MHz MAC: 4c:75:25:xx:xx:xx Uploading stub... Running stub... Stub running... Erasing flash (this may take a while)... Chip erase completed successfully in 17.8s Hard resetting via RTS pin...
ESP32-WROOM-32
※ BOOT ボタンを少し長く押し続けないと、接続に失敗するモデルもある🤔
(pyMicroPython) $ esptool.py --chip esp32 --port /dev/ttyUSB0 erase_flash
esptool.py v4.3 Serial port /dev/ttyUSB0 Connecting...................... Chip is ESP32-D0WDQ6 (revision v1.0) Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None Crystal is 40MHz MAC: 7c:9e:bd:36:81:8c Uploading stub... Running stub... Stub running... Erasing flash (this may take a while)... Chip erase completed successfully in 12.5s Hard resetting via RTS pin...
MicroPython ファームウェアのダウンロード
MicroPython のファームウェアを MicroPython - Python for microcontrollers よりダウンロードする。
ESP32-WROVER-B
※ここでは ESP32 with SPIRAM の最新リリースをダウンロードする。
(pyMicroPython) $ curl -O https://micropython.org/resources/firmware/esp32spiram-20220117-v1.18.bin
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 1592k 100 1592k 0 0 264k 0 0:00:06 0:00:06 --:--:-- 380k
ESP32-WROOM-32
※ここでは ESP32 の最新リリースをダウンロードする。
(pyMicroPython) $ curl -O https://micropython.org/resources/firmware/esp32-20220618-v1.19.1.bin
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 1524k 100 1524k 0 0 440k 0 0:00:03 0:00:03 --:--:-- 441k
MicroPython のファームウェアを ESP32 にフラッシュ
ESP32-WROVER-B
(pyMicroPython) $ esptool.py --chip esp32 --port /dev/ttyACM0 write_flash -z 0x1000 esp32spiram-20220117-v1.18.bin
esptool.py v3.3 Serial port /dev/ttyACM0 Connecting...... Chip is ESP32-D0WD (revision 1) Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None Crystal is 40MHz MAC: 4c:75:25:xx:xx:xx Uploading stub... Running stub... Stub running... Configuring flash size... Flash will be erased from 0x00001000 to 0x0018ffff... Compressed 1631216 bytes to 1052559... Wrote 1631216 bytes (1052559 compressed) at 0x00001000 in 99.2 seconds (effective 131.5 kbit/s)... Hash of data verified. Leaving... Hard resetting via RTS pin...
ESP32-WROOM-32
(pyMicroPython) $ esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 460800 write_flash -z 0x1000 esp32-20220618-v1.19.1.bin
esptool.py v4.3 Serial port /dev/ttyUSB0 Connecting......... Chip is ESP32-D0WDQ6 (revision v1.0) Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None Crystal is 40MHz MAC: 7c:9e:bd:xx:xx:xx Uploading stub... Running stub... Stub running... Changing baud rate to 460800 Changed. Configuring flash size... Flash will be erased from 0x00001000 to 0x0017efff... Compressed 1560976 bytes to 1029132... Wrote 1560976 bytes (1029132 compressed) at 0x00001000 in 24.2 seconds (effective 515.7 kbit/s)... Hash of data verified. Leaving... Hard resetting via RTS pin...
シリアルコンソールに接続する前に、ポートへのアクセス権限を設定
/dev/ttyACM0 の権限を調べる。
(pyMicroPython) $ ls -l /dev/ttyACM0
crw-rw---- 1 root dialout 166, 0 Apr 23 01:09 /dev/ttyACM0
または、/dev/ttyUSB0 の権限を調べる。
(pyMicroPython) $ ls -l /dev/ttyUSB0
crw-rw---- 1 root dialout 188, 0 10月 18 16:26 /dev/ttyUSB0
ユーザーを dialout グループに所属させる。
Linux ユーザー・グループ管理 参照のこと
(pyMicroPython) $ id dietpi
uid=1000(dietpi) gid=1000(dietpi) groups=1000(dietpi)
(pyMicroPython) $ sudo usermod -a -G dialout $USER
dialout
グループへの所属を再ログインせずに反映するには newgrp dialout
コマンドを実行する🤔
(pyMicroPython) $ newgrp dialout (pyMicroPython) $ id
uid=1000(dietpi) gid=1000(dietpi) groups=1000(dietpi),20(dialout)
ESP32 MicroPython のシリアルコンソールに接続
(pyMicroPython) $ screen /dev/ttyACM0 115200
または
接続後に本体の EN ボタンを押すと MicroPython バージョンが表示される🤔
(pyMicroPython) $ screen /dev/ttyUSB0 115200
ets Jun 8 2016 00:22:57 rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:2 load:0x3fff0030,len:4540 ho 0 tail 12 room 4 load:0x40078000,len:12344 ho 0 tail 12 room 4 load:0x40080400,len:4124 entry 0x40080680 MicroPython v1.19.1 on 2022-06-18; ESP32 module with ESP32 Type "help()" for more information. >>>
※接続を終了するにはCtrl + a
-> k
Really kill this window [y/n] に y で答える。
ESP32 MicroPython の help()
>>> help()
Welcome to MicroPython on the ESP32! For generic online docs please visit http://docs.micropython.org/ For access to the hardware use the 'machine' module: import machine pin12 = machine.Pin(12, machine.Pin.OUT) pin12.value(1) pin13 = machine.Pin(13, machine.Pin.IN, machine.Pin.PULL_UP) print(pin13.value()) i2c = machine.I2C(scl=machine.Pin(21), sda=machine.Pin(22)) i2c.scan() i2c.writeto(addr, b'1234') i2c.readfrom(addr, 4) Basic WiFi configuration: import network sta_if = network.WLAN(network.STA_IF); sta_if.active(True) sta_if.scan() # Scan for available access points sta_if.connect("", " ") # Connect to an AP sta_if.isconnected() # Check for successful connection Control commands: CTRL-A -- on a blank line, enter raw REPL mode CTRL-B -- on a blank line, enter normal REPL mode CTRL-C -- interrupt a running program CTRL-D -- on a blank line, do a soft reset of the board CTRL-E -- on a blank line, enter paste mode For further help on a specific object, type help(obj) For a list of available modules, type help('modules') >>>
ampy - Adafruit MicroPython Tool
ampy は Adafruit が今後サポートしなくなる…🤔
MicroPython Basics: Load Files & Run Code - Adafruit より
Adafruit は CircuitPython のみをサポートするので、MicroPython 用の ampy は今後メンテされない😱😇
ampy での実行例を載せているサイトは古い情報なので、MicroPython 公式ドキュメントに記載されている mpremote
MicroPython のリモート制御: mpremote での操作に置き換える😉
The examples in this guide are no longer supported and may not work. We are only supporting CircuitPython on our boards. For more information about using CircuitPython, check out Welcome to CircuitPython: https://learn.adafruit.com/welcome-to-circuitpython
このガイドの例はサポートされなくなり、機能しない可能性があります。 ボードでは CircuitPython のみをサポートしています。 CircuitPython の使用に関する詳細については、CircuitPython へようこそ: https://learn.adafruit.com/welcome-to-circuitpython を参照してください。
ampy のインストール
(pyMicroPython) $ pip install adafruit-ampy
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple/ Collecting adafruit-ampy Downloading https://www.piwheels.org/simple/adafruit-ampy/adafruit_ampy-1.1.0-py2.py3-none-any.whl (16 kB) Collecting python-dotenv Downloading https://www.piwheels.org/simple/python-dotenv/python_dotenv-0.20.0-py3-none-any.whl (17 kB) Collecting click Downloading https://www.piwheels.org/simple/click/click-8.1.2-py3-none-any.whl (96 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 96.6/96.6 KB 128.8 kB/s eta 0:00:00 Requirement already satisfied: pyserial in ./pyJupyterLab/lib/python3.9/site-packages (from adafruit-ampy) (3.5) Installing collected packages: python-dotenv, click, adafruit-ampy Successfully installed adafruit-ampy-1.1.0 click-8.1.2 python-dotenv-0.20.0
動作確認
(pyMicroPython) $ ampy --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.
ESP32 内部のファイルを一覧表示
(pyMicroPython) $ ampy --port /dev/ttyACM0 ls
/boot.py
または
(pyMicroPython) $ ampy --port /dev/ttyUSB0 ls
/boot.py
ファイル内容の表示
(pyMicroPython) $ ampy --port /dev/ttyACM0 get boot.py
# This file is executed on every boot (including wake-boot from deepsleep) #import esp #esp.osdebug(None) #import webrepl #webrepl.start()
または
(pyMicroPython) $ ampy --port /dev/ttyUSB0 get boot.py
# This file is executed on every boot (including wake-boot from deepsleep) #import esp #esp.osdebug(None) #import webrepl #webrepl.start()
参考文献
1. ESP32 での MicroPython の始め方 — MicroPython 1.18 ドキュメント
シリアルコンソール - ArchWiki
How to connect to a serial port as simple as using SSH?
MicroPython Basics: Load Files & Run Code
Linux でシリアル通信を行う方法
ESP32 WROVER-B を使ってみる(SPI RAM対応版 MicroPython導入) - chakokuのブログ(rev4)
Upipを使用して、MicroPythonライブラリをWi-Fi経由でマイクロコンピュータにロードします«ヌルバイト:: WonderHowTo
14. SSD1306 OLED ディスプレイの使い方 — MicroPython 1.18 ドキュメント
クラス SDCard -- SD メモリカード — MicroPython 1.18 ドキュメント