Remixing Pozetron for the ESP8266

The default firmware build for Pozetron on the ESP8266 is what we refer to as an “always on, always connected” build. This just means that it works best when the device is powered by mains power and always connected to WiFi. There are many situations where you want to develop for a device where one or both of these assumptions will not hold. This is the situation where you want to apply another remix. You can consider Pozetron device remixes something akin to Linux distributions, the same kernel, with lots of shared components but each built to serve different a purpose.

This is possible after all because the Pozetron software running on these devices is MIT licensed and available at https://github.com/pozetroninc/micropython

Setting up the dependencies

The OpenSource ESP SDK is the first thing we will set up.

Clone the repository, enter the directory and then run the make command.

$ cd ~
$ git clone --recursive https://github.com/pfalcon/esp-open-sdk.git
$ cd esp-open-sdk/
$ make
$ export PATH=`pwd`/xtensa-lx106-elf/bin:$PATH
$ echo "export PATH=`pwd`/xtensa-lx106-elf/bin:$PATH" >> ~/.bashrc

While that is building you may want to day dream about the awesome stuff you are going to build with MicroPython, it will take a while.

Note: If you can’t run the make command successfully you will first have to set up some essential tools for building the software. On Ubuntu you can use the following command to install them.

$ sudo apt-get install make unrar-free autoconf automake \
    libtool gcc g++ gperf flex bison texinfo gawk \
    ncurses-dev libexpat-dev python-dev python python-serial \
    sed git unzip bash help2man wget bzip2 libtool-bin

Getting MicroPython

Next you will clone the MicroPython repository.

We are using the Pozetron fork of MicroPython here because while a stock MicroPython experience is very nice and immensely useful, it doesn’t have the Pozetron modules to make it extra special. These modules are the ones that allow us to update our code over the air, deploy new code to our device, reboot it or even remotely inspect the logs from our devices.

Note: The addition of these modules, along with a tweak to the Makefile to make room for them, are the only changes from a ‘stock’ MicroPython experience.

$ cd ~
$ git clone --recursive https://github.com/pozetroninc/micropython.git
$ cd micropython/
$ make -C mpy-cross
$ cd ports/esp8266/
$ make axtls
$ ../../tools/make-frozen-py.py ./resources > ./modules/resources.py

Adding the Device ID, Key ID and Secret

If you have followed the Getting Started guide you will have generated or received a Device ID, Key ID and Secret. These values are used to uniquely identify the device to the Pozetron Cloud. We will now configure our firmware to include these values.

Use your favorite text editor to open the file and add in the values.

$ nano ~/micropython/esp8266/scripts/credentials.py
DEVICE_ID = b'a77eed66d15a3b3cba76573376695729bf7bce33d8e0997bfd8399f993fca4f8'
KEY_ID = b'36d43af16bfde6575efff2878c9b081b71bc3d5f3105cbfbead8ed62770d267d'
HMAC_SECRET = b'7ed951450f7bef3f5a359464bbf430a88af316aef3dae5097faf364cae323056'

Make the firmware

$ make

Congratulations! You are now the proud owner of a complete working MicroPython and have successfully created a firmware you can flash onto your ESP8266 module.

Flashing the firmware to your module

Assuming that your ESP8266 module is accessible at /dev/ttyUSB0 you can use the following commands to erase the flash, build a firmware, flash the firmware to the device and connect to the device over serial.

$ cd ~/micropython/esp8266/
# erase module's flash to avoid issues
$ esptool.py --port /dev/ttyUSB0 erase_flash
# bundle the resources
$ ../../tools/make-frozen-py.py ./resources > ./modules/resources.py
# build the firmware
$ make
# deploy the firmware
$ make PORT=/dev/ttyUSB0 deploy
# connect to the device via serial
$ picocom -b 115200 /dev/ttyUSB0

If you don’t have esptool installed you can get it with:

$ pip install esptool

If you don’t have picocom installed you can install it with:

$ sudo apt install picocom

This can all be shortened into one line:

$ esptool.py --port /dev/ttyUSB0 erase_flash && ../../tools/make-frozen-py.py ./resources > ./modules/resources.py && make && make PORT=/dev/ttyUSB0 deploy && picocom -b 115200 /dev/ttyUSB0