0x04 Getting Started with MicroPython on an ESP32
Introduction
There are currently a few weekend projects that have been on my mind the last few weeks. All of them require internet connectivity and all of them are low-powered. I have a few ESP32s lying around from various hardware projects so now is a good time to repurpose them into something more interesting.
There are a few options for programming an ESP32: C, Arduino, and MicroPython. They each have their advantages and disadvantages:
- C can eek out extreme performance and has more fine-grained control of the hardware but is difficult for newbies.
- Arduino is slightly higher level and more familiar to a lot of users. Best blend of both worlds.
- MicroPython is very high-level but much slower and more bloated than the other options (as it is interpreted and not compiled). This makes the development lifecycle pretty fast and allows us to handle network requests without mucking around too much.
MicroPython was selected because a) I’ve never used it in a project before; and b) I need to make network requests and Python is familiar to me.
Installing MicroPython
Installing MicroPython on the ESP32 requires flashing the built MicroPython firmware.
- Make sure your pip is updated as it failed to build esptool for me on an older version.
python3 -m pip install --upgrade pip
- Install esptools
python3 -m pip install esptool
- Download the firmware from the MicroPython website. Here is the list of all devices available and here is the firmware specifically for my ESP32. Download the latest release on the page (currently
v1.15 (2022-01-17).bin
). cd
into the directory you downloaded the firmware in.- Plug in your ESP32 via a USB cable and find the device with
ls /dev/tty*
. It should be something similar to/dev/ttyUSB0
. - Hold the “IO0” button on the ESP32 when plugging in the device to put it into download mode. Erase the flash on the ESP32 with the command
python3 -m esptool --chip esp32 --port /dev/ttyUSB0 erase_flash
. Replace/dev/ttyUSB0
with your device. THIS WILL ERASE EVERYTHING ON YOUR ESP32! - If you’re using Linux (which I am), you may not be part of the
tty
group, denying access to/dev/ttyUSB0
. You could runpython
withsudo
but that’s bad practice so let’s fix the groups.- See what groups are available on your system with
compgen -g
(this will list all available groups on the system). This broke my zsh so I had to run it from bash instead. - Add yourself to the tty group with
sudo usermod -a -G tty <your_username>
- You may need to add yourself to the dialout group as well (Ubuntu). Arch / Manjaro users need to add themselves to the uucp group. This can be confirmed by checking the return from
stat /dev/ttyUSB0
and seeing which group owns the device (in the Gid: ( 987/ uucp) output section) - Logout and login to refresh the groups permission for your user (required).
- See what groups are available on your system with
- Deploy the new firmware with
python3 -m esptool --chip esp32 --port /dev/ttyUSB0 write_flash -z 0x1000 esp32-20220117-v1.18.bin
. Change the .bin filename to the file you downloaded. - Done!
Python REPL
We can connect to the Python REPL over the USB serial port. There is also a way to connect via WiFi but I won’t be doing that.
- Ensure you have picocom installed.
picocom /dev/ttyUSB0 -b115200
. This means we’ll be using a baud rate of 115200 which is what the REPL is configured with by default.- Now we have a Python REPL we can use! Type something like
print("This is cool")
to ensure it works. You should see “This is cool” echoed back to you and “»>” for a prompt line afterwards. CTRL+D will soft reset the board and bring you back to a fresh prompt if you break anything.
We can now control the ESP32! Try this to make the LED on the board turn on and off.
import machine
pin = machine.Pin(2, machine.Pin.OUT)
pin.on()
pin.off()
You can press CTRL+A, CTRL+X to exit picocom.
Now you’re ready to program! The next blog post will go over connecting to the internet and coding up a small program for our ESP.