Getting Started with MicroPython on ESP8266
O
Ohidur Rahman Bappy
MAR 22, 2025
Prerequisites
Flash the appropriate MicroPython firmware onto your ESP8266 board. I recommend using Thonny, as it is a convenient IDE for MicroPython, comes packaged with Python, and offers out-of-the-box support for MicroPython. Uploading files is also straightforward.
Useful Commands
- Use
help()
to list sample code. - Use
help('modules')
to view installed packages.
Simple LED Blinking Program
from machine import Pin
from time import sleep
led = Pin(2, Pin.OUT)
while True:
led.value(not led.value())
sleep(1)
Listing Files on Flash Memory
import uos
print(uos.listdir())
Enabling WebREPL
To enable WebREPL, open the boot.py
file and uncomment the following line:
import webrepl
The default password for the WiFi Access Point is micropythoN
.
Setting Up WebREPL
Connect via:
screen <PORT> 115200
Or, use the Thonny IDE. Then, run:
import webrepl_setup
Retrieve WiFi SSID
MicroPython v1.8.6-7-gefd0927 on 2016-11-10; ESP module with ESP8266
Type "help()" for more information.
>>> import network
>>> ap = network.WLAN(network.AP_IF)
>>> print(ap.config('essid'))
MicroPython-d0fa00
Change WiFi Access Point and Password
MicroPython v1.8.6-7-gefd0927 on 2016-11-10; ESP module with ESP8266
Type "help()" for more information.
>>> import network
>>> ap = network.WLAN(network.AP_IF)
>>> ap.active(True)
>>> ap.config(essid='MyESP8266', authmode=network.AUTH_WPA_WPA2_PSK, password='mypassword')
>>> print(ap.config('essid'))
MyESP8
- Download the webrepl client: GitHub
- Disconnect the USB to Serial converter from the computer.
- Connect the computer to the WiFi Access Point of the ESP8266.
- Open
webrepl.html
using Chrome or Firefox. - Connect via
ws://192.168.4.1:8266
.
Using ESPtool
Thonny uses esptool to communicate with ESP boards. If you prefer using it directly, use these commands:
Install esptool
pip install esptool
Write a Binary to the ESP
esptool --port COM4 write_flash 0x1000 my_app-0x01000.bin
Write to Multiple Flash Addresses
esptool --port COM4 write_flash 0x00000 my_app.elf-0x00000.bin 0x40000 my_app.elf-0x40000.bin
To run a program with the default bootloader, save the file as
main.py
on the ESP board.