MicroPython is a lean and efficient implementation of Python 3 that is optimized to run on microcontrollers. In my experience, it massively accelerates and simplifies the development process. You can find out how to install it on the ESP32 in this tutorial.
To install MicroPyhton on an ESP32, I first download the firmware. For the ESP32 there are options with ESP-IDF v3.x or v4.x. Since v4.x does not yet support all features, I opt for ESP-IDF v3.x Next, you have to choose between the version with and without SPIRAM. My board doesn't have SPIRAM, so I'll take the version without it. And finally the question which version: Here I take the latest version that does not contain the word “unstable”. This is the version at the time the tutorial was written esp32-idf3-20200902-v1.13.bin.
To get the firmware on the ESP32, I use the esptool from the chip manufacturer Espressiv. I do the whole thing in my own virtual Python environment. I create this and activate it with:
> python3 -m venv venv # Windows > ./venv/Scripts/Activate.ps1
In this environment we can then install the esptool:
> pip install esptool
It could take a few minutes. Now you have to connect the ESP32 via USB. In the Windows Device Manager you can find out which COM port the ESP32 is registering as. Simply type “Device Manager” into the Windows search line. Then you should find the COM port under “Connections (COM & LPT)”. If several appear there, simply disconnect and reconnect the ESP32 and watch which COM port is added. For me it's COM5.
Then I first erase the ESP32's memory and then I transfer the firmware:
> esptool.py --port COM5 erase_flash esptool.py v3.0 Serial port COM5 Connecting..... Detecting chip type... ESP32 Chip is ESP32-D0WDQ6 (revision 1) Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None Crystal is 40MHz MAC: 3c:71:bf:96:f1:e8 Uploading stub... Running stub... Stub running... Erasing flash (this may take a while)... Chip erase completed successfully in 7.2s Hard resetting via RTS pin... > esptool.py --chip esp32 --port COM5 write_flash -z 0x1000 esp32-idf3-20200902-v1.13.bin esptool.py v3.0 Serial port COM5 Connecting.... Chip is ESP32-D0WDQ6 (revision 1) Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None Crystal is 40MHz MAC: 3c:71:bf:96:f1:e8 Uploading stub... Running stub... Stub running... Configuring flash size... Compressed 1448768 bytes to 926007... Wrote 1448768 bytes (926007 compressed) at 0x00001000 in 82.5 seconds (effective 140.5 kbit/s)... Hash of data verified. Leaving... Hard resetting via RTS pin...
From now on, Python code can be executed on the ESP32. Either typed line by line in a REPL (Read-Eval-Print Loop, dt: read-in-execute-output loop) or executed as a complete file. To do this, I need a tool with which I can send my code to the ESP32. Here rshell is an option:
> pip3 install rshell
rshellI can look at which files can be found on the ESP32. I just have to
rshellstart and the command
ls -l / pyboardTo run:
> rshell -p COM5 Using buffer-size of 32 Connecting to COM5 (buffer-size 32)... Trying to connect to REPL connected Testing if ubinascii.unhexlify exists ... Y Retrieving root directories ... /boot.py/ Setting time ... Nov 21, 2020 15:02:56 Evaluating board_name ... pyboard Retrieving time epoch ... Jan 01, 2000 Welcome to rshell. Use the exit command to exit rshell. > ls -l /pyboard 139 Jan 1 2000 boot.py
With the command
repl I start the REPL and can enter commands that are executed directly. With the command
help ('modules') can I, for example, display the installed libraries:
> repl Entering REPL. Use Control-X to exit. MicroPython v1.13 on 2020-09-02; ESP32 module with ESP32 Type "help()" for more information. >>> help('modules') __main__ gc uasyncio/stream upip_utarfile _boot inisetup ubinascii upysh _onewire machine ubluetooth urandom _thread math ucollections ure _uasyncio micropython ucryptolib urequests _webrepl neopixel uctypes uselect apa106 network uerrno usocket btree ntptime uhashlib ussl builtins onewire uhashlib ustruct cmath sys uheapq utime dht uarray uio utimeq ds18x20 uasyncio/__init__ ujson uwebsocket esp uasyncio/core umqtt/robust uzlib esp32 uasyncio/event umqtt/simple webrepl flashbdev uasyncio/funcs uos webrepl_setup framebuf uasyncio/lock upip websocket_helper Plus any modules on the filesystem
Saccasner Strasse 19