Post your best Lua script examples here

User avatar
By pit
#17338 So while scripting along you screw up your lua code and now your esp keeps crashing .. rebooting .. init.lua .. crashing again .. rebooting .. init.lua .. crashing .. .

The only way to stop it is to re-flash the chip and reload your lua files. Maybe this time you name your init.lua init1.lua instead so you can carefully dofile('init1.lua') and if it now crashes then at least it wouldn't go into the endless cycle. But what a pain...

Here is another scenario: Your script has grown. init.lua starts your webserver and some other utilities. But now memory consumption is so high that your just uploaded improved fancy.lua scripts no longer compiles. node.compile('fancy.lua') spits out "out of memory".
So you delete init.lua .. reboot .. node.compile('fancy.lua') - which works now since none of your stuff is hogging memory .. upload init.lua .. reboot .. yay!!. Again, what a pain...

How can one stop the boot, in a most convenient and least-intrusive-to-the-development-work-flow way while the init.lua file is present? Post your suggestions please!!

The following snippet placed at the start of my init.lua works ok for me. I can now simply hold down the flash button to stop the boot. But maybe we can do better?
Code: Select allprint("\n\nHold Pin00 low to stop boot, you got one second.")
tmr.delay(1000000)
if gpio.read(3) == 0 then print("...boot stopped") return end
print("...booting")

-- do fancy stuff now...

Note that Pin00 is the flash button pin, so you likely already have a button there. If you hold the button down on crash-reboot or during node.restart() it will not go into flash mode. Only external reset or power cycling the chip while Pin00=LOW will do that. So to stop a crash .. boot .. crash .. boot cycle now you can simply hold your flash button down and shortly it will stop at a lua prompt giving you back control. If though you do need to stop boot after hard reset or power-cycle then simply push that flash button within one second of seeing the "Hold Pin00 low to stop boot" message and voila you get your lua prompt and nothing running or loaded.

Cheers,
pit
User avatar
By TerryE
#17556 By far the easiest method is to keep your init.lua small and use a on-shot timer to execute you mainline code after, say a 5 second delay. This gives you ample time to do a file.rename('init.lua','init_save.lua') through ESPlorer or whatever you are using if you get into a crash loop during developement. You can also wrap yur app in a pcall from init.lua, but this takes up memory.