~/blog/micropython-esp8266
Published on

Micropython for Wemos D1 mini (ESP8266)

Authors
  • avatar
    Name
    Branimir Kirilov
    Twitter

What is it?

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.

Why?

One might have many reasons to do this, but my primary driver is quite simple - developing in Python is much more pleasant for me than developing in C. So without further ado let's see how to install it.

Installation

The microcontroller that I'm going to be using is the ESP8266 based Wemos D1 mini v4.0.0. However, Micropython is available for other chips too and you can see them on the official site.

wemos d1 mini v4

Prerequisites

  • Ensure that you have python installed and running

Install esptool

The next step is to install esptool which is a utility to communicate with the ROM bootloader in Espressif (ESP) chips. This can be done by running:

pip install esptool

Download the firmware

After the esptool is installed, the firmware that is going to be installed on the microcontroller should be downloaded. This can be done by visiting the official site and choosing the firmware for the chipset you have. In the case of Wemos D1 mini, this would be the ESP8266.

Locating the port of the device

Next, the device should be connected to the computer. I'm on MacOS and to locate the correct port I can go to the terminal and type:

ls /dev/cu.usb*

The port of the device would start with cu.usbserial*, in my case I have only one USB plugged in. If you have more than one, you can unplug the microcontroller, check the output of the command and then run it again to see which one is the correct.

output-port

Flashing the firmware

To erase the flash run the following but replace it with your port name:

esptool.py --port /dev/cu.usbserial-1440 erase_flash

To install the new firmware run the following and replace the port and the path to the .bin file that you downloaded earilier

esptool.py --port /dev/cu.usbserial-1440 erase_flash --baud 115200 write_flash --flash_size=4MB -fm dio 0 path/to/firware.bin

Testing

To test everything that was done so far, let's create a quick program to blink the on-board LED of the microcontroller.

To connect to the board, the screen command can be used.

screen /dev/cu.usbserial-1440

After it runs (should be a blank terminal), hit the enter button. The terminal should now display >>>. This indicates that we are now running an interactive shell. Now paste the following and hit enter again:

import time
from machine import Pin

led = Pin(2, Pin.OUT)

while True:
	if led.value():
		led.off()
	else:
		led.on()
	time.sleep_ms(250)

If you see the on-board LED blinking every 250ms (like in the video below), it means that the installation was successful! 🎉