Hi Readers,

Today I would like to tell you another way of programming and controlling ESP8266. This time instead of using ‘AT’ commands I will show you simple application which uses Lua programming language which will be uploaded directly to our ESP8266.

What Lua is? (from ESP8266 wiki)

There are 2 “branches:” of Lua now for ESP8266, and both are now open source. Confusingly, both use the word Node in their name – NodeMCU and NodeLua. As lua.org says, Lua is a powerful, fast, lightweight, embeddable scripting language. Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics.

Lua is small: the Lua interpreter easily fits in under 100 kilobytes including the base libraries.
Lua is fast: independent benchmarks show that Lua is often amongst the fastest interpreted dynamic languages. The Lua interpreter has been extensively tuned for high performance.

What LUA means?

“Lua” (pronounced LOO-ah) means “Moon” in Portuguese. (from https://www.lua.org/about.html)

More about Lua?

If you want to know more about LUA language it’s history, strong and weak points and everything you also would like to know you can visit lua homepage: http://www.lua.org

What editors should I use?

You can use number of tools to user Lua and upload it to ESP8266 I used free tools:

LuaLoader: https://gettoknowthebob.wordpress.com/2016/04/11/lualoader-unknown-but-surprisingly-useful-tool/

ESplorer: https://gettoknowthebob.wordpress.com/2016/04/17/esplorer-probably-best-lua-editor/

And arduino studio, this one I liked the most. Also I have just tried Atom so I don’t want to give you my opinion on it after couple minutes spend together but I believe after spending some time on configuration it. It maybe coolest IDE I ever used for ESP8266.

Of course if you didn’t change your firmware from orginal ATfirmware you will need to replace it with another firmware which you can find with whole description on my blog: https://gettoknowthebob.wordpress.com/2016/04/08/flashing-esp8266-nodemcu-with-latest-firmware/

Examples!

First and the basic example I used is this simple code which turns on and off Nodemcu LED. Here you can see how clear and easy this code looks like. Pin number, Pin mode, Write 1 or 0 and fine. It works!

lighton=0
pin=4
gpio.mode(pin,gpio.OUTPUT)
tmr.alarm(1,3000,1,function()
if lighton==0 then
lighton=1
gpio.write(pin,gpio.HIGH)
else
lighton=0
gpio.write(pin,gpio.LOW)
end
end)

But you can do much more. As we know that all about ESP8266 is Wifi then let’s try connect our board with one or another network:

wifi.setmode(wifi.STATION)
wifi.sta.config("ssid","password")
wifi.sta.connect()
print(wifi.sta.getip())

Or from nodemcu simplest http server:

srv=net.createServer(net.TCP) 
srv:listen(80,function(conn) 
    conn:on("receive",function(conn,payload) 
    print(payload) 
    conn:send("

Hello, NodeMCU.

)
    end) 
end)

I hope you had nice time playing with Lua language as I did 🙂

Github repository: https://goo.gl/ii9xsz

Be positive and stay calm!

Cheers,

Radek

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