Skip to content

Instantly share code, notes, and snippets.

@thomo
Last active August 15, 2024 18:41
Show Gist options
  • Save thomo/b50470b05ce8cc5202001d22d6151f36 to your computer and use it in GitHub Desktop.
Save thomo/b50470b05ce8cc5202001d22d6151f36 to your computer and use it in GitHub Desktop.
NodeMCU init.lua with interrupt opportunity
-- Tested with
-- NodeMCU 3.0.0.0 built on nodemcu-build.com provided by frightanic.com
-- branch: release
-- commit: d4ae3c364bd8ae3ded8b77d35745b7f07879f5f9
-- release:
-- release DTS: 202105102018
-- SSL: false
-- build type: float
-- LFS: 0x0 bytes total capacity
-- modules: bit,file,gpio,net,node,spi,tmr,uart,ucg,wifi
-- build 2021-11-22 17:49 powered by
-- Lua 5.1.4 on SDK 3.0.1-dev(fce080e)
--
-- based on blog post in
-- https://bigdanzblog.wordpress.com/2015/04/24/esp8266-nodemcu-interrupting-init-lua-during-boot/
local FileToExecute = "user.lua"
local BootTimeout = 200
local AbortTimeout = 3000
-- initialize abort boolean flag
local abortFlag = false
function firmwareInfo()
if (1/3) > 0 then
print('FLOAT firmware version')
else
print('INTEGER firmware version')
end
end
function init()
print('Press ENTER to abort startup')
-- if <CR> is pressed, call abort
uart.on('data', '\r', abort, 0)
-- start timer to execute startup function in 3 seconds
tmr.create():alarm(AbortTimeout, 0, startup)
end
function abort()
-- user requested abort
abortFlag = true
end
function fileExists(name)
local l = file.list();
for k,v in pairs(l) do
if k == name then
return true
end
end
return false
end
function startup()
-- turns off uart scanning
uart.on('data')
if abortFlag == true then
print('#### startup aborted ####')
return
end
-- otherwise, start up
if fileExists(FileToExecute) then
print('Run startup script ...')
dofile(FileToExecute)
else
print('Start script '..FileToExecute..' not found.')
end
end
firmwareInfo()
tmr.create():alarm(BootTimeout, 0, init)
@thomo
Copy link
Author

thomo commented Nov 29, 2021

Updated to Lua 5.1.4 on SDK 3.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment