Lua Memory Woes

Don’t you just hate it when people leave old blogs lying around.. I can’t change the name of this or you won’t find it – but suffice it to say that as of updates to Lua of 09/12/2014 and some pretty lateral thinking on my behalf, I no longer have a depressing MESS but I now have a working APP – so the title of this is just all wrong – not woes at all but more like cries of joy.

I’m working out a full article right now but if anyone is in a routine-writing mode… something I’ve come to realise is very important in LUA is the initial setup.

Lua calls the INIT.LUA  program on power up (mandatory if you’ve put one in)– so all you really want to do there is maybe say hello and then move on to your GLOBALS.LUA file.

From here on you want to do two things (Scargill’s rules, you’ll not see this anywhere else)..

Your globals.lua file should contain nothing more than global variables and their initialisation – keep this short as possible as this is the one you’ll want to update – see later. Now move to your SETUP.LUA file again using a simple one-off timer – I’m using the same for everything even though we have 7 of them.

The setup.lua file will do init before passing onto whatever else you want to do.

So what’s this globals file?  Well, imagine you want to turn a light on and off with your little board… and lets say you turn it on – and you get a power failure… how can Lua know what state the light should be in – well right now it can’t.  You don’t have EPROM memory like an Arduino- so you want to update the globals file with any settings…  I have no idea how I’m going to do this at the time of writing but here’s what I’m thinking… a call that says..

update_global(“myvar”,”myvalue”)

I think I might be able to get away with firing integers at the last half of that.. but the basic plot would be that the routine would go into the globals file – look for myvar, if it’s not there add it, if it is there, replace it’s value and close the file. Dead easy on a PC with loads of memory but this needs to be as tight as possible.  So next time the unit powers up it knows what state things need to be in!!

See the point of the globals file having nothing but global vars in it – and even then only the ones you are likely to want to change. Keeps the file SMALL and so hopefully relatively easy to manipulate.

Time to learn the file commands – keep eyes peeled as I am DEAD CHUFFED I have a working mechanism on the way to make programs that work reliably using Lua on the ESP8266 – I was beginning to seriously doubt if this would ever happen.

Pete Scargill

Advertisements

5 thoughts on “Lua Memory Woes

  1. Hi,
    Haven’t got my esp-03, so I can’t test your code.
    I have a question, if I’ll code the same program but with other GCC compailer and load it to the esp, will it consume the same a mount of RAM?
    Does Lua has more overload on the size of the program?

    • You’d have to ask ZERODAY that question – he’s the developer of the LUA code… I’ve asked questions about RAM and FLASH – you can interrogate how much RAM you have left but I’m fairly sure you can’t interrogate how much FLASH space there is for files and that’s really important too – the worst thing about RAM usage is the only thing I’m sure of is that by the time you get to around 5K of RAM left – it’s in trouble.

    • It certainly does but it’s not solving the issues I’m having – my method is sound – it’s some kind of memory issue I think – I’m hoping others can try to narrow this down as the issue will apply to any sizeable project. The board crashes badly when memory gets low – and there doesn’t seem to be a way to tell how much FLASH is left.

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