- 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

View File

@@ -0,0 +1,21 @@
-- super mini bootstrap
print("\n init_minizf1.lua zf180909.1112 \n")
zswitch=3 --switch flash
gpio.mode(zswitch, gpio.INT, gpio.PULLUP)
initalarme=tmr.create()
function zbutton()
gpio.trig(zswitch, "none")
tmr.unregister(initalarme)
dofile("start_boot.lua")
dofile("start_job.lua")
end
gpio.trig(zswitch, "both", zbutton)
tmr.alarm(initalarme, 8000, tmr.ALARM_SINGLE, function()
print("\nStart\n")
dofile("start_boot.lua")
-- dofile("start_job.lua")
end)

View File

@@ -0,0 +1,90 @@
-- initz.lua
-- test si clic sur le bouton
-- clic <2s goto start.lua
-- clic >2s goto config wifi
print("\ninit.lua zf180719.1100 \n")
zBTNz = 3 -- GPIO0 button
--zRelay = 6 -- GPIO12 PWM0 relay (active high)
zLEDz = 0 -- SonOff: 7 GPIO13 PWM1, NodeMCU: 0, (active low)
zFlag_LEDz = 0
function blink_LEDz ()
if zFlag_LEDz==gpio.LOW then
zFlag_LEDz=gpio.HIGH
tmr.alarm(ztmr_LEDz, zTm_Off_LEDz, tmr.ALARM_SINGLE, blink_LEDz)
else
zFlag_LEDz=gpio.LOW
tmr.alarm(ztmr_LEDz, zTm_On_LEDz, tmr.ALARM_SINGLE, blink_LEDz)
end
gpio.write(zLEDz, zFlag_LEDz)
end
gpio.mode(zLEDz, gpio.OUTPUT)
ztmr_LEDz = tmr.create()
zTm_On_LEDz = 500
zTm_Off_LEDz = 500
blink_LEDz ()
function btn_testz()
t2z=tmr.now()
t3z=(t2z-t1z)/1000000
if gpio.read(zBTNz)==1 then
tmr.stop(0)
gpio.trig(zBTNz)
tmr.stop(ztmr_LEDz)
gpio.write(zLEDz,1)
print("start.lua")
ztmr_clear_bootstrapz = tmr.create()
-- tmr.alarm(ztmr_clear_bootstrapz,1000,tmr.ALARM_SINGLE, function() dofile("clear_bootstrap.lua") end)
dofile("start.lua")
end
if t3z>2 then
tmr.stop(0)
gpio.trig(zBTNz)
zTm_On_LEDz = 100
zTm_Off_LEDz = 100
blink_LEDz ()
wifi.sta.disconnect()
wifi.sta.clearconfig()
print("\nwifi config http://192.168.4.1\n")
tmr.alarm(0, 1000, tmr.ALARM_AUTO , get_ipz)
enduser_setup.start()
end
end
function btn_clicz()
t1z=tmr.now()
tmr.stop(ztmr_LEDz)
gpio.write(zLEDz,0)
tmr.alarm(0,100,tmr.ALARM_AUTO, btn_testz)
end
function get_ipz()
if wifi.sta.getip() == nil then
print("Connecting to AP...")
else
tmr.stop(0)
print("Connected! IP: ",wifi.sta.getip())
tmr.alarm(0,4000,tmr.ALARM_SINGLE, function() node.restart() end)
end
end
function dir()
l=file.list() i=0
for k,v in pairs(l) do
i=i+v print(k..string.rep(" ",19-string.len(k)).." : "..v.." bytes")
end
print('\nUsed: '..i..' bytes\nusage: dofile("file.lua")\n')
end
gpio.mode(zBTNz,gpio.INT)
gpio.trig(zBTNz, "down", btn_clicz)
wifi.sta.connect()
dofile("telnet_srv.lua")

View File

@@ -0,0 +1,90 @@
-- initz.lua
-- test si clic sur le bouton
-- clic <2s goto start.lua
-- clic >2s goto config wifi
print("\ninit.lua zf180719.1045 \n")
zBTN = 3 -- GPIO0 button
--zRelay = 6 -- GPIO12 PWM0 relay (active high)
zLED = 0 -- SonOff: 7 GPIO13 PWM1, NodeMCU: 0, (active low)
zFlag_LED = 0
function blink_LED ()
if zFlag_LED==gpio.LOW then
zFlag_LED=gpio.HIGH
tmr.alarm(ztmr_LED, zTm_Off_LED, tmr.ALARM_SINGLE, blink_LED)
else
zFlag_LED=gpio.LOW
tmr.alarm(ztmr_LED, zTm_On_LED, tmr.ALARM_SINGLE, blink_LED)
end
gpio.write(zLED, zFlag_LED)
end
gpio.mode(zLED, gpio.OUTPUT)
ztmr_LED = tmr.create()
zTm_On_LED = 500
zTm_Off_LED = 500
blink_LED ()
function btn_test()
t2=tmr.now()
t3=(t2-t1)/1000000
if gpio.read(zBTN)==1 then
tmr.stop(0)
gpio.trig(zBTN)
tmr.stop(ztmr_LED)
gpio.write(zLED,1)
print("start.lua")
ztmr_clear_bootstrap = tmr.create()
tmr.alarm(ztmr_clear_bootstrap,1000,tmr.ALARM_SINGLE, function() dofile("clear_bootstrap.lua") end)
dofile("start.lua")
end
if t3>2 then
tmr.stop(0)
gpio.trig(zBTN)
zTm_On_LED = 100
zTm_Off_LED = 100
blink_LED ()
wifi.sta.disconnect()
wifi.sta.clearconfig()
print("\nwifi config http://192.168.4.1\n")
tmr.alarm(0, 1000, tmr.ALARM_AUTO , get_ip)
enduser_setup.start()
end
end
function btn_clic()
t1=tmr.now()
tmr.stop(ztmr_LED)
gpio.write(zLED,0)
tmr.alarm(0,100,tmr.ALARM_AUTO, btn_test)
end
function get_ip()
if wifi.sta.getip() == nil then
print("Connecting to AP...")
else
tmr.stop(0)
print("Connected! IP: ",wifi.sta.getip())
tmr.alarm(0,4000,tmr.ALARM_SINGLE, function() node.restart() end)
end
end
function dir()
l=file.list() i=0
for k,v in pairs(l) do
i=i+v print(k..string.rep(" ",19-string.len(k)).." : "..v.." bytes")
end
print('\nUsed: '..i..' bytes\nusage: dofile("file.lua")\n')
end
gpio.mode(zBTN,gpio.INT)
gpio.trig(zBTN, "down", btn_clic)
wifi.sta.connect()
dofile("telnet_srv.lua")

View File

@@ -0,0 +1,23 @@
-- start.lua
-- programme de start
-- zf180717.1141
zLED=0
zTm_On_LED = 100 --> en ms
zTm_Off_LED = 900 --> en ms
zFlag_LED = 0
function blink_LED ()
if zFlag_LED==gpio.LOW then
zFlag_LED=gpio.HIGH
tmr.alarm(ztmr_LED, zTm_Off_LED, tmr.ALARM_SINGLE, blink_LED)
else
zFlag_LED=gpio.LOW
tmr.alarm(ztmr_LED, zTm_On_LED, tmr.ALARM_SINGLE, blink_LED)
end
gpio.write(zLED, zFlag_LED)
end
gpio.mode(zLED, gpio.OUTPUT)
ztmr_LED = tmr.create()
blink_LED()

View File

@@ -0,0 +1,7 @@
-- Scripts à charger après le boot pour démarrer son appli
print("\n start_job.lua zf180909.1117 \n")
jobtimer1=tmr.create()
jobtimer2=tmr.create()