[July 2017] Setting up Lua / MicroPython for the NodeMCU ESP8266

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

https://nodemcu-build.com/index.php

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!

Firmware Flasher

https://github.com/espressif/esptool

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

Output:
Collecting 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.

Flashing Process

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

Output:
esptool.py v2.0
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266
Uploading stub...
Running stub...
Stub running...
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.

Leaving…
Hard resetting…

Setting up an IDE (Optional)

ESPlorer is an IDE which will make it easier to upload and debug your Lua / MicroPython code.

https://esp8266.ru/esplorer/#download

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.

Lua:
station_cfg={}
station_cfg.ssid=”wifi-name”
station_cfg.pwd=”password”
=wifi.sta.config(station_cfg)
=print(wifi.sta.getip())

Of course, replace your Wifi details in the code.

Conclusions

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.

Useful Links

Documentation

https://nodemcu.readthedocs.io/en/master/

Old Guide

http://www.cnx-software.com/2015/10/29/getting-started-with-nodemcu-board-powered-by-esp8266-wisoc/

This is the guide that I was working off of. Thanks and shout out to cnxsoft!

Arduino Guide

http://www.instructables.com/id/Quick-Start-to-Nodemcu-ESP8266-on-Arduino-IDE/

The NodeMCU can also be programmed through the Arduino IDE, if that suits you more than Lua or MicroPython.

Amazon Link

https://www.amazon.com/HiLetgo-Version-NodeMCU-Internet-Development/dp/B010O1G1ES

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s