2016-08-20 19 views
0

我想在NodeMCU內存上保存Lua程序。當NodeMCU在復位後啓動完成並準備好接收命令後,該腳本應自動開始執行,而無需將NodeMCU連接到任何外部計算機(通過ESPlorer等)。我仍然可以通過ESPlorer終止執行。一個可行的例子將非常感謝。如何在復位後NodeMCU出來後自動啓動Lua程序

回答

4

init.lua是你的朋友。請參閱https://nodemcu.readthedocs.io/en/latest/en/upload/#initlua的完整文檔。

-- load credentials, 'SSID' and 'PASSWORD' declared and initialize in there 
dofile("credentials.lua") 

function startup() 
    if file.open("init.lua") == nil then 
     print("init.lua deleted or renamed") 
    else 
     print("Running") 
     file.close("init.lua") 
     -- the actual application is stored in 'application.lua' 
     -- dofile("application.lua") 
    end 
end 

print("Connecting to WiFi access point...") 
wifi.setmode(wifi.STATION) 
wifi.sta.config(SSID, PASSWORD) 
-- wifi.sta.connect() not necessary because config() uses auto-connect=true by default 
tmr.alarm(1, 1000, 1, function() 
    if wifi.sta.getip() == nil then 
     print("Waiting for IP address...") 
    else 
     tmr.stop(1) 
     print("WiFi connection established, IP address: " .. wifi.sta.getip()) 
     print("You have 3 seconds to abort") 
     print("Waiting...") 
     tmr.alarm(0, 3000, 0, startup) 
    end 
end) 
+0

謝謝!正是我正在尋找的答案 - 不知何故,我錯過了readthedocs頁面中的這一部分。 – sm535

相關問題