Getting Started with Micro Python on ESP32

I got my little game working on the Raspberry Pi yesterday, and today I got to thinking how I could make it work on something a bit, er, cheaper. It’s not so much that I begrudge paying the price for the Pi, more that I can think of more demanding things to do with the Pi than just run the game. My thoughts turned to the ultra-cheap ESP32 devices that I’ve been playing with. The only snag is that I’ve written the entire game in Python for the Raspberry Pi, and I don’t fancy re-writing it in the C++ that these devices normally run.

So, why not run Pyhon on an ESP 32 device?

This turns out to be really easy. If you’ve installed ESP32 devices as part of your Arduino development environment you will have a useful little program called esptool.py on your machine. To convert a device to Micro Python you just have to plug in one of those ultra-cheap devices, find out what serial port it is connected to (in my case com4) and then use the command below to program the chip with Micro Python.

esptool.py --chip esp32 --port com4 --baud 460800 write_flash -z 0x1000 micropython.bin

This loads the image in the file micropython.bin into the device. To get a Python image, go to the download site and look for ESP32 devices. I used the one in the file esp32-20190714-v1.11-146-g154062d9c.bin

Above you can see what happened when I ran the command.

When you restart your ESP32 you will be able to talk Python to it via a command prompt. That’s fine, but what you really want is an IDE that lets you write and deploy Python programs.

The best one I’ve found is called Thonny. It’s works with “normal” Python nicely enough, but it also has an option you can use to point it at an embedded device:

In the options menu above I’ve told Thonny to search for a device running MicroPython and connect to that. Now, when I run a program it is deployed into the device and executes from there.

The MicroPython installation provides a tiny filesystem that can hold python programs. When you run a program it is transferred into that filesystem and runs from there. There are two special program files on the device, boot.py and main.py. The program in the boot.py file is executed when the device powers up, followed by the one in main.py The Thonny program has an option (in the Device menu) to save the program you are editing one of these files. This makes it really easy to make your Python program run when the device is powered up. I made my game program the main.py one and now I have my original Python program running inside a device that only cost around a fiver.

It wasn’t quite as simple as just copying the files over. The api (application programmer interface) to use GPIO ports and Neopixels are different in the two devices and I discovered that MicroPython does not provide a random.shuffle method. This meant that I had to create my own shuffling function, which wasn’t too hard.

Anyhoo, I now have my program running in Python on a tiny embedded device. Which is rather nice. I’ll put the code up on GitHub later this week.