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 https://github.com/espressif/esptool or https://pypi.python.org/pypi/esptool .
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)wget http://micropython.org/resources/firmware/esp8266-20170108-v1.8.7.bin 
(localpy)esptool.py --port /dev/ttyUSB0 erase_flash 
esptool.py v1.3 
Connecting.... 
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)esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0  esp8266-20170108-v1.8.7.bin 
esptool.py v1.3 
Connecting.... 
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)... 
Leaving... 

Output:

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
boot.py

Create a blink example:

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

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

while True:
 pin.low()
 time.sleep(1.0)
 pin.high()
 time.sleep(1.0)
ampy -p /dev/ttyUSB0 run blink.py 

Running the blink.py permanently:

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

 
$ampy -p /dev/ttyUSB0 put blink.py  /main.py 

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 *