Lua Revisited

I like to revisit things I’ve looked at in the past just to see if anything has changed. And so it was that I went off to the NODEMCU site to take a look at how it is coming on. NodeMCU is software for the ESP8266 which runs an interpreted language called LUA. As we know, interpreted languages are a lot safer and less crash prone than compiled languages…. right?

There is a great tool now that lets you pick which modules you are interested in. I selected what I would think would be the most useful one, ignoring chips I’d never heard off and therefore would not be able to test tonight.

Your NodeMCU custom build finished successfully. You may now download the firmware:

- float: http://nodemcu-build.com/builds/nodemcu-master-21-modules-2016-07-07-23-15-02-float.bin

- integer: http://nodemcu-build.com/builds/nodemcu-master-21-modules-2016-07-07-23-15-02-integer.bin

This was built against the master branch and includes the following modules: adc, bmp085, cjson, dht, enduser_setup, file, gpio, http, i2c, mdns, mqtt, net, node, ow, pwm, sntp, spi, tmr, uart, wifi, ws2812.

The files are guaranteed to be available for download for 24h.

I think that is very neat.  Sadly, assumptions have been made. No-where could I find how you were supposed to get this BIN file onto the ESP. Thankfully I already knew the answer and a quick search of the web showed that the code should be located at ZERO.   I grabbed the nodemcu programmer and fitted out one of my newly refurbished ESP-01 modules – just because it was on the desk.

Success. The module powered up immediately. Off I went for some examples – and of course the most basic example of all is that of the flashing light. I grabbed the example code.

lighton=0
tmr.alarm(0,1000,1,function()
if lighton==0 then
    lighton=1
    led(512,512,512)
    -- 512/1024, 50% duty cycle
else
    lighton=0
    led(0,0,0)
end
end)

Can’t get any simpler, can it? The result:

> PANIC: unprotected error in call to Lua API (stdin:4: attempt to call global 'led' (a nil value))
c_ÇRSöâFJSúfJÃîXËãÛã‡ãËSÎËÏÿöý

Ok, For now, I’ll stick with my C code – and look in again in a few months. This kind of thing is why I abandoned NodeMCU in the first place.  A simple “sorry you need this module” would have sufficed.

Facebooktwittergoogle_pluspinterestlinkedin

12 thoughts on “Lua Revisited

  1. Well if you call a function that is nowhere defined - led() - a c compiler throws an error as well. Where did you take that example code from?

      1. (stdin:4: attempt to call global 'led' (a nil value))

        Thats the simple "sorry you havent defined the function led()". Every undefined variable, table, function in LUA has the value "nil". A panic doesnt do any harm. The module just reboot. You needed to add the PWM code as well, then it should work:

        function led(r,g,b)
        pwm.setduty(1,r)
        pwm.setduty(2,g)
        pwm.setduty(3,b)
        end
        pwm.setup(1,500,512)
        pwm.setup(2,500,512)
        pwm.setup(3,500,512)
        pwm.start(1)
        pwm.start(2)
        pwm.start(3)
        led(512,0,0) -- red
        led(0,0,512) -- blue

        There is the led()-function defined. You should invest a little more time into LUA nodemcu, it is a damn productiv workflow especially because you dont have to compile and flash all the time. You should have a look at

        http://www.esp8266.com/wiki/doku.php?id=nodemcu-unofficial-faq#how_do_i_avoid_a_panic_loop_in_initlua

        (Current versions of the ESP8266 run the SDK over the native hardware so there is no underlying operating system to capture errors and to provide graceful failure modes, so system or application errors can easily “PANIC” the system causing it to reboot. Error handling has been kept simple to save on the limited code space, and this exacerbates this tendency. Running out of a system resource such as RAM will invariably cause a messy failure and system reboot.)

        You shouldnt talk about that project that badly when youve just invested 5 minutes. I mean it a f#*ing interpreter on that MCU. Just for that fact it works marvelous. But your whole article doesnt sound like that was a serious try to go back to nodemcu. Keep an ESP with nodemcu and play a bit longer with it. Btw the ESP-01 has not enought GPIOs to drive a 3pin RGB led like the code suggests.

        1. Hi Lny- oh don't get me wrong I invested a LOT of time into it - learned Lua, even bought a book or two on it - suffered all the original problems of insufficient RAM - ages ago - spent WAY too much time - I just thought I'd give it another go as it is so long since I looked in. I really would have thought by now that it would not CRASH and mess up FLASH content just because of a missing function. So - sorry - I've not just "invested 5 minutes" - as the title suggests, it was a quick revisit - and yes I'm well aware of ESP-01 limits - I just happened to have one handy - I normally use ESP-12. I'm sure it is fine for many folk, but for now - I think I'll stick with C - I have my own pretty massive C code - which never crashes and runs 24-7. But I will look back in from time to time.

          1. (read last paragraph first)

            You know you dont need to buy books to learn any language nowadays. Internet and stackoverflow is the way to go (dont waste money). It sounded like youre new to Lua. I'm far from beeing a Lua guru (or any other language) but the error message is pretty clear. On another lua interpreter you wont get more info.

            Sure I'm suffering from low RAM as well but if you are willing(!) to adjust your style of coding it is well possible to overcome most of the problems (btw: stuffing the module with nearly ever possible lua-module is a good way to burn RAM as well - compile only the ones you really need).

            How I said: its not messing messing up FLASH just because of a panic. After a reboot its like nothing happened. The error you were facing is like a segmentation fault. The only safe way is to terminate the program i.e. reboot. What else should it do? Execute some code from somewhere? Theres no compiler in between checking your code, you have to do it by your own. A reboot takes 2secs and you good to go again. How long does it take to compile... and flash... and how easily can you debug your deployed code? Sure you need to use print statements but it is all instant! No need to compile-flash between each step of the binary search for the error. Have a look here: http://esp8266.ru/esplorer/ best for nodemcu developing. If you code well nodemcu doesnt crash either.

            I really like nodemcu and to me the mistakes was obvious. Thats what made me think you did not make a serious try. If you like c better go for it. But telling people nodemcu is not done well is wrong especially if you say that after such a problem.

            Sorry that it ended up in such an argument. Hope you can forgive me... i guess we all fight for the things we love 🙂

          2. "...but for now – I think I’ll stick with C – I have my own pretty massive C code – which never crashes and runs 24-7."

            What setup are you using to program the ESP8266 in C? And thanks for a really interesting and useful site.

        1. Hi

          Check out the links below to dispel any rumours about me being new to Lua 🙂

          I think your comment about the RAM says it for me - and yes I do understand the about favourite languages - basic v C etc - there was a time when people came ot serious blows over which language to use... I'm really not new to Lua either - for a while I got really excited about it and of course it is used far beyond the confines of the ESP8266 - I just think an environment with 10x the amount of RAM would be better for that particular setup and enough room for proper error handling (though there is no shortage of FLASH - the code ultimately ends up in RAM).

          When I code in C incidentally - I don't to it via command line etc with reams of messages - in Eclipse IDE I see only errors and the compilation process takes seconds. I have it set up so it is a one-button press from compilation through blowing the chip. As you are an enthusiast I know I won't sway you - but when I originally had issues running out of RAM all the time with the NodeMCU software - I did a lot of searching for help and ideas on the web - shall we say I was not alone in griping about RAM - sadly there really is no way to expand it on the ESP8266.

          Regarding the segmentation error and rebooting - trust me - after reboot there was absolutely definitely something wrong - perhaps in the process something had been written to Flash.

          When I say "book" - I meant of course electronically... don't think I've bought a "book"... erm in a decade?

          And finally - Esplorer - yes lovely piece of work - it triggered me off to write my own editor - which has basic facilities for handling Lua in it such as auto "lua.writeline" prefix and suffix.. did that a while ago now...

          http://tech.scargill.net/simple-serial-terminal-lost-blog/

          Some much earlier items

          https://scargill.wordpress.com/2014/12/23/new-lua-update-is-open-source/

          https://scargill.wordpress.com/2014/12/11/esp8266-lua-timer-on-a-tight-budget/

          http://tech.scargill.net/nodemcu-broken/

          I actually wrote a whole boatload on the subject, but some time ago I swapped providers for the blog and as you can see from the "lost-blog" comment - my early posts went awol. Here are just a few - clearly old now. I've no idea why they did not end up on the new blog:

          https://scargill.wordpress.com/2015/01/24/lua-and-the-ds18b20ds18b20p-temperature-sensor/

          https://scargill.wordpress.com/2015/01/24/esp8266-lua-and-mqtt/

          https://scargill.wordpress.com/2015/01/15/return-to-the-land-of-esp-lua/

          https://scargill.wordpress.com/2014/12/10/more-esp8266-lua-depression/

          1. Nice we ended up in a placable way.

            "I just think an environment with 10x the amount of RAM would be better for that particular setup" I was already wondering why the hell Espressif made the FLASH build in instead of extending the RAM at thier ESP8285.

            Anyway the ESP32 seems to have the 10x more RAM (400 kB). Looking forward for a port of nodemcu to that chip. Since they seem code compatible that might not take too long.

            Might be another reason for you to check it out again 🙂

  2. Lny - absolutely - if the ESP32 ever actually does anything - laymans view - looking at Ebay/AliExpress- not seeing any boards coming out yet... and yes there are a number of environments we could look at again with more space. Just need boards based on the new chip to be just as cheap and I'm not sure that will happen.

  3. Thank you for recommending the Unofficial esp8266 development environment for my consideration. I look forward to trying it whenever I obtain an ESP unit.

Leave a Reply

Your email address will not be published. Required fields are marked *