Rangé toutes mes refactorisations des modules de base

This commit is contained in:
Christian Zufferey
2019-03-10 16:14:39 +01:00
parent ec1e9e0575
commit aa23150466
11 changed files with 378 additions and 45 deletions

View File

@@ -1,7 +1,10 @@
-- programme pour faire clignoter x fois une LED avec un rapport on/off
print("\n flash_led_xfois.lua zf181105.1111 \n")
zLED=0
function flash_led_xfois()
print("\n flash_led_xfois.lua zf190310.1533 \n")
--zLED=0 --NodeMCU
zLED=4 --EPS-M3
zTm_On_LED = 50 --> en ms
zTm_Off_LED = 100 --> en ms
nbfois = 0
@@ -11,7 +14,7 @@ ztmr_Flash_LED = tmr.create()
function blink_LED ()
if nbfois >= xfois then
print(nbfois)
-- print(nbfois)
nbfois = 0
else
if gpio.read(zLED)==gpio.HIGH then
@@ -25,8 +28,10 @@ function blink_LED ()
end
end
xfois =2
blink_LED ()
-- xfois =2
-- blink_LED ()
end

View File

@@ -0,0 +1,31 @@
-- Scripts à charger après le boot pour démarrer son appli
print("\n boot.lua zf190310.1537 \n")
function heartbeat()
f= "flash_led_xfois.lua" if file.exists(f) then dofile(f) end
flash_led_xfois()
boottimer1=tmr.create()
tmr.alarm(boottimer1, 1*1000, tmr.ALARM_AUTO, function()
xfois =2
blink_LED ()
end)
end
f= "led_rgb.lua" if file.exists(f) then dofile(f) end
f= "wifi_ap_start.lua" if file.exists(f) then dofile(f) end
f= "wifi_cli_conf.lua" if file.exists(f) then dofile(f) end
f= "wifi_cli_start.lua" if file.exists(f) then dofile(f) end
--f= "telnet_srv2.lua" if file.exists(f) then dofile(f) end
--f= "web_ide2.lua" if file.exists(f) then dofile(f) end
--f= "web_srv2.lua" if file.exists(f) then dofile(f) end
--f= "set_time.lua" if file.exists(f) then dofile(f) end
--f= "dsleep.lua" if file.exists(f) then dofile(f) end
f=nil
heartbeat=nil
--heartbeat()

View File

@@ -0,0 +1,37 @@
-- programme pour faire clignoter x fois une LED avec un rapport on/off
function flash_led_xfois()
print("\n flash_led_xfois.lua zf190310.1533 \n")
--zLED=0 --NodeMCU
zLED=4 --EPS-M3
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 ()
end

View File

@@ -0,0 +1,52 @@
--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 xx secondes avant de continuer
--Source: https://nodemcu.readthedocs.io/en/master/en/modules/node/#nodebootreason
print("\n init.lua zf190310.1549 \n")
zswitch=3 --switch flash
gpio.mode(zswitch, gpio.INT, gpio.PULLUP)
function hvbouton()
-- gpio.trig(zswitch, "none")
tmr.unregister(initalarme)
f= "boot.lua" if file.exists(f) then dofile(f) end
end
gpio.trig(zswitch, "both", hvbouton)
function second_chance()
print("seconde chance...")
f= "repair.lua" if file.exists(f) then dofile(f) end
initalarme=tmr.create()
tmr.alarm(initalarme, 4*1000, tmr.ALARM_SINGLE, function()
f= "boot.lua" if file.exists(f) then dofile(f) end
end)
end
_, reset_reason = node.bootreason()
print("reset_reason:",reset_reason)
if reset_reason == 0 then
print("power on")
second_chance()
elseif reset_reason == 4 then
print("node.restart")
gpio.trig(zswitch)
hvbouton=nil
second_chance=nil
zswitch=nil
reset_reason=nil
f= "boot.lua" if file.exists(f) then dofile(f) end
elseif reset_reason == 5 then
print("dsleep wake up")
f= "boot.lua" if file.exists(f) then dofile(f) end
elseif reset_reason == 6 then
print("external reset")
second_chance()
-- f= "boot.lua" if file.exists(f) then dofile(f) end
else
print("autre raison")
second_chance()
end

72
JSON/a_test1_connect.lua Normal file
View File

@@ -0,0 +1,72 @@
-- Juste pour tester la connection avec un socket
print("\n a_test1_connect.lua zf190310.1607 \n")
function zgetstream()
zgetstream_tmr=tmr.create()
tmr.alarm(zgetstream_tmr, 1000, tmr.ALARM_AUTO , function()
if wifi.sta.getip() == nil then
print("Connecting to AP...")
else
tmr.unregister(zgetstream_tmr)
zgetstream_tmr = nil
zcount = 0 zsum = 0
zhost = "192.168.0.34" zurl = "/json/meteo/meteo.lausanne.190302.1231.json"
srv = net.createConnection(net.TCP, 0)
srv:on("connection", function(sck, c)
print("on est connecté...")
sck:send("GET "..zurl.." HTTP/1.1\r\nHost: "..zhost.."\r\nConnection: close\r\nAccept: */*\r\n\r\n")
end)
srv:on("disconnection", function(a,b)
print("a: ",a,"b: ",b)
print("on est déconnecté...",node.heap())
end)
srv:on("reconnection", function(a,b)
print("a: ",a,"b: ",b)
print("on est reconnecté...",node.heap())
end)
srv:on("receive", function(sck, payload)
zcount = zcount + 1 zlen = string.len(payload) zsum = zsum + zlen
print("counter: ",zcount,"payload len: ",zlen,"sum: ",zsum)
-- print(payload)
end)
print("on connecte...",node.heap())
srv:connect(80, zhost)
end
end)
end
--[[
-- On affiche combien on a de RAM
print(node.heap())
-- On charge le module et regarde combien cela a pris de RAM
zh0=node.heap() print("zh0: ",zh0)
f= "a_test1_connect.lua" if file.exists(f) then dofile(f) end
zh1=node.heap() print("zh0-zh1: ",zh0-zh1)
-- On exécute le module
zh0=node.heap() print("zh0: ",zh0)
zgetstream()
-- On regarde combien cela a pris de RAM pour exécuter le module
zh1=node.heap() print("zh0-zh1: ",zh0-zh1)
-- On libère le module et on regarde combien on a libéré de RAM
zh0=node.heap() print("zh0: ",zh0)
srv = nil
zgetstream = nil
zh1=node.heap() print("zh0-zh1: ",zh0-zh1)
-- On affiche combien on a de RAM
print(node.heap())
]]

View File

@@ -0,0 +1,37 @@
-- Scripts juste pour allumer ou éteindre une LED sur un ruban RGB
-- tout sur la couleur: https://www.w3schools.com/colors/default.asp
-- roue des couleurs: https://iro.js.org/?ref=oldsite
print("\n led_rgb.lua zf190303.1436 \n")
nbleds=3
ws2812.init()
myLedStrip = ws2812.newBuffer(nbleds, 3)
function RGB_clear()
myLedStrip:fill(0, 0, 0) ws2812.write(myLedStrip)
end
function RGB_reform(R1, G1, B1) --conversion de RGB à BRG
rR1=B1 rG1=R1 rB1=G1
end
function zled_rgb(num_led, R1, G1, B1, zpower)
RGB_reform(R1, G1, B1)
myLedStrip:set(num_led, rR1*zpower, rG1*zpower, rB1*zpower)
ws2812.write(myLedStrip)
end
function zled_write()
ws2812.write(myLedStrip)
end
RGB_clear()
--[[
zled_rgb(1,255,0,0,1)
zled_rgb(2,0,255,0,1)
zled_rgb(2,0,255,0,0.05)
zled_rgb(3,0,0,255,1)
]]

72
Tests/a_test1_connect.lua Normal file
View File

@@ -0,0 +1,72 @@
-- Juste pour tester la connection avec un socket
print("\n a_test1_connect.lua zf190310.1607 \n")
function zgetstream()
zgetstream_tmr=tmr.create()
tmr.alarm(zgetstream_tmr, 1000, tmr.ALARM_AUTO , function()
if wifi.sta.getip() == nil then
print("Connecting to AP...")
else
tmr.unregister(zgetstream_tmr)
zgetstream_tmr = nil
zcount = 0 zsum = 0
zhost = "192.168.0.34" zurl = "/json/meteo/meteo.lausanne.190302.1231.json"
srv = net.createConnection(net.TCP, 0)
srv:on("connection", function(sck, c)
print("on est connecté...")
sck:send("GET "..zurl.." HTTP/1.1\r\nHost: "..zhost.."\r\nConnection: close\r\nAccept: */*\r\n\r\n")
end)
srv:on("disconnection", function(a,b)
print("a: ",a,"b: ",b)
print("on est déconnecté...",node.heap())
end)
srv:on("reconnection", function(a,b)
print("a: ",a,"b: ",b)
print("on est reconnecté...",node.heap())
end)
srv:on("receive", function(sck, payload)
zcount = zcount + 1 zlen = string.len(payload) zsum = zsum + zlen
print("counter: ",zcount,"payload len: ",zlen,"sum: ",zsum)
-- print(payload)
end)
print("on connecte...",node.heap())
srv:connect(80, zhost)
end
end)
end
--[[
-- On affiche combien on a de RAM
print(node.heap())
-- On charge le module et regarde combien cela a pris de RAM
zh0=node.heap() print("zh0: ",zh0)
f= "a_test1_connect.lua" if file.exists(f) then dofile(f) end
zh1=node.heap() print("zh0-zh1: ",zh0-zh1)
-- On exécute le module
zh0=node.heap() print("zh0: ",zh0)
zgetstream()
-- On regarde combien cela a pris de RAM pour exécuter le module
zh1=node.heap() print("zh0-zh1: ",zh0-zh1)
-- On libère le module et on regarde combien on a libéré de RAM
zh0=node.heap() print("zh0: ",zh0)
srv = nil
zgetstream = nil
zh1=node.heap() print("zh0-zh1: ",zh0-zh1)
-- On affiche combien on a de RAM
print(node.heap())
]]

View File

@@ -1,6 +1,7 @@
-- Démarre le WIFI en mode AP
print("\n wifi_ap_start.lua zf181119.2359 \n")
function wifi_ap_start()
print("\n wifi_ap_start.lua zf190310.1511 \n")
local zmodewifi=wifi.getmode()
if zmodewifi == wifi.NULLMODE then
@@ -11,4 +12,8 @@ elseif zmodewifi == wifi.STATION then
wifi.setmode(wifi.STATIONAP)
end
wifi.ap.config({ ssid = "NodeMCU "..wifi.ap.getmac(), pwd = "12345678" })
f= "wifi_info.lua" if file.exists(f) then dofile(f) end
--f= "wifi_info.lua" if file.exists(f) then dofile(f) end
end
wifi_ap_start()
wifi_ap_start=nil

View File

@@ -1,4 +1,20 @@
-- Petit script pour configurer le client WIFI du NodeMCU
print("\n wifi_cli_conf.lua zf180824.2000 \n")
wifi.sta.config{ssid="3g-s7", pwd="12234567", save=true}
function wifi_cli_conf()
print("\n wifi_cli_conf.lua zf190310.1527 \n")
--credentials par défaut
--cli_ssid="3g-s7"
cli_ssid="3G-zf"
cli_pwd="12234567"
--ses propre credentials
f= "credentials.lua" if file.exists(f) then dofile(f) end
wifi.sta.config{ssid=cli_ssid, pwd=cli_pwd, save=true}
end
wifi_cli_conf()
wifi_cli_conf=nil
cli_ssid=nil
cli_pwd=nil

View File

@@ -1,5 +1,7 @@
-- Petit script pour connecter le NodeMCU sur un AP Wifi avec l'accompte sauvé en EEPROM
print("\n wifi_cli_start.lua zf181119.2359 \n")
function wifi_cli_start()
print("\n wifi_cli_start.lua zf190310.1519 \n")
local zmodewifi=wifi.getmode()
if zmodewifi == wifi.NULLMODE then
@@ -11,4 +13,8 @@ elseif zmodewifi == wifi.SOFTAP then
end
wifi.sta.autoconnect(1)
wifi.sta.connect()
f= "wifi_get_ip.lua" if file.exists(f) then dofile(f) end
--f= "wifi_get_ip.lua" if file.exists(f) then dofile(f) end
end
wifi_cli_start()
wifi_cli_start=nil