I wanted to check out all the buzz about MicroPython. And, try it out on ESP32.
An aspect that I love is that libraries can be compiled to machine code taking less space than if they are left as scripts.
micropython is built upon espressif SDK .
MicroPython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimised to run on microcontrollers and in constrained environments.
The MicroPython pyboard is a compact electronic circuit board that runs MicroPython on the bare metal, giving you a low-level Python operating system that can be used to control all kinds of electronic projects.
MicroPython is packed full of advanced features such as an interactive prompt, arbitrary precision integers, closures, list comprehension, generators, exception handling and more. Yet it is compact enough to fit and run within just 256k of code space and 16k of RAM.
MicroPython aims to be as compatible with normal Python as possible to allow you to transfer code with ease from the desktop to a microcontroller or embedded system.
I didn’t want to use precompiled firmware but wanted the ability to add modules, libraries and recompile the firmware myself.
To keep everything neat and tidy a virtual box machine is set up.
Download and install VirtualBox
Download latest lbuntu.iso
Create Virtual Machine in VirtualBox
To compile stuff faster we will need more memory
Open up a terminal and install virtualbox additions
> cd /media/micro/VBox_GAs_6.1.22/
> sudo ./VBoxLinuxAdditions.run
Restart. Now We need build tools.
> sudo apt-get install build-essential libssl-dev libffi-dev python-dev
> sudo apt-get install libreadline-dev libffi-dev git pkg-config gcc-arm-none-eabi libnewlib-arm-none-eabi
> sudo apt-get install flex bison gperf python3-pip python3-setuptools python3-serial python3-future python3-pyparsing python3-pyelftools cmake ninja-build ccache libffi-dev python-is-python3 dfu-util
> mkdir MicroPythonFirmware
> cd MicroPythonFirmware/
> git clone --recurse-submodules https://github.com/micropython/micropython.git
this will download the entirety of the micropython repository from GitHub. Not needed but…
> git clone -b v4.4 https://github.com/espressif/esp-idf.git
> cd esp-idf/
> git submodule sync
> git submodule update --init
> ./install.sh esp32
> cd ~/MicroPythonFirmware/micropython/
> make -C mpy-cross
add to .bashrc
alias get_idf='. $HOME/MicroPythonFirmware/esp-idf/export.sh'
> cd ~/microPython/micropython/ports/esp32/
> get_idf
> idf.py set-target esp32
> idf.py menuconfig
Here we get a really nice build menu with all compile options.
> idf.py build
Project build complete. To flash, run this command:
/home/micro/.espressif/python_env/idf4.4_py3.8_env/bin/python ../../../esp-idf/components/esptool_py/esptool/esptool.py -p (PORT) -b 460800 --before default_reset --after hard_reset --chip esp32 write_flash --flash_mode dio --flash_size detect --flash_freq 40m 0x1000 build/bootloader/bootloader.bin 0x8000 build/partition_table/partition-table.bin 0x10000 build/micropython.bin
or run 'idf.py -p (PORT) flash'
if your VirtualBox has a USB passthrough then you could flash your esp32 board from the VM.
If not, just open a file manager, go to ~/microPython/micropython/ports/esp32/build, and drag and drop micropython.bin, partition_table folder, and bootloader/bootloader.bin over to your windows system desktop.
On windows system open command prompt as admin and navigated to a folder that contains microptyhon.bin file and issue command:
> esptool --chip esp32 --port COM9 erase_flash
> esptool -p COM9 -b 460800 --before default_reset --after hard_reset --chip esp32 write_flash --flash_mode dio --flash_size detect --flash_freq 40m 0x1000 bootloader/bootloader.bin 0x8000 partition_table/partition-table.bin 0x10000 micropython.bin
In VS Code with Pymakr plugin we connect to the board and are presented with python interactive console.
And now we can upload our python script to be executed on freshly compiled micropython.
You can freeze libs by placing the .py libraries in the esp32/frozen/common folder and then rebuilding it
Hi! Nice post, thank you! Two questions: why not use 5.0-dev?
And when I try idf.py set-target esp32 it throws up
MicroPythonFirmware/esp-idf/tools/cmake/build.cmake:197 (message):
Failed to resolve component 'tcpip_adapter'.