Home > general > Flashing MicroPython on ESP8266

Flashing MicroPython on ESP8266

This post is a summary on how to install and use MicroPython on ESP8266. MicroPython is a Python 3 implementation, optimized to run on microcontrollers. Due to resource restrictions, it contains a subset of the original standard library. Nevertheless, it allows us to use our beloved Python language and rapid prototype our projects.

First, we need to flash the MicroPython firmware. Goto https://micropython.org/download and download the latest firmware for ESP8266. At the time of writing, this is esp8266-20161110-v1.8.6.bin. Connect your ESP8266 to the USB port and figure out the port name. I am using Linux so I use dmesg. In my case, the port is /dev/ttyUSB0. If your module powers up but does not connect to a port the are a couple of possibilities. Make sure you are using a normal USB cable with data connections. In other words, make sure that your cable is not for charging only; usually the cables coming with power banks are power only, connecting only the power pins of the USB to the module. Another possibility, is that not enough power goes through to keep the connection alive. In this case, try another cable, or try connecting the cable to another USB port.

For flahsing the firmware we need the esptool.py script. Install it using pip:

sudo pip install esptool

Once the installation is finished and the module is connected, run the following command:

esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash --flash_size=8m 0 esp8266-20161110-v1.8.6.bin

and here is what it should look like:

esp8266_micropython_flashing

By the way, you can try higher baud rates if you want to speed up the process but 115200 is a safe one.

Next, disconnect and reconnect the USB cable. Check again the port name and use a serial terminal application to connect to the module which now expects 115200 baud rate communication. I use picoterm using the following:

picocom /dev/ttyUSB0 -b 115200

Press ENTER once and you will be prompted with the familiar >>> Your module is now flahsed and you can start typing away in Python. Before closing, lets see a couple of commands to see how this works. We will flash the LED on the ESP8266 a couple of times. To do this, we need to import machine in order to manipulate the values on the pins.

import machine
import time
 
led_pin = machine.Pin(2, machine.Pin.OUT)
 
def flash_led(lp):
    lp.low()
    time.sleep(1)
    lp.high()
    time.sleep(1)
    lp.low()
    time.sleep(1)
    lp.high()
    time.sleep(1)

With the above snippet, we created a function `flash_led` that takes as an argument a pin from the ESP8266 module and toggles its value. At the start, we assign pin 2 to `led_pin`. So finally, run

flash_led(led_pin)

to flash the LED on the board.

Hope this was useful to you. Let me know of your comments.

Reference: http://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/index.html


Related Posts:



Categories: general Tags:
  1. No comments yet.
  1. No trackbacks yet.