How to use 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 ESP8266. Since 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 esptool:
You can get this tool from here or
Since it is part of the CheeseShop I wanted to try that one 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 a  esp-12-e based self built board . Make it ready for flashing with the apropriate 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 bootmode.
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 baudrate

Getting started with Ampy:
The Ampy tool is very handy for uploading, download, running, reseting the micropython
installed esp8266.

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

Create a blink example:
The ESP-12-E module has a onboard 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 if I open the serial console (I used putty) immediately after flashing micropython and then use 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 *