ESP8266 Lua Adventures

After appearing on Hackaday the ESP8266 wifi modules took the world by storm. No wonder; for a few Euro a piece, these modules are already cheap as embedded wifi adapters. But with an SDK available, the ESP8266 is absolutely amazing.

Development for these modules has recently gotten even better with the release of Lua firmware for these modules. Not that I’m particularly fond of Lua as a language, but it is lightweight and its not too hard to whip up some code in it without much experience. And that is what I’ve been doing today, I wrote some Lua code that runs unattended op the module, scanning for nearby wifi access points and sending these, along with the signal strength (RSSI) value to a server in my home network.

I hope to build a rudimentary location based service with this, tracking the location of the ESP8266 module through the house.

The code for the location tag is pretty straightforward:

main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
wifi.setmode(wifi.STATION)
wifi.sta.config("essid", "wpakey")

function listap(t)
        myid = string.lower(string.gsub(wifi.sta.getmac(), "-", ":"))
        conn=net.createConnection(net.UDP, false)
        conn:connect(8021, "192.168.1.1")
        conn:send(myid..": begin scan\n")
        for key,value in pairs(t) do
                enc, rssi, bssid, chan = string.match(value,
                        "(%d),(-?%d+),(%x%x:%x%x:%x%x:%x%x:%x%x:%x%x),(%d+)")
                conn:send(myid..": "..bssid..","..rssi.."\n")
        end
        conn:close()
end
tmr.alarm(0, 30000, 1, function() wifi.sta.getap(listap) end)

Every 30 seconds the tmr.alarm() call fires wifi.sta.getap(). This function scans for wifi networks and makes a callback to listap() on completion of the scan. The listap() function munges the data from wifi.sta.getap() and pushes it through some UDP packets to a remote host for further processing.

Right now on the remote host there is just a netcat listening:

hessch@leibniz ~ $ nc -ul 8021
18:fe:34:yy:yy:yy: begin scan
18:fe:34:yy:yy:yy: 84:9c:a6:xx:xx:xx,-76
18:fe:34:yy:yy:yy: 00:20:91:xx:xx:xx,-47
18:fe:34:yy:yy:yy: d4:ca:6d:xx:xx:xx,-46
18:fe:34:yy:yy:yy: d8:bx:4c:xx:xx:xx,-85
18:fe:34:yy:yy:yy: begin scan
18:fe:34:yy:yy:yy: 84:9c:a6:xx:xx:xx,-74
18:fe:34:yy:yy:yy: 00:20:91:xx:xx:xx,-49
18:fe:34:yy:yy:yy: d4:ca:6d:xx:xx:xx,-48
18:fe:34:yy:yy:yy: d8:bx:4c:xx:xx:xx,-85

Eventually a script should be listening here that computes a likely position based on these values. Not sure yet how I’m hoing to implement this, maybe more about this in a later post.

Playing with Lua on the ESP8266 today, though, makes me believe that its not very likely that I’ll come anything near to an Arduino for anything wifi related.
The combination of a interpreted language like Lua and Wifi for this price is unbeatable, the only things that I’d really like to see still are IPv6 support and access to raw wifi packets, preferably in monitor mode.

I’m already planning a few more projects with these modules: a physical switch for my Lifx lightbulbs in the house and various sensors for energy measuring.

Comments