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

C:\Users\antho>

Then erase the flash

C:\Users\antho\Downloads>esptool.py -p com5 erase_flash
esptool.py v2.0
Connecting........_
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>esptool.py --port com5 --baud 460800 write_flash --flash_size=detect 0 esp8266-20170526-v1.9.bin
esptool.py v1.2-dev
Connecting...
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)...
Leaving...

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
usl
add if1
#5 ets_task(4020e374, 29, 3fff7078, 10)
dhcp server start:(ip:192.168.4.1,mask:255.255.255.0,gw:192.168.4.1)
bcn 100
could not open file 'main.py' 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.active(True)
		sta_if.connect(ssid, passw)
		while not sta_if.isconnected():
			pass
	print('network config:', sta_if.ifconfig())
	sta_if.config('dhcp_hostname')

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

setup('yourSSID','yourPassword')

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

network config: ('0.0.0.0', '0.0.0.0', '0.0.0.0', '192.168.1.254')
ip:192.168.1.151,mask:255.255.255.0,gw:192.168.1.254
network config: ('192.168.1.151', '255.255.255.0', '192.168.1.254', '192.168.1.254')
>>> 

Keep a note of that first ip address - in this case '192.168.1.151' - 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 boot.py, 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://192.168.4.1:8266
WebREPL daemon started on ws://192.168.1.151:8266
Started webrepl in setup mode
>>> 

Now run WebREPL By default, the connect address will be 'ws://192.168.4.1:8266/' You need to change this to use the ip address you saved earlier. So, in my case ws://192.168.1.151:8266/. 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://192.168.4.1:8266
WebREPL daemon started on ws://192.168.1.151:8266
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 boot.py Enter boot.py 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
#esp.osdebug(None)
import gc
#import webrepl
#webrepl.start()
gc.collect()

Uncomment the webrepl lines so you have

# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
import gc
import webrepl
webrepl.start()
gc.collect()

and save the file

Now, using WebREPL, click the browse button under the Send a file label and find and select the boot.py 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()
['boot.py', 'port_config.py']

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 https://github.com/micropython/micropython.git
$ cd micropython/mpy-cross
$ sudo make

~/micropython/mpy-cross$ ./mpy-cross ~/python/test.py

test.py 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()
['boot.py', 'port_config.py', '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()
1

Links