I have been playing around with MicroPython for a while now. I enjoy writing straight up C/C++ code but MicroPython has brought the ease of Python to embedded platforms especially ESP8266. ESP8266 and MicroPython make a killer combo.
I’ve written about the ESP8266 and built a couple projects with it already. I’ve built a WiFi temperature sensor using the ESP8266 board that publishes the temperature over MQTT to a central data logging server. It’s a fun project.
MicroPython on the ESP8266 is not supported out of the box. To use it, you’ll need to first compile the ESP SDK. Then using the ESP SDK we’ll compile MicroPython toolchain. I know it’s a little roundabout but it’s not too tough. Once you’re done with it you can use PyCharm or any other IDE to write code for it.
Step 1: Clone the ESP SDK github repo
$ git clone --recursive https://github.com/pfalcon/esp-open-sdk.git
Step 2: Install all macOS dependencies
For this step, I assume you already have Home Brew. If not, please set it up. See instructions here.
$ brew tap homebrew/dupes $ brew install binutils coreutils automake wget gawk libtool help2man gperf gnu-sed --with-default-names grep
Step 3: Export path to find the toolchain
$ export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"
Step 4: Create a case-sensitive Volume
This step is not required if you already have a case sensitive filesystem on your Mac. To check run this command:
$ diskutil info / | grep 'File System Personality' File System Personality: Case-sensitive Journaled HFS+
If you see ‘Case-sensitive’, you’re good and you can skip Step 5. If not, please continue.
$ sudo hdiutil create ~/Documents/esp-build.dmg -volname "esp-build" -size 20g -fs "Case-sensitive HFS+" $ sudo hdiutil mount ~/Documents/esp-build.dmg $ cd /Volumes/esp-build
Step 5: Build ESP SDK
Step 6: Clone MicroPython git repo
$ git clone --recursive https://github.com/micropython/micropython.git
Step 7: Build MicroPython cross-compiler
$ cd micropython $ make -C mpy-cross
Step 8: Build MicroPython port for ESP8266
$ cd esp8266 $ make axtls $ make
Step 9: Flash MicroPython to your ESP8266
$ make PORT=/dev/cu.SLAB_USBtoUART FLASH_MODE=qio FLASH_SIZE=32m deploy
You may need to update PORT variable to reflect your ESP8266’s serial port.
Step 10: Fire up the serial terminal
$ miniterm.py /dev/tty.SLAB_USBtoUART 115200
You’ll be greeted with a Python REPL. Enjoy!
For further reading, see MicroPython’s port to ESP8266.
Latest posts by Dinesh (see all)
- Designing Fast & Scalable Python MicroServices with Django - July 21, 2018
- PyConSK 2018 Experience - March 19, 2018
- Designing Fast & Scalable Python MicroServices using Django - February 25, 2018
Also published on Medium.