- 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

@@ -1,75 +0,0 @@
-- initz.lua
-- test si clic sur le bouton
-- clic <2s goto start.lua
-- clic >2s goto config wifi
print("\ninit.lua zf180720.1550 \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(ztmr_btnz)
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")
else
if t3z>3 then
tmr.stop(ztmr_btnz)
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")
ztmr_get_ipz = tmr.create() tmr.alarm(ztmr_get_ipz, 4000, tmr.ALARM_AUTO , get_ipz)
enduser_setup.start()
end
end
end
function btn_clicz()
gpio.trig(zBTNz, "none") gpio.mode(zBTNz,gpio.INPUT)
t1z=tmr.now()
tmr.stop(ztmr_LEDz) gpio.write(zLEDz,0)
ztmr_btnz = tmr.create() tmr.alarm(ztmr_btnz, 500,tmr.ALARM_AUTO, btn_testz)
end
function get_ipz()
if wifi.sta.getip() == nil then
print("Connecting to AP...")
else
tmr.stop(ztmr_get_ipz)
print("Connected! IP: ",wifi.sta.getip())
tmr.alarm(ztmr_get_ipz,1000,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

@@ -1,9 +0,0 @@
-- Scripts à charger au moment du boot afin de pouvoir travailler avec le le réseau
print("\n start_boot.lua zf180909.1116 \n")
--dofile("wifi_cnf_start.lua")
--dofile("wifi_ap_stop.lua")
--dofile("wifi_cli_start.lua")
--dofile("web_srv.lua")
--dofile("telnet_srv.lua")

View File

@@ -0,0 +1,23 @@
-- programme pour faire clignoter une LED avec un rapport on/off
print("\n blink_led1.lua zf181116.0014 \n")
zLED=0
zTm_On_LED = 50 --> en ms
zTm_Off_LED = 500 --> 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

@@ -1,9 +1,10 @@
-- Teste le deep sleep !
-- s'endore pendant 3 secondes après 8 secondes
-- à mettre à la place du init.lua
-- à mettre à la place du init.lua !
-- ATTENTION: il faut connecter la pin 0 à la pin RESET !
print("\n dsleep.lua zf181113.2040 \n")
print("\n dsleep.lua zf181118.21227 \n")
_, reset_reason = node.bootreason()
print("reset_reason: ",reset_reason)
@@ -12,7 +13,7 @@ if reset_reason == 5 then print("Coucou, je suis réveillé...") end
if reset_reason == 6 then print("Coucou, hard reset...") end
ztmr_SLEEP = tmr.create()
tmr.alarm(ztmr_SLEEP, 8000, tmr.ALARM_SINGLE, function ()
tmr.alarm(ztmr_SLEEP, 10*1000, tmr.ALARM_SINGLE, function ()
print("Je dors...")
node.dsleep(3000000)
node.dsleep(10*1000*1000)
end)

View File

@@ -0,0 +1,32 @@
-- programme pour faire clignoter x fois une LED avec un rapport on/off
print("\n flash_led_xfois.lua zf181105.1111 \n")
zLED=0
zTm_On_LED = 50 --> en ms
zTm_Off_LED = 100 --> en ms
nbfois = 0
gpio.write(zLED, gpio.HIGH)
gpio.mode(zLED, gpio.OUTPUT)
ztmr_Flash_LED = tmr.create()
function blink_LED ()
if nbfois >= xfois then
print(nbfois)
nbfois = 0
else
if gpio.read(zLED)==gpio.HIGH then
gpio.write(zLED, gpio.LOW)
tmr.alarm(ztmr_Flash_LED, zTm_Off_LED, tmr.ALARM_SINGLE, blink_LED)
else
gpio.write(zLED, gpio.HIGH)
nbfois = nbfois+1
tmr.alarm(ztmr_Flash_LED, zTm_On_LED, tmr.ALARM_SINGLE, blink_LED)
end
end
end
xfois =2
blink_LED ()

23
DeepSleep/test1/initz.lua Normal file
View File

@@ -0,0 +1,23 @@
--Script de bootstrap, en appuyant sur le bouton ça démarre start_boot,
-- autrement en attendant 8 secondes cela démarre start_boot
print("\n init.lua zf181017.1026\n")
zswitch=3 --switch flash
gpio.mode(zswitch, gpio.INT, gpio.PULLUP)
initalarme=tmr.create()
function hvbouton()
gpio.trig(zswitch, "none")
tmr.unregister(initalarme)
dofile("start_boot.lua")
-- dofile("start_job.lua")
end
gpio.trig(zswitch, "both", hvbouton)
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,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,7 @@
-- Scripts à charger au moment du boot
print("\n start_boot.lua zf181118.1043 \n")
dofile("dsleep.lua")

View File

@@ -0,0 +1,9 @@
-- Déconnecte le WIFI
print("\n wifi_off.lua zf180822.0959 \n")
wifi.setmode(wifi.NULLMODE)
--[[
print(wifi.NULLMODE, wifi.STATION, wifi.SOFTAP, wifi.STATIONAP)
print(wifi.getmode())
]]

43
Net_utils/telnet_srv.lua Normal file
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

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

View File

@@ -1,5 +1,5 @@
-- Petit script pour afficher les infos actuel du WIFI
print("\n wifi_info.lua zf180824.2000 \n")
print("\n wifi_info.lua zf181119.0014 \n")
local zmodewifi=wifi.getmode()
@@ -16,8 +16,9 @@ elseif zmodewifi == wifi.STATION then
end
elseif zmodewifi == wifi.SOFTAP then
print("WIFI mode AP")
print("AP MAC:\n\t"..wifi.ap.getmac())
print("AP IP:\n\t"..wifi.ap.getip())
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())