While hopping on the NodeMCU bandwagon, I couldn’t find any good, up to date guides on setting up the Lua environment on these little chips. If you’re reading this, I’ll assume you already know how neat these little things are and that you’re just here to get started.
I successfully got this working on my official board that I ordered off of Amazon (Link at the bottom of the article) on Ubuntu 16.04. Presumably this guide will also work with other Debian based OSs. I’m marking this entry with June / July 2017 as the guide I’m working off of was less than 18 months old and already outdated. (Link also at the end)
Specs I’m working with:
- NodeMCU ESP8266 Board V1.0
- Ubuntu 16.04
- Python 2.7.12
Before starting, install the latest version of Python 2 or 3.
Getting the Firmware
This site provides firmware customization. You select which modules you want to use, and it builds the binary for you. I recommend getting I2C and SPI for connectivity with peripherals.
CAUTION: The digital GPIO pins are only 3.3V tolerant! The analog pin can only handle up to 1.0V!
esptool is a Python program which will flash the binary onto the ESP8266 module.
In the terminal, type the following:
sudo python -m pip install esptool
Collecting pyserial>=2.5 (from esptool)
Downloading pyserial-3.3-py2.py3-none-any.whl (189kB)
100% |████████████████████████████████| 194kB 1.6MB/s
Collecting ecdsa (from esptool)
Downloading ecdsa-0.13-py2.py3-none-any.whl (86kB)
100% |████████████████████████████████| 92kB 2.2MB/s
Collecting pyaes (from esptool)
Installing collected packages: pyserial, ecdsa, pyaes, esptool
Successfully installed ecdsa-0.13 esptool-2.0 pyaes-1.6.0 pyserial-3.3
The normal pip install did not work for me. Look through esptool’s readme for alternatives if this does not work.
Plug in the NodeMCU into a USB port.
To flash the binary, you need to know the port it’s on.
For Linux users, this should be /dev/ttyUSB0/
Navigate to the folder with your binary.
Run esptool with your binary:
esptool.py --port /dev/ttyUSB0 write_flash 0x00000 ./[name of your binary].bin
Detecting chip type... ESP8266
Chip is ESP8266
Configuring flash size...
Auto-detected Flash size: 4MB
Flash params set to 0x0040
Compressed 413408 bytes to 266423...
Wrote 413408 bytes (266423 compressed) at 0x00000000 in 23.5 seconds (effective 140.6 kbit/s)...
Hash of data verified.
Setting up an IDE (Optional)
ESPlorer is an IDE which will make it easier to upload and debug your Lua / MicroPython code.
Other IDEs are available, and it is also possible to mess with programming it on the command line, but I think this is the best option for people who want to jump right into IoT.
The IDE is simple to set up. Download and unzip the file, and then run the Java jar.
Verifying the Setup
I successfully ran this short program, which proved that everything was up and running.
It simply connects to your access point and prints its IP address over serial.
Of course, replace your Wifi details in the code.
And that’s it!
My plan is to use this to automate video playing on my Chromecast. Somewhat annoyingly, CastV2 isn’t as simple as the original Chromecast’s HTTP requests. I’m reading up on that protocol, and I hope to have some lightweight code running soon. There’s a good chance that I’ll be updating my progress on this blog.
This is the guide that I was working off of. Thanks and shout out to cnxsoft!
The NodeMCU can also be programmed through the Arduino IDE, if that suits you more than Lua or MicroPython.
The board that I got off of Amazon is more expensive than what you can get on Ebay or AliExpress, but they’re genuine, as far as I can tell.