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.
To ensure your code executes the next time its powered on, with no interaction from you, either:
- drop it all into an init.lua file (this is bad, avoid it)
- 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:
dofile('oled_wifi.lua') -- this is the file to call that you've previously saved on your NodeMCU
print('Starting up...') -- a reminder that we're pausing
tmr.alarm(0,5000,0,startup) -- wait 5 seconds in case you need to interrupt start up
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.