- complètement remanié le script de boot init.lua. Maintenant il tient compte de la cause du reboot, et si c'est un poweron, démarre le script repair.lua au lieu de boot.lua

- renommé le script start_job.lua en boot.lua, script qui est lancé par init.lua juste après le boot
- créé le script repair.lua qui démarre le wifi en mode AP et lance le serveur telnet afin de pouvoir se connecter au moment du poweron si on a un problème de boucle au boot
- mon script dsleep.lua fonctionne bien maintenant
- amélioré le script wifi_info.lua, il indique le nom de l'AP ainsi que le password
- teste à chaque 'dofile' si le fichier existe avant de la lancer, ne plante donc plus si le script n'existe pas, grosse amélioration
This commit is contained in:
Christian Zufferey
2018-11-19 00:30:08 +01:00
parent b2b632a6cd
commit 8b78b4aa04
33 changed files with 337 additions and 93 deletions

View File

@@ -0,0 +1,11 @@
# Version minimaliste d'un init.lua
Test au moment du boot qui a été la cause de boot et alors prend une décision en conséquence.
Si le boot a été causé par un power on, alors attend 10 secondes avant de démarrer le fichier boot.lua. Cela permet d'avoir le temps de prendre une action en cas de plantage en boucle.
Dans les autres cas, démarre sans autre le fichier boot.lua pour autant qu'il existe.
zf181118.2231

View File

@@ -0,0 +1,11 @@
-- Scripts à charger après le boot pour démarrer son appli
print("\n boot.lua zf181118.2329 \n")
if file.exists("wifi_ap_stop.lua") then dofile("wifi_ap_stop.lua") end
if file.exists("dsleep.lua") then dofile("dsleep.lua") end
jobtimer1=tmr.create()
tmr.alarm(jobtimer1, 1*1000, tmr.ALARM_AUTO, function()
print("coucou...")
end)

View File

@@ -0,0 +1,11 @@
-- Teste le deep sleep !
-- après x seconde, s'endore pendant y secondes
-- ATTENTION: il faut connecter, avec une résistance de 1k, la pin 0 à la pin RESET !
print("\n dsleep.lua zf181119.0022 \n")
ztmr_SLEEP = tmr.create()
tmr.alarm(ztmr_SLEEP, 2*1000, tmr.ALARM_SINGLE, function ()
print("Je dors...")
node.dsleep(15*1000*1000)
end)

View File

@@ -0,0 +1,18 @@
--Script de bootstrap, test au moment du boot qui a été la cause de boot.
-- Si la cause est un power on ou une connexion depuis l'IDE, alors
-- le script repair.lua pendant 30 secondes avant de continuer
print("\n init.lua zf181119.0024 \n")
_, reset_reason = node.bootreason()
print("reset_reason:",reset_reason)
if reset_reason == 0 or reset_reason == 6 then
print("seconde chance...")
if file.exists("repair.lua") then dofile("repair.lua") end
initalarme=tmr.create()
tmr.alarm(initalarme, 30*1000, tmr.ALARM_SINGLE, function()
if file.exists("boot.lua") then dofile("boot.lua") end
end)
else
if file.exists("boot.lua") then dofile("boot.lua") end
end

View File

@@ -0,0 +1,11 @@
-- Scripts de seconde chance pour réparer une boucle dans le restart
print("\n repair.lua zf181119.006 \n")
if file.exists("wifi_ap_start.lua") then dofile("wifi_ap_start.lua") end
if file.exists("telnet_srv.lua") then dofile("telnet_srv.lua") end
jobtimer1=tmr.create()
tmr.alarm(jobtimer1, 5*1000, tmr.ALARM_AUTO, function()
print("repair...")
end)

View File

@@ -0,0 +1,12 @@
-- pour effacer TOUS les fichiers qui se trouve dans la flash du NodeMCU
print("\n rm_files.lua zf180907.1511 \n")
l=file.list() i=0
for k,v in pairs(l) do
i=i+v
file.remove(k)
end
print("-------------------------------")
print("\nC'est tout effaced :-) \n")

View File

@@ -0,0 +1,43 @@
-- a simple telnet server
print("\ntelnet_srv.lua zf181119.0005 \n")
-- restart server if needed
if telnet_srv ~= nil then
telnet_srv:close()
end
telnet_srv = net.createServer(net.TCP, 180)
telnet_srv:listen(23, function(socket)
local fifo = {}
local fifo_drained = true
local function sender(c)
if #fifo > 0 then
c:send(table.remove(fifo, 1))
else
fifo_drained = true
end
end
local function s_output(str)
table.insert(fifo, str)
if socket ~= nil and fifo_drained then
fifo_drained = false
sender(socket)
end
end
node.output(s_output, 0) -- re-direct output to function s_ouput.
socket:on("receive", function(c, l)
node.input(l) -- works like pcall(loadstring(l)) but support multiple separate line
end)
socket:on("disconnection", function(c)
node.output(nil) -- un-regist the redirect output function, output goes to serial
end)
socket:on("sent", sender)
print("Welcome to NodeMCU world.")
end)
print("Telnet server running...\nUsage: telnet -rN ip\n")

View File

@@ -0,0 +1,4 @@
--test si un fichier existe
--zf181118.2237
if file.exists("boot.lua") then dofile("boot.lua") end

View File

@@ -0,0 +1,14 @@
-- Démarre le WIFI en mode AP
print("\n wifi_ap_start.lua zf181118.2331 \n")
local zmodewifi=wifi.getmode()
if zmodewifi == wifi.NULLMODE then
print("WIFI mode AP only")
wifi.setmode(wifi.SOFTAP)
elseif zmodewifi == wifi.STATION then
print("WIFI mode AP+CLI")
wifi.setmode(wifi.STATIONAP)
end
wifi.ap.config({ ssid = "NodeMCU "..wifi.ap.getmac(), pwd = "12345678" })
if file.exists("wifi_info.lua") then dofile("wifi_info.lua") end

View File

@@ -0,0 +1,10 @@
-- Démarre le WIFI en mode AP
print("\n wifi_ap_stop.lua zf180824.2000 \n")
local zmodewifi=wifi.getmode()
if zmodewifi == wifi.SOFTAP then
wifi.setmode(wifi.NULLMODE)
elseif zmodewifi == wifi.STATIONAP then
wifi.setmode(wifi.STATION)
end
print("WIFI AP arrêté")

View File

@@ -0,0 +1,31 @@
-- Petit script pour afficher les infos actuel du WIFI
print("\n wifi_info.lua zf181119.0014 \n")
local zmodewifi=wifi.getmode()
--wifi.NULLMODE, wifi.STATION, wifi.SOFTAP, wifi.STATIONAP
if zmodewifi == wifi.NULLMODE then
print("WIFI OFF")
elseif zmodewifi == wifi.STATION then
print("WIFI mode CLI")
print("Connected IP:\n",wifi.sta.getip())
do
local sta_config=wifi.sta.getconfig(true)
print(string.format("Current client config:\n\tssid:\"%s\"\tpassword:\"%s\"\n\tbssid:\"%s\"\tbssid_set:%s", sta_config.ssid, sta_config.pwd, sta_config.bssid, (sta_config.bssid_set and "true" or "false")))
end
elseif zmodewifi == wifi.SOFTAP then
print("WIFI mode AP")
print("AP MAC:\n",wifi.ap.getmac())
print("AP IP:\n",wifi.ap.getip())
print("AP Connect:\n",wifi.ap.getconfig())
elseif zmodewifi == wifi.STATIONAP then
print("WIFI mode CLI+AP")
print("Connected IP:\n",wifi.sta.getip())
do
local sta_config=wifi.sta.getconfig(true)
print(string.format("Current client config:\n\tssid:\"%s\"\tpassword:\"%s\"\n\tbssid:\"%s\"\tbssid_set:%s", sta_config.ssid, sta_config.pwd, sta_config.bssid, (sta_config.bssid_set and "true" or "false")))
end
print("AP MAC: "..wifi.ap.getmac())
print("AP IP: "..wifi.ap.getip())
end