ESP8266 MicroPython

From ProjectPages
Jump to: navigation, search

Getting Started

The first thing to do is to follow this guide - Building and Running MicroPython on the ESP8266

Flashing the Firmware

There are several dependencies to install - see this guide for details

When it comes to flashing the firmware I was using Windows 10 on my PC and a USB serial adaptor to the ESP8266. Probably the same as most people. It may not be necessary to erase the flash first, but several forums recommend doing this before flashing the firmware. First make sure you are in the folder that contains the esptool directory

C:\>cd users\antho


Then erase the flash

C:\Users\antho\Downloads> -p com5 erase_flash v2.0
Detecting chip type... ESP32
Chip is ESP32D0WDQ6 (revision 0)
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 3.6s

I have downloaded the latest firmware from here

Now upload the firmware

C:\Users\antho\Downloads> --port com5 --baud 460800 write_flash --flash_size=detect 0 esp8266-20170526-v1.9.bin v1.2-dev
Auto-detected Flash size: 32m
Running Cesanta flasher stub...
Flash params set to 0x0040
Writing 589824 @ 0x0... 589824 (100 %)
Wrote 589824 bytes at 0x0 in 13.3 seconds (354.3 kbit/s)...

If all goes well you should see the leds on the serial adaptor and the ESP-12 blinking away furiously during this.

Getting Connected

Next is to check all is working. I used screen.

$ sudo screen /dev/ttyUSB0 115200

hit the reset button on the ESP8266 and you should get to the familiar >>> python prompt

Performing initial setup
bcn 0
del if1
add if1
#5 ets_task(4020e374, 29, 3fff7078, 10)
dhcp server start:(ip:,mask:,gw:
bcn 100
could not open file '' for reading

MicroPython v1.8.3-dirty on 2016-10-04; ESP module with ESP8266
Type "help()" for more information.

If you need to get back to the bash prompt then use Ctrl+A followed by Ctrl+D to exit Screen

Next I like to get connected to my WiFi network so I copy and paste this definition into screen.

def setup(ssid,passw):
	import network
	sta_if = network.WLAN(network.STA_IF)
	if not sta_if.isconnected():
		print('connecting to network...')
		sta_if.connect(ssid, passw)
		while not sta_if.isconnected():
	print('network config:', sta_if.ifconfig())

Then run it. Change 'yourSSID' and 'yourPassword' to match the credentials on your network


You should get loads happening on the screen ending with something like this:

network config: ('', '', '', '')
network config: ('', '', '', '')

Keep a note of that first ip address - in this case '' - as this is the ip address the router has assigned to your ESP8266.

Enable WebREPL

WebREPL is not enabled by default. To enable it on reboot we need to edit, but to do that we need WebREPL!

So, while you are still in your screen session from the previous section, type or copy the following two lines.

>>> import webrepl
>>> webrepl.start()
WebREPL daemon started on ws://
WebREPL daemon started on ws://
Started webrepl in setup mode

Now run WebREPL By default, the connect address will be 'ws://' You need to change this to use the ip address you saved earlier. So, in my case ws:// Then click connect On connecting you will get a welcome message and will be asked to enter a password. You will then be asked to confirm that password. You will need that password every time you connect using WebREPL The ESP8266 will automatically reboot

You won't be able to start WebREPL right away as it is now no longer running. So, in screen, we have to repeat the previous step

>>> import webrepl
>>> webrepl.start()
WebREPL daemon started on ws://
WebREPL daemon started on ws://
Started webrepl in setup mode

Check the ip address is correct and click connect. This time, when you enter the password you should get the python prompt

Now we want to get the file Enter in the box under the Get a file label and click on the button.

Save the file. Using your favourite text editor, e.g. nano, edit the file.

# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
import gc
#import webrepl

Uncomment the webrepl lines so you have

# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
import gc
import webrepl

and save the file

Now, using WebREPL, click the browse button under the Send a file label and find and select the file you just saved. Now send the file back to the ESP8266. WebREPL will now be enabled on boot.

This is how you send Micropython files to the ESP8266 You can write python script in a .py file, and send that to the ESP8266 file system.

You can see the files like this

>>> import os
>>> os.listdir()
['', '']

Create .mpy file

In order to get as much as you can out of this little python system you can cross compile your .py files to .mpy files. This means they are pre-compiled and use much less memory at load time.

So, once again, I am using an Ubuntu system.

First I made a new python file

def test():
	return 1

In a bash session

$ sudo git clone
$ cd micropython/mpy-cross
$ sudo make

~/micropython/mpy-cross$ ./mpy-cross ~/python/ is the source python file. This will generate test.mpy in the same folder.

You can now use WebREPL to send test.mpy to the ESP8266

We can use listdir again to see we have the file

>>> import os
>>> os.listdir()
['', '', 'test.mpy']

Now we import the module and show it's properties

>>> import test
>>> dir(test)
['__name__', 'test']

Then we can execute it!

>>> test.test()