Metrika
Ivy Knob

ESP8266 SDK Comparison: MicroPython Part 3

ESP8266 SDK Comparison: MicroPython Part 3

MicroPython - is lightweight Python 3 implementation for micro-controllers. It only includes small set of original standard library components and optimized to work in environments with the small amount of resources.

Installation is easy, just download the firmware and flash it. Nice feature to have is WebREPL, it allows to have REPL over web interface: upload/download files and use console on the running board. But WebREPL interface is ugly and not for humans.

Libraries

MicroPython have standard library with ready to use components. There are MQTT-client and support of DHT sensors from the box, but no modules for BME280 sensors.

Some libraries are published in PyPi with micropython- prefix. But the are no central place or catalog for dependency search.

Then you need to upload the library to the board and include it in your code. To save memory it's recommended to precompile libs with mpy-cross.

Toolchain

First of all, there are a lot of "micropythons" exists. MicroPython, pfalcon MicroPython, pycom, CircuitPython. They are all almost the same. Pycom and CircuitPython are vendors forks of MicroPython with the support of Pycom and Adafruit boards.

Unfortunately, working with files is inconvenient, you have to upload it via WebREPL, or with special programs, like ampy. I made simple Makefile to simplify this process, but it still not very good and fast.

Tests and CI/CD

There is cross-compiler called mpy-cross that allows to precompile the code and upload it to the board. It can be used to check if code compiles on CI/CD.

Someone made unittest standard library, but there are no info how to use it. Should the test code run on the board itself? Will it run on CI/CD worker? No info even in Google.

Memory

MicroPython SDK, as Lua, is memory hungry, so sometimes you need to make a few tricks to save the memory. One of them is precompiled modules. It uses mpy-cross for that. Just note, you can't precompile main.py and boot.py. Problem is, that to use mpy-cross you need to compile it with all dependencies.

Another great way of saving memory is adding your modules to micropython firmware binary. You need to include your libraries in the code of micropython and build it yourself.

Mac OS X Problem

When I tried to build micropython on my mac, I got a problem with strip and size tools while building.

There are two ways of solving this problem.

First one is to run compilation with verbose output (make V=1), then copy failed linker command and run it manually without stripping and sizing.

Second solution is to modify the Makefile and remove strip and size commands. I commented 137 and 139 lines in py/mkrules.mk:

ifndef DEBUG
# $(Q)$(STRIP) $(STRIPFLAGS_EXTRA) $(PROG)
endif
# $(Q)$(SIZE) $$(find $(BUILD) -path "$(BUILD)/build/frozen*.o")
$(PROG)

Conclusion

MicroPython is great overall SDK for ESP8266/ESP32 wifi system on chip. It's based on easy to use python programming language, that is much easier comparing to C++ and Arduino IDE. It does support modern libraries, but without central search place. It eats memory, but there are some tricks.

It does support CLI tools for CI/CD, compilation, code check and unit tests. That is huge comparing to NodeMCU and makes MicroPython great SDK for beginner or pro users.

Sometimes it lacks of documentation and support of popular libraries, but all these little problems can be solved easily.

I recommend giving MicroPython a try in your new project.