New Lua update is open source

No I don’t mean the new implementation that has sprung up and is early days – I mean the one that Zeroday put together. As of yesterday it’s using the new SDK 0.9.4 (I’m on the lookout for AT binaries using 0.9.4 if anyone knows where they are hiding)…

LuaSo what’s new? Well, the screen powers up making it clear which build it is on and a quick heap check shows a healthy 18k of RAM available. Sadly that crap is still in the firmware on power up.. I don’t know why they feel the need to leave that in – but as you can see it’s based on 0.9.5 and has 18096 bytes available.

Of course that’s all very nice but the implementation I was using yesterday drove me nuts due to it’s inability to call a simple web page reliably. At the time I had no idea if it was me, the interpreter, the board or what – but having had my AT implementation calling the site repeatedly without a hitch since then I can categorically state the issue was with the interpreter.

So what’s changed? Well for one thing the code is open source so HOPEFULLY instead of people rushing off to make a name for themselves, we might see some fresh eyes looking at this – in fact that HAS to happen really – lets hope we can look ahead to more features – more RAM. I’d like to see support for serial LEDs in the code as there is no way you can bit-bang with the interpreter fast enough to drive them  – simultaneously there is no doubt in my mind they are the future of lighting (single wire, 5v operation high brilliance RGB leds – look them up on Ebay). It would REALLY be nice if one of these boards could drive them with little more than a 3v3 regulator and a heavy duty 5v supply.

So, back to reality – I put my code back in – the same code that was driving me nuts yesterday.. and….

Sadly… same again… run the code live – it goes off to my web page and gets the time – make a file to do it and..

Note that a simple timer function and web page reduce the 18k of RAM to 12k!!

NodeMcu 0.9.4 build 20141222  powered by Lua 5.1.4
lua: cannot open init.lua
> dofile(“setclocktimer.lua”)
> 13544 13240 12944 12624 12648
Sent time request
time request processed
Sent time request
time request processed
04:32:35 23/12/2014

Once running I called print(head.node()) to check the RAM and sure enough – 12k remaining even though I’m erasing the COMM variable when I’m done with it. There is no further loss after that which is a good thing! The really excellent ESPLORER ide from Russia (I believe) is having trouble with the new version, something to do with handshaking – I found I had to have several attempts (and resets) to get the code up there – something that did NOT happen with the previous version of Lua – but sadly the guy does not have and English website so I don’t know how to leave him a message.

second=0 minute=0 hour=0 day=0 month=0 year=0

tmr.alarm(2,8000, 1, function()
if (year==0) then
     print(wifi.sta.getip() .. ” ” .. node.heap())
     conn=net.createConnection(net.TCP, 0)

          conn:send(“GET /time1921681974.php HTTP/1.1\r\nHost: ” .. host .. “\r\n”
              ..”Connection: keep-alive\r\nAccept: */*\r\n\r\n”)
          print(“Sent time request”)

     conn:on(“receive”, function(conn, payload)
          hour,minute,second,day,month,year=string.match(payload,”.*timestr=(%d+):(%d+):(%d+) (%d+)-(%d+)-(%d+)”)
          print(“time request processed”)

     tmr.alarm(1,1000, 1, function()
     if second>=60 then
        second=0 minute=minute+1
        if minute>=60 then
            minute=0 hour=hour+1
            if hour>=24 then
                hour=0 day=day+1
                if day>=32 then day=0
    print(string.format(“%02d:%02d:%02d %02d/%02d/%04d”,hour,minute,second,day,month,year))

I suggest if you want to try this – you get my PHP code and make your own as the page is temporary – clearly I don’t want everyone using my web server to get the time but you can do this with any PHP page… as you can see the code is split in two.. and ASSUMES you already have a connection to your router.

Run as is it works most times. A timer (timer2) starts up 8 seconds after power up (easily enough for the board to pick up the WIFI connection) and attempts to call the web page . Because of the way Lua works (ie non-blocking) it sets up 2 callbacks – one for when you have a connection (and one for when you receive something from that connection. Finally the connect is made to my website.

Until the time is picked up nothing else happens. Hopefully, the connection is made and a GET request is made to the site for the time. Then when the board gets a response, the time is set and the connection is emptied to save space.

AT THAT TIME the timer (2) is stopped as it is no longer appropriate – and timer (1) is started up to show the time every second – incrementing seconds, minutes etc each time. The time is displayed once per second.

My new ESP-01 tester boardOnce it gets going it is fine – and if you run the code, it is likely to connect first time – but once you put it in a file – it seems to take several attempts to get the time. The AT code does NOT do this – only the Lua code.  Incidentally, Zeroday was very helpful in showing me how to use the callbacks – but it’s still not quite right!

And why it ends up eating up 6K of precious RAM is WAY beyond me…. thoughts welcome and if anyone speaks Russian – please let the ESPLORER designer know his IDE is acting up a little in the latest version.. also it he would care to NOT clear the screen when you close the connection – it would really prevent a lot of heartache when trying to screengrab output!!

(the picture of the little red board on the right is my new (couple of bits missing) board for testing ESP-01s – had a few of them made in China as I got sick of wires all over the bench. Plugs straight into an ESP-01 and an FTDI cable, has regulator, LEDS and some level shifting – more on that when I find out if it works or not).

NodeMcu 0.9.4 build 20141222  powered by Lua 5.1.4
lua: cannot open init.lua
> dofile(“setclocktimer.lua”)
> 13544 13256 13280
Sent time request
time request processed
04:52:55 23/12/2014
04:52:56 23/12/2014
04:52:57 23/12/2014
04:52:58 23/12/2014
04:52:59 23/12/2014

> 04:53:00 23/12/2014

Note: The new Facebook page is coming along – still struggling to get the likes up to 100 – clearly Google+ is a lot more popular for this kind of thing.

5 thoughts on “New Lua update is open source

  1. Hello Peter,

    Have you ever tried manually invoking Lua’s garbage collector (see You can force a collection by issuing ‘collectgarbage(“collect”)’ at opportune times in your code. It will obviously slow things down but I suspect your RAM concerns are a higher priority. I’d be interested to know if it reclaims any of your “lost” memory.

    Another option might be to pre-compile your lua code into lua byte-code. You can use the ‘luac’ program to do this but the standard version doesn’t produce architecture independent byte-code. I think the eLua program has a variant that you can tune (via command-line args) to emit byte-code compatible with the ESP8266. Looks like NodeMCU code has it in the sources but is not currently compiled. It would also be interesting to know what (RAM) optimizations they’re using in that project because there are a couple of patches ( and that might improve matters. Perhaps if you correspond further with the developer you can gain additional insight. The pre-compiling option, although not as flexible for general prototyping and trying things out, might save you a little RAM because the interpreter doesn’t have to convert all that text you feed it to the byte-code it actually runs. Again, might help but requires some experimentation.

    Finally, another platform you might want to keep an eye on although it’s more expensive: (The Photon).

    Not released (yet) but for $20 you get a complete development board and a better/faster processor. Still, the $4.50 ESP8266 is tough to beat – certainly for simple data-logging IoT applications.

    • I’ve not tried that – I would be surprised if that command exists in the ESP8266 implementation (floats are also not implemented) as Zeroday would surely have mentioned it. Worth a try though.

      Recompiling code – you’re assuming others are familiar with how to do this – I’m quite at home compiling in Visual studio, or indeed the Arduino environment but having had a couple of attempts to bring the Linux setup for the ESP8266 SDK etc into Windows I’ve decided life is too short, the output is WAY too verbose. If only the whole thing could be brought into Atmel Studio or Visual Studio.

      Hopefully, those more familiar with this environment will read your response in here and now that the code is open source, perhaps we can look forward to improvements. I keep going back to the code, getting disappointed and going back again to the AT commands.

      I looked at the Photon and you’re right – it is too expensive – let’s face it you can get a damned good full Ethernet card for Arduino for less than $20 which has none of the issues we’re facing. The challenge here is to get this board which costs peanuts (less than $4.50 I may add, cheapest I’ve seen in the UK (via China) is £2.30 freepost which is, what, well under $4?

      Spark’s website doesn’t work properly in my (Chrome) browser so I can’t check but I’ll bet the postage from the USA to Britain makes the product unfeasible for us.

      Thanks for the feedback Glenn.

Leave a Reply to Scargill Cancel reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s