Arduino based controller boards have been all time favorite for developing prototypes and small projects.
Arduino uses C++ style of coding. For many new starters it may not be the best programming language with start with.
A promising alternate to Arduino is micropython firmware. This is available for variety of boards.
In this article, I will cover how to flash firmware, connect to WiFi, write and upload you own python code on ESP8266 controller.
First connect controller on USB port of Linux machine and detect USB port in /var/log/messages
Mar 9 08:21:54 scorpio kernel: [38233.463891] usb 1-1.1: new full-speed USB device number 7 using ehci-pci
Mar 9 08:21:54 scorpio kernel: [38233.603119] usb 1-1.1: New USB device found, idVendor=10c4, idProduct=ea60, bcdDevice= 1.00
Mar 9 08:21:54 scorpio kernel: [38233.603122] usb 1-1.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Mar 9 08:21:54 scorpio kernel: [38233.603124] usb 1-1.1: Product: CP2102 USB to UART Bridge Controller
Mar 9 08:21:54 scorpio kernel: [38233.603126] usb 1-1.1: Manufacturer: Silicon Labs
Mar 9 08:21:54 scorpio kernel: [38233.603127] usb 1-1.1: SerialNumber: 0001
Mar 9 08:21:54 scorpio kernel: [38233.611157] cp210x 1-1.1:1.0: cp210x converter detected
Mar 9 08:21:54 scorpio kernel: [38233.613257] usb 1-1.1: cp210x converter now attached to ttyUSB0
You can notice in above logs that controlled is hooked on ttyUSB0
Download firmware for ESP8266 from micropython download page
wget -c http://micropython.org/resources/firmware/esp8266-20191220-v1.12.bin
HTTP request sent, awaiting response... 200 OK
Length: 619828 (605K) [application/octet-stream]
Saving to: ‘esp8266-20191220-v1.12.bin’
esp8266-20191220-v1.12.bin 100%[=====================================================================================>] 605.30K 443KB/s in 1.4s
2020-03-09 08:28:39 (443 KB/s) - ‘esp8266-20191220-v1.12.bin’ saved [619828/619828]
Install esptool to manage firmware of ESP board.
python3 -m pip install esptool
Collecting esptool
Downloading esptool-2.8.tar.gz (84 kB)
|████████████████████████████████| 84 kB 297 kB/s
Successfully built esptool
Installing collected packages: esptool
Successfully installed esptool-2.8
Clear default firmware from ESP8266 controller board using esptool
esptool.py --port /dev/ttyUSB0 erase_flash
esptool.py v2.8
Serial port /dev/ttyUSB0
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: cc:50:e3:05:08:a2
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 2.0s
Hard resetting via RTS pin...
Flash downloaded micropython firmware on ESP controller
esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash --flash_size=detect 0 esp8266-20191220-v1.12.bin
esptool.py v2.8
Serial port /dev/ttyUSB0
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: cc:50:e3:05:08:a2
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Flash params set to 0x0040
Compressed 619828 bytes to 404070...
Wrote 619828 bytes (404070 compressed) at 0x00000000 in 35.7 seconds (effective 139.0 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting via RTS pin...
Connect to micropython REPL on ESP controller
miniterm /dev/ttyUSB0 115200 # passing USB serial port and baud rate
--- Miniterm on /dev/ttyUSB0 115200,8,N,1 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
>>>
Press return/enter once or twice to get REPL prompt. Now you are connected to python on controller.
To terminate connection, press CTRL+]
List built in micropython modules
>>> help('modules')
__main__ http_client_ssl uasyncio/__init__ upysh
_boot http_server uasyncio/core urandom
_onewire http_server_ssl ubinascii ure
_webrepl inisetup ucollections urequests
apa102 lwip ucryptolib urllib/urequest
btree machine uctypes uselect
builtins math uerrno usocket
dht micropython uhashlib ussl
ds18x20 neopixel uheapq ustruct
esp network uio utime
example_pub_button ntptime ujson utimeq
example_sub_led onewire umqtt/robust uwebsocket
flashbdev port_diag umqtt/simple uzlib
framebuf ssd1306 uos webrepl
gc sys upip webrepl_setup
http_client uarray upip_utarfile websocket_helper
Plus any modules on the filesystem
Connect ESP controller to WiFi
>>> import network
>>> sta_if = network.WLAN(network.STA_IF)
#Check current state of WiFi
>>> sta_if.active()
False
# Turn if on
>>> sta_if.active(True)
#8 ets_task(4020f4d8, 28, 3fff9de8, 10)
# Verify state again
>>> sta_if.active()
True
# Now connect to WiFi Access point
>>> sta_if.connect('SSID', 'Password')
# Verify
>>> sta_if.isconnected()
True
# Check IP config assigned by Access point
>>> sta_if.ifconfig()
('192.168.0.107', '255.255.255.0', '192.168.0.1', '192.168.0.23')
# Above output has IPaddress, Netmask, Gateway and DNS server
# To disconnect from WiFi
sta_if.disconnect()
# Verify
>>> sta_if.isconnected()
False
boot.py and main.py python program files
There are two files where you should put your code in.
boot.py – executed at bootup, you can add startup config and any other things you want to do at bootup operation.
main.py – executed after boot.py, contains main program.
Upload code on board from Linux machine using adafruit-ampy
Install adafruit-ampy on linux machine.
python3 -m pip install adafruit-ampy
Installing collected packages: adafruit-ampy
Successfully installed adafruit-ampy-1.0.7
Lets us list files on board, From Linux machine issue following command.
ampy --port /dev/ttyUSB0 --baud 115200 ls
/boot.py
Read content of boot.py from ESP board
ampy --port /dev/ttyUSB0 --baud 115200 get boot.py
# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
import uos, machine
#uos.dupterm(None, 1) # disable REPL on UART(0)
import gc
#import webrepl
#webrepl.start()
gc.collect()
Let us write a simple program to connect to WiFi and upload it using ampy.
Save following content in main.py on linux machine.
import network
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect('SSID', 'Password')
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
Upload main.py to ESP board from Linux machine using ampy.
ampy --port /dev/ttyUSB0 --baud 115200 put main.py
Check uploaded main.py on board.
ampy --port /dev/ttyUSB0 --baud 115200 get main.py
import network
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect('SSID', 'Password')
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
Verify WiFi state of WiFi before rebooting.
>>> import network
>>> sta_if = network.WLAN(network.STA_IF)
>>> sta_if.isconnected()
False
Reboot ESP board. After reboot, connect to micropython REPL using miniterm and verify WiFi.
>>> sta_if = network.WLAN(network.STA_IF)
>>> sta_if.ifconfig()
('192.168.0.107', '255.255.255.0', '192.168.0.1', '192.168.0.23')
…..and you see main.py execution has connected board with WiFi network.
From this point you can take ESP8266 and micropython to the heights of your imaginations and innovations.