Programming the ESP8266

The software for this project is available on GitHub, or you can download it as a Zip file by clicking the button:

Get the software and you will see two files. First is one called init.lua. This is the program that runs on the ESP8266 to collect the sensor data and push it to the local MQTT broker. We'll go over the mechanism for getting that file onto the ESP8266 in a minute. The second file is the Python program that runs on a Raspberry PI; it extracts the sensor data from the local MQTT broker and sends it to Adafruit.IO.

Here are the steps necessary to flash the init.lua program onto the ESP8266.

The ESP8266 is a wonderful little ecosystem and I've become quite fond of it lately. The Feather HUZZAH ESP8266 comes pre-flashed with the NodeMCU Lua interpreter, as does the HUZZAH ESP8266 breakout. This means you can program the WiFi chip directly using Lua.

When you are programming the ESP8266 with Lua, you can write any Lua programs you like to the board's flash memory. If you create a program named init.lua it will run when the board resets.

WARNING: if you crate a tight loop (or an infinite loop) in init.lua you will effectively brick the device and have to re-flash it with a new image!

Step 1 - Edit init.lua to set your local network and MQTT broker parameters.

The init.lua file from the GitHub repository has several variables that must be set correctly to match your local configuration. These are fairly obvious because they will say things like CHANGEME. Carefully edit init.lua and set these parameters accordingly. If you break your board, don't panic! You can always flash a new version of NodeMCU to fix it. Just see the page titled "How to Re-flash Your ESP8266

Step 2 - Load Flash Memory on the ESP8266

Once you have the code for the two Lua programs saved on your local machine and edited accordingly, you need to upload them to the flash memory of the ESP8266 device. I am on a Mac and use a tool called luatool that just uploads programs into the ESP8266 flash.

Luatool is written in Python so it should run on any OS that supports Python. You can download luatool from its GitHub repository.

Once you have loaded init.lua onto the board, you can press the reset button and the board should boot and run your light monitor code. You can test this by running the following command on your MQTT server:

Download: file
mosquitto_sub -v -t /lightsensor

You should see a stream of sensor IDs and values representing the light leval.

This guide was first published on Aug 20, 2016. It was last updated on Aug 20, 2016. This page (Programming the ESP8266) was last updated on Oct 13, 2019.