・ 使用した ESP32ボード
YD-ESP32-S3 のコピー品 の ESP32-S3-N16R8 ボード
1個 570円 x2 で 1,140円(送料無料)と 格安。
(ESP32-S3-WROOM-1-N16R8モジュールを買うより安い)
・ 使用した CAN通信ボード
1個 359円 x2 で 718円(+送料300円)。
(5個セットだと 766円 と激安。おそらくこれも コピー品でしょう)
・ 使用した CANライブラリ
MicroPython 標準では CANライブラリがないため、次の CANライブラリを使用した。
ただし、このライブラリは MicroPython の ユーザーCモジュールのため、MicroPython そのものをビルドする必要がある。
・ ESP32-S3-N16R8 向け MicroPython
MicroPythonリポジトリ の ESP32_GENERIC_S3 は、ESP32-S3-N8 向けで SPIRAM も使用できないため、次の ESP32-S3-N16R8 向けのボード情報でビルドした。
(CircuitPythonだと YD-ESP32-S3ーN16R8 ボード向けが標準で用意されているが、SN65HVD230 の CANモジュールが使えない??)
・ ESP32-S3-N16R8 の MicroPython をビルド
基本的には、 ESP32-S3-N16R8 向けのボード情報 と CANライブラリ の README.md に従ってコマンドを投入すれば良い。
最終リリース版 v1.26.1 の MicroPython をビルドしたかったのだが、make途中でエラーとなり、原因が分からず諦めた1。よって、最新ソースでビルドしたため、v1.27.0-preview.309.gc9a16e862c.dirtyと プレビュー版になってしまった。
何故か partitions-16MiB.csv がリポジトリに見当たらない。以前にCloneしてあったリポジトリからコピーして対策。内容は以下の通り。
# Notes: the offset of the partition table itself is set in
# $IDF_PATH/components/partition_table/Kconfig.projbuild.
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x6000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 0x1F0000,
vfs, data, fat, 0x200000, 0xE00000,
$ idf.py -D MICROPY_BOARD=ESP32_GENERIC_S3_N16R8 -D USER_C_MODULES="../../../../cmodules/micropython-esp32-twai/src_can_v2/micropython.cmake" -B build-ESP32_GENERIC_S3_N16R8_TWAI
% esptool.py --chip esp32s3 --port /dev/cu.wchusbserial51850109041 erase_flash
esptool.py v4.8.1
Serial port /dev/cu.wchusbserial51850109041
Connecting....
Chip is ESP32-S3 (QFN56) (revision v0.2)
Features: WiFi, BLE, Embedded PSRAM 8MB (AP_3v3)
Crystal is 40MHz
MAC: 10:20:ba:49:ab:08
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 3.7s
Hard resetting via RTS pin...
% esptool.py --chip esp32s3 --port /dev/cu.wchusbserial51850109041 -b 460800 --before default_reset --after no_reset write_flash --flash_mode dio --flash_size 16MB --flash_freq 80m 0x0 build-ESP32_GENERIC_S3_N16R8_TWAI/bootloader/bootloader.bin 0x8000 build-ESP32_GENERIC_S3_N16R8_TWAI/partition_table/partition-table.bin 0x10000 build-ESP32_GENERIC_S3_N16R8_TWAI/micropython.bin
esptool.py v4.8.1
Serial port /dev/cu.wchusbserial51850109041
Connecting...
Chip is ESP32-S3 (QFN56) (revision v0.2)
Features: WiFi, BLE, Embedded PSRAM 8MB (AP_3v3)
Crystal is 40MHz
MAC: 10:20:ba:49:ab:08
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Flash will be erased from 0x00000000 to 0x00004fff...
Flash will be erased from 0x00008000 to 0x00008fff...
Flash will be erased from 0x00010000 to 0x001b3fff...
SHA digest in image updated
Compressed 19232 bytes to 12490...
Writing at 0x00000000... (100 %)Wrote 19232 bytes (12490 compressed) at 0x00000000 in 0.4 seconds (effective 360.9 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 117...
Writing at 0x00008000... (100 %)Wrote 3072 bytes (117 compressed) at 0x00008000 in 0.0 seconds (effective 848.2 kbit/s)...
Hash of data verified.
Compressed 1717776 bytes to 1151419...
Writing at 0x00010000... (1 %)Writing at 0x00018f29... (2 %)Writing at 0x00025ea5... (4 %)Writing at 0x00032ebc... (5 %)Writing at 0x00038f88... (7 %)Writing at 0x0003f63c... (8 %)Writing at 0x00048319... (9 %)Writing at 0x0005171d... (11 %)Writing at 0x0005ae9d... (12 %)Writing at 0x00060824... (14 %)Writing at 0x000678b6... (15 %)Writing at 0x0006de61... (16 %)Writing at 0x00073425... (18 %)Writing at 0x000787e9... (19 %)Writing at 0x0007dae7... (21 %)Writing at 0x00083003... (22 %)Writing at 0x000885ff... (23 %)Writing at 0x0008da5e... (25 %)Writing at 0x00092d20... (26 %)Writing at 0x0009802a... (28 %)Writing at 0x0009d314... (29 %)Writing at 0x000a2f58... (30 %)Writing at 0x000a8e70... (32 %)Writing at 0x000ae268... (33 %)Writing at 0x000b32c9... (35 %)Writing at 0x000b853d... (36 %)Writing at 0x000bd70b... (38 %)Writing at 0x000c2455... (39 %)Writing at 0x000c77ca... (40 %)Writing at 0x000cce25... (42 %)Writing at 0x000d2f6f... (43 %)Writing at 0x000d85ed... (45 %)Writing at 0x000ddc81... (46 %)Writing at 0x000e3a36... (47 %)Writing at 0x000e91a6... (49 %)Writing at 0x000eecb0... (50 %)Writing at 0x000f444c... (52 %)Writing at 0x000f97b9... (53 %)Writing at 0x000ffe5b... (54 %)Writing at 0x001063f6... (56 %)Writing at 0x0010c009... (57 %)Writing at 0x001118a6... (59 %)Writing at 0x001174f3... (60 %)Writing at 0x0011d29e... (61 %)Writing at 0x00123611... (63 %)Writing at 0x00128a2c... (64 %)Writing at 0x0012da3d... (66 %)Writing at 0x00132f09... (67 %)Writing at 0x00138567... (69 %)Writing at 0x0013d831... (70 %)Writing at 0x00143469... (71 %)Writing at 0x00148674... (73 %)Writing at 0x0014d6f0... (74 %)Writing at 0x00152d43... (76 %)Writing at 0x00158542... (77 %)Writing at 0x0015da10... (78 %)Writing at 0x00163008... (80 %)Writing at 0x00168c8c... (81 %)Writing at 0x0016e9d3... (83 %)Writing at 0x001739f2... (84 %)Writing at 0x00178b11... (85 %)Writing at 0x0017e228... (87 %)Writing at 0x00183f3d... (88 %)Writing at 0x00189964... (90 %)Writing at 0x0018f67d... (91 %)Writing at 0x00194c9f... (92 %)Writing at 0x00199f32... (94 %)Writing at 0x0019fb84... (95 %)Writing at 0x001a55c6... (97 %)Writing at 0x001ab330... (98 %)Writing at 0x001b1535... (100 %)Wrote 1717776 bytes (1151419 compressed) at 0x00010000 in 26.1 seconds (effective 525.9 kbit/s)...
Hash of data verified.
Leaving...
Staying in bootloader.
% screen /dev/cu.wchusbserial51850109041 115200
MicroPython v1.27.0-preview.309.gc9a16e862c.dirty on 2025-10-12; Generic ESP32S3 module with Octal-SPIRAM with ESP32S3
Type "help()" for more information.
>>> import CAN
>>> dev = CAN(0, extframe=False, tx=5, rx=4, mode=CAN.LOOPBACK, bitrate=50000, auto_restart=False)
CAN: TIMING
CAN: timing brp=0
CAN: timing tseg_1=11
CAN: timing tseg_2=4
CAN: timing sjw=2
CAN: timing triple_sampling=0
CAN: BRP_MIN=2, BRP_MAX=16384
CAN: bitrate 50000kb
CAN: Mode 1
CAN: Loopback flag 1
>>>
無事に CANライブラリ を インポートできた。
・ MicroPython で CAN通信
上記 CANライブラリの README.md に記載の Pythonコードを、基本的にそのまま使用した(なお、ループバックではなく 2台で通信)。
(先頭3行は ESP32ボードを区別するために Macアドレスを出力するように追加)
import network
sta_if = network.WLAN(network.STA_IF)
print("Mac:", ":".join([hex(b)[2:] for b in sta_if.config('mac')])) # my mac address
import asyncio
import CAN
dev = CAN(0, extframe=False, tx=5, rx=4, mode=CAN.NORMAL, bitrate=50000, auto_restart=False)
async def reader():
while True:
if dev.any():
data = dev.recv()
print(f"RECEIVED: id:{hex(data[0])}, ex:{data[1]}, rtr:{data[2]}, data:{data[3]}")
await asyncio.sleep(0.01)
async def sender():
counter = 0
while True:
# Send a message once per second
msg_id = 0x123 # CAN message identifier
# Use list of bytes instead of bytes object
msg_data = [counter & 0xFF, (counter >> 8) & 0xFF]
# Correct parameter order: data first, then ID
dev.send(msg_data, msg_id) # data, id
print(f"SENT: id:{hex(msg_id)}, data:{msg_data}")
counter += 1
await asyncio.sleep(1) # Send message once per second
async def main():
# Start both tasks concurrently
read_task = asyncio.create_task(reader())
send_task = asyncio.create_task(sender())
# Wait for both tasks (this will run forever)
await asyncio.gather(read_task, send_task)
# Run the example
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
・ 実行結果
以下の通り 2台の ボードで相互に通信できていることが 確認できた。
・ .binファイルのマージ
前記のesptool.py write_flashから分かるように、①ブートローダ、②パーテションテーブル、③MicroPythonファームウェアの 3つの .binファイル を指定していますが、これらをマージして 一つの .binファイルにしておく。
% esptool.py --chip esp32s3 merge_bin --output ESP32-S3-N16R8_TWAI-v1.27.0-preview.309.bin --flash_mode dio --flash_size 16MB --flash_freq 80m 0x0 build-ESP32_GENERIC_S3_N16R8_TWAI/bootloader/bootloader.bin 0x8000 build-ESP32_GENERIC_S3_N16R8_TWAI/partition_table/partition-table.bin 0x10000 build-ESP32_GENERIC_S3_N16R8_TWAI/micropython.bin
esptool.py v4.10.0
SHA digest in image updated
Wrote 0x1b3610 bytes to file ESP32-S3-N16R8_TWAI-v1.27.0-preview.309.bin, ready to flash to offset 0x0
% esptool.py --chip esp32s3 --port /dev/cu.wchusbserial51850109041 -b 460800 --before default_reset --after no_reset write_flash --flash_mode dio --flash_size 16MB --flash_freq 80m 0x0 ESP32-S3-N16R8_TWAI-v1.27.0-preview.309.bin
esptool.py v4.10.0
Serial port /dev/cu.wchusbserial51850109041
Connecting....
Chip is ESP32-S3 (QFN56) (revision v0.1)
Features: WiFi, BLE, Embedded PSRAM 8MB (AP_3v3)
Crystal is 40MHz
MAC: 34:85:18:99:b5:ac
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Flash will be erased from 0x00000000 to 0x001b3fff...
SHA digest in image updated
Compressed 1783312 bytes to 1164353...
Wrote 1783312 bytes (1164353 compressed) at 0x00000000 in 26.5 seconds (effective 539.2 kbit/s)...
Hash of data verified.
Leaving...
Staying in bootloader.
もちろん、マージ前と同様に問題なく動きました。
・ Arduino環境
Arduino環境で CAN通信 する場合は、次の記事を参照してください。
以上
-
v1.26.1 の MicroPython がビルドできなかった原因は、どうやら ESP-IDF のバージョンが関係しているようだ。今回は ESP-IDF 5.5.1 を使っていた。
関連記事:ESP-IDF 環境を bookworm-slim で構築(https://qiita.com/nak435/items/6ce4a419f4210c2ec3ab) ↩
