Using MicroPython on FireBeetle ESP32

In Fire Beetle ESP 32 Project I wrote about connecting the FireBeetle ESP32 with the Arduino IDE and writing a little C-style program. Another interesting approach is to use MicroPython.

Installing MicroPython

Install esptool

To get MicroPython onto the board we use the esptool command line. Because we are in the Python ecosystem we use pip to install esptool:

pip install esptool

Download firmware

Go to https://micropython.org/download#esp32

and download esp32-idf3-20191220-v1.12.bin

This one worked for me 🙂

Erase flash

Before we can install the firmware it is recommended to erase the flash

On my machine the port is /dev/tty.usbserial-14130. This can vary depending on your OS and setup.

esptool.py --port /dev/tty.usbserial-14130 erase_flash

Flash Firmware

Flashing the firmware looks like the following:

esptool.py --chip esp32 --baud 115200 --port /dev/tty.usbserial-14130 write_flash -z 0x1000 ./esp32-idf3-20191220-v1.12.bin

To prevent flash errors we set the transmission speed to 115200 baud

Connect via Serial

When you work with an unixoid system like linux or macos you can use screen to connect via serial port. On Windows you can download the program putty

screen /dev/tty.usbserial-14130 115200

After connecting you will get the prompt from the python command line aka REPL

Working with scripts

After fiddling around with the REPL we want to write our first program. We write a program like the Arduino blink sketch but just in Python:

Blink Program

The onboard blue led is connected to pin 2

from machine import Pin
import time

p2 = Pin(2, Pin.OUT)

def blink():
    while True:
        p2.value(0)
        time.sleep(1)
        p2.value(1)
        time.sleep(1)

blink()

Save this script in a file with the name blink.py

Uploading scripts

To upload scripts to the FireBeetle board we use another nice library called ampy from AdaFruit. We install it via pip as well

pip install adafruit-ampy

With ampy we can upload the file to our board.

ampy --port /dev/tty.usbserial-14130 put blink.py

If it doesn’t work right away, please close your terminal connection.

Starting scripts

When the upload is finished you can reconnect via serial console and import your script and the led should start to blink

>>>import blink

Running scripts on startup

When your script should start directly at power on, you upload it as main.py

ampy --port /dev/tty.usbserial-14130 put blink.py /main.py

References

https://docs.micropython.org/en/latest/esp32/quickref.html#