Using NodeMCU Lua

We have found Lua is best for typing in short commands for testing things out, we suggest Arduino IDE for the best and most-tested programming interface!

Each HUZZAH ESP8266 breakout comes pre-programmed with NodeMCU's Lua interpreter. As of this writing, we ship with NodeMCU 0.9.5 build 20150318  powered by Lua 5.1.4 but it may be more recent

The Lua interpreter runs on the ESP8266 and you can type in commands and read out the results over serial. A serial console cable is perfect for this! Use either an FTDI cable or any console cable, you can use either 3V or 5V logic and power as there is level shifting on the RX pin.

Don't forget to visit esp8266.com for the latest and greatest in ESP8266 news, software and gossip!

The ESP8266 uses a lot of current, so if you're getting flakey behavior make sure you are plugging your console cable into either a motherboard USB port or a powered USB hub. Don't use the 'extra' USB port on your monitor or keyboard.

Connect USB-Serial cable

Connect either your console cable or FTDI cable. If using FTDI, make sure the black wire goes to the GND (ground) pin

If using a console cable, connect the black wire to ground, red wire to V+, white wire to TX and green wire to RX

You will see the red and blue onboard LED flicker when powered up, but they will not stay lit.

Open up serial console

Next up, on your computer, use a serial console program such as CoolTerm (Mac) or Putty (Windows) or screen (linux). Teraterm seems to dislike the initial 115.2kbps data stream from the ESP8266 so you can try it but you'll possibly need to reset the terminal software.

Connect up to the COM or Serial port used by your cable, at 9600 Baud

Make sure you have turned off any hardware handshake or flow control

Also make sure you have line endings set to CRLF "\r\n" You may also want to turn on inter-character delay if you are pasting in large chunks of code. Each terminal software is different in setting it up, check the manual for the software you're using!

 

Once the terminal software is connected, click the Reset button on the HUZZAH ESP board to reset it and have it print out the welcome message:

If you don't get this message, first check that the red/blue leds flickered when you press the reset button. If they didnt, make sure the board is powered via V+ or Vbat. If they do flicker, make sure you've got the right baud rate selected in the software (9600) and the RX/TX/GND pins connected right

Hello world!

Ok we can now turn on an LED. There is a red LED on each board, connected to GPIO #0

NodeMCU's pinouts are not the same as the Arduino/gcc pinouts. We print the Arduino pinouts on the board so watch out!
Rev A of this board has GPIO #4 and #5 swapped (the modules changed pinouts on us) so if #4/#5 aren't working for you, try swapping! We'll fix in the next PCB run

Pin Notes

PCB/Arduino

NodeMCU/Lua

No pullups!

0

3

2

4

3

9

4

1

5

2

9

11

10

12

12

6

13

7

14

5

15

8

16

0

So to set the pin #0 LED on and off first make it an output by typing (not copy & paste)

gpio.mode(3, gpio.OUTPUT)

Turn the LED on by typing (not copy & paste)

gpio.write(3, gpio.LOW)

And off by typing (not copy & paste)

gpio.write(3, gpio.HIGH)

You can make this a little more automated by running a longer script.

For longer text, pasting can be difficult as the lua interpreter needs a little delay time between characters and also require CR-LF settings. For that reason you may want to paste each line and then hit return manually.

while 1 do
  gpio.write(3, gpio.HIGH)
  tmr.delay(1000000)   -- wait 1,000,000 us = 1 second
  gpio.write(3, gpio.LOW)
  tmr.delay(1000000)   -- wait 1,000,000 us = 1 second
end

The LED will now be blinking on and off.

Note that since its in a loop, its not possible to get it to stop via the interpreter. To stop it, click the Reset button again!

Scanning & Connecting to WiFi

We'll continue with a quick demo of scanning for WiFi and connecting.

Once you're back at the Lua prompt, set the ESP8266 into WiFi Client mode with

wifi.setmode(wifi.STATION)

Then you can run the scanner and have it print out the available AP's

-- print ap list
function listap(t)
      for k,v in pairs(t) do
        print(k.." : "..v)
      end
end
wifi.sta.getap(listap)

or for more detail...

-- print ap list
function listap(t)
      for ssid,v in pairs(t) do
        authmode, rssi, bssid, channel = 
          string.match(v, "(%d),(-?%d+),(%x%x:%x%x:%x%x:%x%x:%x%x:%x%x),(%d+)")
        print(ssid,authmode,rssi,bssid,channel)
      end
end
      
wifi.sta.getap(listap)

We can connect to the access point with wifi.sta.config and wifi.sta.connect - it will take a second or two to complete the connection, you can query the module to ask the status with wifi.sta.status() - when you get a 5 it means the connection is completed and DHCP successful

      wifi.sta.config("accesspointname","yourpassword")
wifi.sta.connect()
tmr.delay(1000000)   -- wait 1,000,000 us = 1 second
print(wifi.sta.status())
print(wifi.sta.getip())
    

WebClient example

Once you've got the IP address you can connect to adafruit, for example, and read a webpage and print it out:

sk=net.createConnection(net.TCP, 0)
sk:on("receive", function(sck, c) print(c) end )
sk:connect(80,"104.236.193.178")
sk:send("GET /testwifi/index.html HTTP/1.1\r\nHost: wifitest.adafruit.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")

You can also have the module do DNS for you, just give it the hostname instead of IP address:

sk=net.createConnection(net.TCP, 0)
sk:on("receive", function(sck, c) print(c) end )
sk:connect(80,"wifitest.adafruit.com")
sk:send("GET /testwifi/index.html HTTP/1.1\r\nHost: wifitest.adafruit.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")

This is just a light overview of testing out your HUZZAH ESP breakout! For much more, check out NodeMCU's tutorial page https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en for the details on what functions are available to you, as well as  http://www.lua.org to learn more about the Lua scripting language

Last updated on 2017-04-14 at 02.00.13 PM Published on 2015-04-24 at 06.24.48 PM