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..
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.