How to use Micropython and Ampy with ESP8266

This is a quick tutorial for using Micropython and Ampy with ESP8266.

I have been working with  my ESP8266 off late and I found that someone has asked in reddit about how to get  micropython running in ESP8266. Because I have already done something similar for node mcu, I  wrote a tutorial about it.

$sudo pip install virtualenv
$virtualenv mylocalpy
$source mylocalpy/bin/activate


Installing ESP tool:

You can get this tool from here or .
Since it is part of the CheeseShop , I wanted to try it this time.

(localpy)$pip install esptool


Installing Ampy:

The next thing is a tool used for working with MicroPython called Ampy from Adafruit. It is a a utility to work with MicroPython using Serial interface.

(localpy)$pip install adafruit-ampy

Now insert your favourite ESP8266 board. It can be anything from nodeMCU or an  esp-12-e based self built board. Make it ready for flashing with the appropriate boot settings. Identify the serial device from dmesg and lsusb commands.

Download the Micropython:

(localpy) --port /dev/ttyUSB0 erase_flash v1.3 
Running Cesanta flasher stub... 
Erasing flash (this may take a while)... 
Erase took 10.8 seconds 

Pull the power off and reinsert again to get the board back in to the boot mode.
I needed this for my board, you may not need to do this.

(localpy) --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0  esp8266-20170108-v1.8.7.bin v1.3 
Auto-detected Flash size: 32m 
Running Cesanta flasher stub... 
Flash params set to 0x0040 
Wrote 589824 bytes at 0x0 in 13.1 seconds (359.4 kbit/s)... 


You can now test your micropython by opening your favourite serial terminal such as minicom, putty etc. You need to open the serial line with 115200 as baud rate.

Getting started with Ampy:

The Ampy tool is very handy for uploading, download, running, resetting the micropython installed ESP8266.

(localpy)$ampy -p /dev/ttyUSB0 ls

Create a blink example:

The ESP-12-E module has a on-board LED connected to GPIO 2.

ampy -p /dev/ttyUSB0 put 
$ampy -p /dev/ttyUSB0 get 
import machine
import time
pin = machine.Pin(2, machine.Pin.OUT)

while True:
ampy -p /dev/ttyUSB0 run 

Running the permanently:

The Micropython runs the and the run the if that exist.  Using Ampy its possible to copy the as as show below.  After successfully copying the file, reboot the ESP8266 and it should blink automatically 🙂

$ampy -p /dev/ttyUSB0 put  / 

I experienced some inconsistency in the entire flow, but things were working when I opened the serial console (I used putty), immediately after flashing micropython and then using the Ampy.

One thought on “How to use Micropython and Ampy with ESP8266

Leave a Reply

Your email address will not be published. Required fields are marked *