Setup MicroPython on ESP32 (for macOS)

CP210x 드라이버 설치 (https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers)

esptool 설치 (pip3가 없는 경우, homebrew를 이용해 설치한다)

$ pip3 install esptool

보드를 연결하고, 기존 flash 모두 초기화

$ esptool.py --chip esp32 -p /dev/tty.SLAB_USBtoUART erase_flash

esptool.py v2.6
Serial port /dev/tty.SLAB_USBtoUART
Connecting........__
Chip is ESP32D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
MAC: 84:0d:8e:0c:c9:f0
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 2.9s
Hard resetting via RTS pin...

펌웨어 다운로드 (https://micropython.org/download/#esp32)

펌웨어를 보드에 Flash 한다.

$ esptool.py --chip esp32 -p /dev/tty.SLAB_USBtoUART write_flash -z 0x1000 esp32-bluetooth.bin

esptool.py v2.6
Serial port /dev/tty.SLAB_USBtoUART
Connecting.....
Chip is ESP32D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
MAC: 84:0d:8e:0c:c9:f0
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 1549888 bytes to 970594...
Wrote 1549888 bytes (970594 compressed) at 0x00001000 in 85.5 seconds (effective 145.0 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

보드의 전원을 껐다가 다시 인가하고, 터미널에서 다음과 같이 입력하여 REPL 환경으로 진입.

$ screen /dev/tty.SLAB_USBtoUART 115200

처음엔 아무런 내용이 나타나지 않지만, 명령어를 입력하면 됨. 평소 사용한 REPL 환경와 동일.

>>> print("hello")
hello
>>> 

REPL 환경에서 나오려면, ctrl + a, ctrl + \ 키를 순서대로 누르고, 화면 밑 프롬프트가 나오면 y를 눌러 종료 가능.

Adafruit에서 제공하는 Micro Python Tool을 이용해 보드 내부의 파일 시스템에 파일을 넣거나 스크립트를 실행할 수 있음.

$ pip3 install adafruit-ampy

보드 내부 파일 시스템에 있는 파일들 표시

$ ampy -p /dev/tty.SLAB_USBtoUART ls

/boot.py

다음의 명령어를 이용해 파일들 조작 가능.

ampy
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.

보드에 있는 LED로 GPIO 테스트. 13번 핀에 연결되어 있음.

>>> import machine
>>> led = machine.Pin(13, machine.Pin.OUT)
>>> led.value(1)
>>> led.value(0)

일단은 여기까지.

참고링크: https://github.com/pvanallen/esp32-getstarted

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.