ESP8266 and MicroPython: first scripts

So I have MicroPython installed on an Adafruit Huzzah ESP8266 dev board. The next step is to get it to run something.

MicroPython establishes a basic filesystem using the board’s flash memory. This is handy for storing configuration and data files. And it supports a directory structure.

But there are two files in particular you need to get to grips with – boot.py and main.py.

Every time MicroPython boots it runs boot.py. So this is a good place to put essential configuration stuff. Here’s my first attempt at a boot.py script:

#boot.py
import gc
import network
import ubinascii

print('[boot]')

SSID = '<***redacted***>'
password = '<***redacted***>'

wlan = network.WLAN(network.STA_IF)	# STA_IF means station interface
ap = network.WLAN(network.AP_IF)	# AP_IF means access point
print('Shutting down access point...')
ap.active(False)	# shut down access point

if not wlan.isconnected():
    print('Connecting to wifi network...')
    wlan.active(True)
    wlan.connect(SSID, password)
    while not wlan.isconnected():
        pass

(ip, netmask, gateway, dns) = wlan.ifconfig()

gc.collect()
print(' ')
print('Memory free:', gc.mem_free())
print(' ')

After boot.py has run, MicroPython looks for a file called main.py and, if it finds it, runs that. Here’s my first main.py script.


# main.py script for ESP8266
gc.collect()
print('[main]')
print('Welcome to the ESP8266 Huzzah!')

if not wlan.isconnected():
    print('Wifi not connected')
else:
    print('ip :', ip)
    print('netmask:', netmask)
    print('gateway:', gateway)
    print('DNS :', dns)
    print('MAC :', ubinascii.hexlify(wlan.config('mac'),':').decode('utf-8'))

And here’s the output as a result of those two scripts running. Notice anything odd (apart from the garbage at the beginning which is to do with the board initially sending data at too high a baud rate)?

Well, the second script, main.py, uses modules (network, gc and ubinascii) imported in the first script. It also accesses variables (ip, netmask, gateway and dns) and objects (wlan, gc) created in the first script. In other words, everything set up in boot.py is still in scope when main.py runs.

I haven’t yet found the reason for this, but I’m assuming that the scripts are, in effect, just being fed to the Python REPL. In fact, after main.py has completed you’re dropped back into the REPL where all the modules, objects and variables are still available.

Not that this is a problem. This is just a microcontroller, after all. Typically, the way you’ll use it is to use boot.py for some kind of standard configuration and then have a single script in main.py that will use an infinite loop to run your main program.

There’s not much to note in the code above – it’s pretty basic – other than that I turn off the access point mode of the board. It appears to be on by default and I don’t need it.

Remote control

As I noted in my previous post, you’ll need some way of getting scripts on to and off your ESP8266 board. Currently, I’m using Adafruit’s ampy. This is a command-line Python script that runs on your PC. You use ‘put’ to push a file to the ESP8266 board, ‘get’ to move one the other way, ‘ls’ to list files and there are command to delete files and create and delete directories.

With each command you have to specify the port (/dev/cu.usbserial-FTFXZ7LX on my Mac using the FTDI cable) and baud rate. I mentioned that I’d set up macros (eg, BD=115200) to save typing, but it turns out this is the difficult way to do it. Ampy allows you to create environment variables – eg.

export AMPY_PORT=/dev/cu.usbserial-FTFXZ7LX
export AMPY_BAUD=115200

You can do this manually at the beginning of each session or even set them up in your Bash config. Then you can forget about them. It means that the following two commands become equivalent:

ampy -b115200 -p/dev/cu.usbserial-FTFXZ7LX put boot.py
ampy put boot.py

I know which one I’d rather type.

(Also, ampy assumes a baud rate of 115200, so you could just forget about baud altogether.)

More power

Ampy is fairly crude. It does the job fine, but if you want something a little more powerful then rshell is a good choice. As you’d expect from the name, this creates a shell-like environment with *nix-like filesystem commands (cd, cat, cp, edit, ls, rm, mkdir and more – even rsync) and has the ability to access the REPL from your PC. I’m still getting to grips with it, but it seems like the way to go although I’ll probably stick to ampy for just pushing files to the board.

Okay, so now I can program the ESP8266 I just need to think of something to do with it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.