Running an LUA script on ESP8266 with NodeMCU

In our last NodeMCU post, we covered the installation of NodeMCU on our ESP8266 board. Now we can start to load full LUA script files onto the ESP8266. I will show you how you can do this with a quick example.

Needed software

You need a terminal software to communicate via the serial interface of the ESP8266. Here we have to use software that lets us change the transmission speed for each line. The LUA interpreter can some times not hold up with the serial transmission speed. So we have to add a little delay between each line.

I’m using a software called ESPlorer from 4refr0nt. This tool lets you use the default AT-based firmware on the ESP8266 and has NodeMCU support.

“Hello LED” LUA script

The “Hello world!” on a microcontroller is always a blinking LED. So we start with that task.

ledPin = 3	-- GPIO0
ledStatus = 0

gpio.mode(ledPin, gpio.OUTPUT) -- GPIO0 as output

tmr.alarm(0, 1000, 1, function() -- Set an 1000 ms alarm on timer 0
	ledStatus = 1 - ledStatus -- Toggle ledStatus between 0 and 1

	gpio.write(ledPin, ledStatus) -- Toggle LED
end)

The script above starts an alarm on timer 0. We define a new function inside this alarm which is called every 1000 ms. The function takes the current value of ledStatus and toggles its values between 0 and 1. This value will be used to set the GPIO pin for the LED to 0 or 1, which represents OFF (0) or ON (1).

ESPlorer with the Hello LED script
ESPlorer with the Hello LED script

In the ESPlorer you connect to your board’s serial port first. Then you can write your code into the “Scripts”-tab and save it to your computer. The button “Save to ESP” will save the file on the ESP8266 file system.

If you want to start a script at the start of your ESP8266 module, you should save it as init.lua on the file system.

Here you can find other NodeMCU related posts.

Leave a Comment