IoT - ESP8266 WiFi Module with LUA Firmware
Jan 3, 2015- Setup: This how-to was done using a Linux-PC (LMDE-Linux Mint Debian).
- Scope: PoC with LUA Firmware.
Using GtkTerm on Linux make sure that the configuration of the baudrate is set to 115200 (file: “~/.gtktermrc”).
[default]
port = /dev/ttyUSB1
speed = 115200
To get the LUA firmware for the esp8266 module (version ESP-01):
git clone https://github.com/nodemcu/nodemcu-firmware.git
Copy the file “nodemcu_512k_latest.bin” from folder pre_build/latest and connect GPIO0 to low; after that restart the module (with GPIO0 to low it boots to flashable mode). How the connection from USB to the module can be done has been documented in a (previous post)[http://hanneslehmann.github.io/2014/12/ESP8266Module/].
Open a linux terminal, navigate to the already known Python script for flashing the firmware:
./esptool.py -p /dev/ttyUSB1 write_flash 0x000000 nodemcu_512k_latest.bin
Connecting...
Erasing flash...
Writing at 0x0007ef00... (100 %)
Leaving...
Change baudrate of gtkterm:
[default]
port = /dev/ttyUSB1
speed = 9600
Now opening again GtkTerm you can enter commands (note: every line is a command, the sign “>” indicates that the module expects a lua command).
> wifi.setmode(wifi.STATION)
> wifi.sta.config("mywifi","mypass")
stdin:1: unexpected symbol near 'char(27)'
> print(wifi.sta.getip())
stdin: bad header in precompiled chunk
> print(wifi.sta.getip())
192.168.1.9
>
Ok! This seemed to work, our esp8266 module is connected to our network (and has the ip 192.168.1.9)! If you get some errors like “stdin:1: unexpected symbol near ‘char(27)’” or “stdin: bad header in precompiled chunk” then your terminal doesn’t support backspace or arrow input (either copy&paste issue, or you have done a correction of your input). Don’t worry, just repeat the command.
I then copy and pasted the example for the simple http server from here; note: it even worked without correct line breaks!
> srv=net.createServer(net.TCP) srv:listen(80,function(conn) conn:on("receive",function(conn,payload) print(payload) conn:send("<h1> Hello, NodeMcu.</h1>") end) conn:on("sent",function(conn) conn:close() end) end)
After browsing to http://192.168.15.9/ I get the expected response “Hello, NodeMcu.” and following log entries in the terminal:
> GET / HTTP/1.1
Host: 192.168.1.9
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:34.0) Gecko/20100101 Firefox/34.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
GET /favicon.ico HTTP/1.1
Host: 192.168.1.9
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:34.0) Gecko/20100101 Firefox/34.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
GET /favicon.ico HTTP/1.1
Host: 192.168.1.9
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:34.0) Gecko/20100101 Firefox/34.0
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
To restart the module the command node.restart() can be used.
>node.restart()
> Z0�
G��fR�f� ��C�NodeMcu 0.9.4 build 20141230 powered by Lua 5.1.4
lua: cannot open init.lua
>
What happened? Our previous configured webserver vanished!! To make something permanent, the boot up message gives the hint: we need an “init.lua” file which is executed at the start and can be used to setup the module with the needed commands. But even if the webserver vanished, the settings for the home/wifi access have been stored, and after reboot of the module it reconnects automatically!
To make things easier, let’s use a small python script for uploading lua script files to esp8266:
git clone https://github.com/4refr0nt/luatool.git
This tool can be used to write a file into the wifi module, as it automates sending the file line by line.
Sample usage:
./luatool.py --port /dev/ttyUSB1 --src init.lua --dest init.lua --verbose
Attention: don’t forget to close your Serial monitor, otherwise the tool might not get the responses from the module and quit working (or disable the check)!
To solve my previous problem (arduino not getting the full message from the module, see post from 30DEC) I now created a lua.init file which I uploaded to route through the commands sent by tcp.
From the linux box I now can send a message like:
echo 'Hello' > /dev/tcp/192.168.1.9/2323
and it will be send to the serial output of the module.
I will keep on going to check out this nice little module (already ordered a new version with some more GPIOs).
Please let me know in case of any questions or clarifications needed!