Avoid init.lua hell

Hopefully, you’ll find the following handy tip a bit of a lifesaver if you’re trying out a nodeMCU!

Once you’ve got a bit of code running, you want to free your NodeMCU board from the shackles of its USB cable to your machine, and just give it a power source all of its own.  Of course you do!  This is where things can get interesting though.

Kranz on Console - GPN-2000-001406" by NASA - Great Images in NASA Description. Licensed under Public Domain via Wikimedia Commons - https://commons.wikimedia.org/wiki/File:Kranz_on_Console_-_GPN-2000-001406.jpg#/media/File:Kranz_on_Console_-_GPN-2000-001406.jpg

Kranz on Console – GPN-2000-001406″ by NASA – Great Images in NASA Description. Licensed under Public Domain via Wikimedia Commons – https://commons.wikimedia.org/wiki/File:Kranz_on_Console_-_GPN-2000-001406.jpg#/media/File:Kranz_on_Console_-_GPN-2000-001406.jpg

To ensure your code executes the next time its powered on, with no interaction from you, either:

  1. drop it all into an init.lua file (this is bad, avoid it)
  2. write a small piece of code for init.lua that refers to your file, with a delay (this is good)

Here’s one of the simplest examples:

So why have a pause before starting up?  Well unless you are 100% sure of your code (i.e. completely bug free), and don’t want to stop the code before it executes (e.g. to make changes), it’s imperative to have that few seconds of pause time.  The pause enables you to quickly rename init.lua (so you can break the cycle), or upload different code, or whatever.

Finally, if you have got into that mess of your NodeMCU in a permanent loop, its simply a case of flashing it again with the firmware.

Leave a Reply

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