uniq_

Micro Python on NodeMCU

NodeMCU is a pretty cheap ESP8266 based WiFi capeable micro controller, with good open source support. I had one stowed away in a box for years. When I re-discovered it the other day I finally gave it a try. It's also supported by Micro Python so getting software running there is pretty effortless

I started by getting the latest version of micro python firmware for Node MCU. From: https://micropython.org/download/ESP8266_GENERIC/

wget https://micropython.org/resources/firmware/ESP8266_GENERIC-20240105-v1.22.1.bin

Next I needed to setup my Debian user to be allowed to open tty devices. Because NodeMCU (like many other micro controllers) uses serial.

# you need to re-login for this change to take effect
sudo adduser $USER dialout

Then I've connected the micro controller to my computer with USB. A good tool for inspecting USB devices is lsusb. The micro controller showed up as /dev/ttyUSB0 for me.

Next I needed to install esptool a script for flashing firmware to mirco controllers. It's not packaged for Debian, but it can be comfortably installed using pip. (pip is pythons package manager and is packaged for Debian sudo apt install python3-pip)

pip install esptool --break-system-packages

Now I we can proceed to flash the previously downloaded pre-compiled micro python firmware.

esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash --flash_size=detect --flash_mode qio 0 ./ESP8266_GENERIC-20240105-v1.22.1.bin

(Side note: The manufacturer printed "use 9600 baud" on my board. However esptool runs into a timeout when the baud rate is too low. This was quite confusing to figure out ...)

Micro Python is installed now. To have some quick fun, we can use screen to connect to the micro controller and get an interactive python shell. For testing I've connected a LED to the pin labeled D1 (and G). Here's how to use an interactive shell to make it blink:

screen /dev/ttyUSB0 115200
>>> from time import sleep
>>> from machine import Pin
>>> p = Pin(5, Pin.OUT)
>>> while True:
...     p.value(1 - p.value())
...     sleep(0.5)
...

To interrupt the loop press [Ctrl+c], to quit the screen session press [Ctrl+a] [\] [y]

It was also easy to connect to my WiFi in the interactive shell by just following the instructions of micro pythons built in help().

Here are some docs I found helpful:

written by uniq on 2024-01-31