Ajouté le projet Timetable à Alexandre et des scripts pour gérer le bouton

This commit is contained in:
Christian Zufferey
2019-02-28 12:28:26 +01:00
parent 0e6c684007
commit ecfe06aafa
18 changed files with 627 additions and 0 deletions

28
Btn/btn_flipflop.lua Normal file
View File

@@ -0,0 +1,28 @@
-- Petit scripts pour faire un flip flop avec le bouton et la led du nodemcu
-- Très intéressant la techno utilisée ici pour enlever les rebonds du micro switch
-- dans la variable b se trouve l'heure à laquelle l'interruption est arrivée, il suffit juste alors de lui mettre un petit délai de 300mS
print("\n btn_flipflop.lua zf190228.0935 \n")
gpio.write(0,1)
gpio.mode(0,gpio.OUTPUT)
gpio.mode(3,gpio.INT)
d=tmr.now()
function zled (a,b,c)
print("a: "..a..",b: "..b..",c: "..c)
if b-d > 300*1000 then
if gpio.read(0) == 0 then
gpio.write(0,1)
else
gpio.write(0,0)
end
print(gpio.read(0))
d=b
end
end
gpio.trig(3, "down", zled)
print("tutu")

34
Btn/btn_timeout.lua Normal file
View File

@@ -0,0 +1,34 @@
-- Petit scripts pour faire un timeout avec le bouton et la led du nodemcu
-- C'est seulement après 3 secondes de pression sur le bouton que l'on allume la led
print("\n btn_timeout.lua zf190228.0935 \n")
gpio.write(0,1)
gpio.mode(0,gpio.OUTPUT)
gpio.mode(3,gpio.INT)
d=tmr.now()
jTimer1 = tmr.create()
function jButton (a,b,c)
if b-d > 300*1000 then
if gpio.read(3) == 0 then
tmr.alarm(jTimer1, 3*1000, tmr.ALARM_SINGLE, jLed)
else
tmr.unregister(jTimer1)
gpio.write(0,1)
end
d=b
end
end
function jLed()
gpio.write(0,0)
end
gpio.trig(3, "both", jButton)
print("tutu")

1
Timetable/JLEB/README.md Normal file
View File

@@ -0,0 +1 @@
# Affiche l'horaire du train Cheseaux à Lausanne sur un ruban de LED's RGB

32
Timetable/JLEB/boot.lua Normal file
View File

@@ -0,0 +1,32 @@
-- Scripts à charger après le boot pour démarrer son appli
print("\n boot.lua zf190227.1836 \n")
function heartbeat()
f= "flash_led_xfois.lua" if file.exists(f) then dofile(f) end
boottimer1=tmr.create()
tmr.alarm(boottimer1, 1*1000, tmr.ALARM_AUTO, function()
xfois =2
blink_LED ()
end)
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= "b.lua" if file.exists(f) then dofile(f) end
f= "jled_rgb.lua" if file.exists(f) then dofile(f) end
f= "diff_time.lua" if file.exists(f) then dofile(f) end
--heartbeat()

View File

@@ -0,0 +1,24 @@
-- Scripts pour calculer une différence entre deux heures
print("\n diff_time.lua zf190227.1842 \n")
function time2sec(jtime)
jsecondes = 3600 * tonumber(string.sub(jtime,1,2))
jsecondes = jsecondes + 60 * tonumber(string.sub(jtime,4,5))
jsecondes = jsecondes + tonumber(string.sub(jtime,7,8))
return jsecondes
end
function diff_time(jtime1, jtime2)
diff_secondes = time2sec(jtime1) - time2sec(jtime2)
return diff_secondes
end
function zround(num, dec)
local mult = 10^(dec or 0)
return math.floor(num * mult + 0.5) / mult
end
--[[
print(diff_time("18:42:00", "18:40:00"))
]]

View File

@@ -0,0 +1,33 @@
-- programme pour faire clignoter x fois une LED avec un rapport on/off
print("\n flash_led_xfois.lua zf181208.1521 \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 ()

47
Timetable/JLEB/initz.lua Normal file
View File

@@ -0,0 +1,47 @@
--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 zf190217.1231 \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")
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

View File

@@ -0,0 +1,69 @@
-- Scripts juste pour flasher deux LED's 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 jflash_2rgb.lua jv190227.1628 \n")
dofile("jled_rgb.lua")
zLED1=1 zLED2=6
nbfois1 = 0 nbfois2 = 0
zLED_state1 = 0 zLED_state2 = 0
R1 = 255 G1 = 0 B1 = 0
R2 = 0 G2 = 255 B2 = 0
zTm_On_LED = 200 zTm_Off_LED = 400 zTm_Pause = 2000 --> en ms
ztmr_Flash_LED1 = tmr.create() ztmr_Flash_LED2 = tmr.create()
function flash_LED1 ()
if nbfois1 >= xfois1 then
print(nbfois1)
nbfois1 = 0
jled_rgb(zLED1,R1,G1,B1,0)
tmr.alarm(ztmr_Flash_LED1, zTm_Pause, tmr.ALARM_SINGLE, flash_LED1)
else
if zLED_state1 == 0 then
jled_rgb(zLED1,R1,G1,B1,1)
zLED_state1 = 1
tmr.alarm(ztmr_Flash_LED1, zTm_On_LED, tmr.ALARM_SINGLE, flash_LED1)
nbfois1 = nbfois1 + 1
else
jled_rgb(zLED1,R1,G1,B1,0)
zLED_state1 = 0
tmr.alarm(ztmr_Flash_LED1, zTm_Off_LED, tmr.ALARM_SINGLE, flash_LED1)
end
end
end
function flash_LED2 ()
if nbfois2 >= xfois2 then
print(nbfois2)
nbfois2 = 0
jled_rgb(zLED2,R2,G2,B2,0)
tmr.alarm(ztmr_Flash_LED2, zTm_Pause, tmr.ALARM_SINGLE, flash_LED2)
else
if zLED_state2 == 0 then
jled_rgb(zLED2,R2,G2,B2,1)
zLED_state2 = 1
tmr.alarm(ztmr_Flash_LED2, zTm_On_LED, tmr.ALARM_SINGLE, flash_LED2)
nbfois2 = nbfois2 + 1
else
jled_rgb(zLED2,R2,G2,B2,0)
zLED_state2 = 0
tmr.alarm(ztmr_Flash_LED2, zTm_Off_LED, tmr.ALARM_SINGLE, flash_LED2)
end
end
end
xfois1 =5
flash_LED1 ()
xfois2 =2
flash_LED2 ()
--[[
]]

View File

@@ -0,0 +1,38 @@
-- 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 jled_rgb.lua jv190227.1626 \n")
nbleds=6
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) --RGB to GRB
rR1=G1 rG1=R1 rB1=B1
end
function jled_rgb(num_led, R1, G1, B1, jpower)
RGB_reform(R1, G1, B1)
myLedStrip:set(num_led, rR1*jpower, rG1*jpower, rB1*jpower)
ws2812.write(myLedStrip)
end
function jled_write()
ws2812.write(myLedStrip)
end
RGB_clear()
--[[
jled_rgb(1,255,0,0,1)
jled_rgb(4,0,0,255,1)
jled_rgb(4,0,0,255,0.05)
jled_rgb(6,0,255,0,1)
]]

View File

@@ -0,0 +1,12 @@
-- Scripts pour faire un soft reset
print("\n restart.lua zf181209.1753 \n")
restarttimer1=tmr.create()
tmr.alarm(restarttimer1, 2*1000, tmr.ALARM_SINGLE, function()
node.restart()
end)
print("hello zuzu")

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,29 @@
-- Scripts pour régler l'horloge quand on est connecté en WIFI
-- Permet aussi de 'compresser' le unix time afin de prendre moins de place dans les strings
print("\n set_time.lua zf190217.1426 \n")
--source: https://www.freeformatter.com/epoch-timestamp-to-date-converter.html
ztime2019 = 1546300800 -- Unix time pour le 1.1.2019
function set_time()
sntp.sync(nil, nil, nil, 1)
end
function ztime_compress(ztime_long)
return ztime_long - ztime2019
end
function ztime_uncompress(ztime_short)
return ztime_short + ztime2019
end
function ztime_format(ztime)
tm = rtctime.epoch2cal(ztime + 3600)
return(string.format("%04d/%02d/%02d %02d:%02d:%02d", tm["year"], tm["mon"], tm["day"], tm["hour"], tm["min"], tm["sec"]))
end
set_time()
print(ztime_format(rtctime.get()))

View File

@@ -0,0 +1,37 @@
-- Petit script pour envoyer quelque chose sur un serveur WEB
print("\n web_cli.lua zf190227.1838 \n")
function disp_send()
-- http.get("http://192.168.4.1/?line1="..zlength.."m", nil, function(code, data)
print("web_cli: ")
http.get("http://transport.opendata.ch/v1/connections?from=Cheseaux&to=Lausanne-Flon&fields[]=connections/from/departure", nil, function(code, data)
if (code < 0) then
print("HTTP request failed")
else
print(code, data)
jp= 50 h1=string.sub (data,jp,jp+7)
jp= 100 h2=string.sub (data,jp,jp+7)
jp= 150 h3=string.sub (data,jp,jp+7)
jp= 200 h4=string.sub (data,jp,jp+7)
print(h1,h2,h3,h4)
tm = rtctime.epoch2cal(rtctime.get() + 3600)
h0 = string.format("%02d:%02d:%02d", tm["hour"], tm["min"], tm["sec"])
d1=zround((diff_time(h2, h0)/180),0)
d2=zround((diff_time(h3, h0)/180),0)
print(d1,d2)
xfois1 = d2 xfois2 =d1
end
end)
end
disp_send()
--[[
tm = rtctime.epoch2cal(ztime + 3600)
h0 = string.format("%02d:%02d:%02d", tm["hour"], tm["min"], tm["sec"])
print(h0)
]]

159
Timetable/JLEB/web_ide2.lua Normal file
View File

@@ -0,0 +1,159 @@
-- Petit WEB IDE tout simple autonome
-- ATTENTION: tourne sur le port 88 !
print("\n _web_ide2.lua zf190227.1739 \n")
--[[
XChip's NodeMCU IDE
Create, Edit and run NodeMCU files using your webbrowser.
Examples:
http://<mcu_ip>/ will list all the files in the MCU
http://<mcu_ip>/newfile.lua displays the file on your browser
http://<mcu_ip>/newfile.lua?edit allows to creates or edits the specified script in your browser
http://<mcu_ip>/newfile.lua?run it will run the specified script and will show the returned value
]]--
srv=net.createServer(net.TCP)
srv:listen(88,function(conn)
local rnrn=0
local Status = 0
local DataToGet = 0
local method=""
local url=""
local vars=""
conn:on("receive",function(conn,payload)
if Status==0 then
_, _, method, url, vars = string.find(payload, "([A-Z]+) /([^?]*)%??(.*) HTTP")
print(method, url, vars)
end
if method=="POST" then
if Status==0 then
--print("status", Status)
_,_,DataToGet, payload = string.find(payload, "Content%-Length: (%d+)(.+)")
if DataToGet~=nil then
DataToGet = tonumber(DataToGet)
--print(DataToGet)
rnrn=1
Status = 1
else
print("bad length")
end
end
-- find /r/n/r/n
if Status==1 then
--print("status", Status)
local payloadlen = string.len(payload)
local mark = "\r\n\r\n"
local i
for i=1, payloadlen do
if string.byte(mark, rnrn) == string.byte(payload, i) then
rnrn=rnrn+1
if rnrn==5 then
payload = string.sub(payload, i+1,payloadlen)
file.open(url, "w")
file.close()
Status=2
break
end
else
rnrn=1
end
end
if Status==1 then
return
end
end
if Status==2 then
--print("status", Status)
if payload~=nil then
DataToGet=DataToGet-string.len(payload)
--print("DataToGet:", DataToGet, "payload len:", string.len(payload))
file.open(url, "a+")
file.write(payload)
file.close()
else
conn:send("HTTP/1.1 200 OK\r\n\r\nERROR")
Status=0
end
if DataToGet==0 then
conn:send("HTTP/1.1 200 OK\r\n\r\nOK")
Status=0
end
end
return
end
DataToGet = -1
if url == "favicon.ico" then
conn:send("HTTP/1.1 404 file not found")
return
end
conn:send("HTTP/1.1 200 OK\r\n\r\n")
-- it wants a file in particular
if url~="" and vars=="" then
DataToGet = 0
return
end
conn:send("<html><body><h1>NodeMCU IDE</h1>".."Connected IP: "..wifi.sta.getip().."<br><br>")
if vars=="edit" then
conn:send("<script>function tag(c){document.getElementsByTagName('w')[0].innerHTML=c};\n")
conn:send("var x=new XMLHttpRequest()\nx.onreadystatechange=function(){if(x.readyState==4) document.getElementsByName('t')[0].value = x.responseText; };\nx.open('GET',location.pathname,true)\nx.send()</script>")
conn:send("<h2><a href='/'>Back to file list</a>\n")
conn:send("<br><br><button onclick=\"tag('Saving');x.open('POST',location.pathname,true);\nx.onreadystatechange=function(){if(x.readyState==4) tag(x.responseText);};\nx.send(new Blob([document.getElementsByName('t')[0].value],{type:'text/plain'}));\">Save</button><a href='?run'>run</a><w></w>")
conn:send("</h2><br><textarea name=t cols=110 rows=50></textarea></br>")
end
if vars=="run" then
conn:send("<verbatim>")
local st, result=pcall(dofile, url)
conn:send(tostring(result))
conn:send("</verbatim>")
end
if url=="" then
local l = file.list();
for k,v in pairs(l) do
conn:send("<a href='"..k.."?edit'>"..k.."</a>, size:"..v.."<br>")
end
end
conn:send("</body></html>")
end)
conn:on("sent",function(conn)
if DataToGet>=0 and method=="GET" then
if file.open(url, "r") then
file.seek("set", DataToGet)
local line=file.read(512)
file.close()
if line then
conn:send(line)
DataToGet = DataToGet + 512
if (string.len(line)==512) then
return
end
end
end
end
conn:close()
end)
end)
print("listening, free:", node.heap())

View File

@@ -0,0 +1,14 @@
-- Démarre le WIFI en mode AP
print("\n wifi_ap_start.lua zf190227.1727 \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 jLEB "..wifi.ap.getmac(), pwd = "12345678" })
f= "wifi_info.lua" if file.exists(f) then dofile(f) end

View File

@@ -0,0 +1,13 @@
-- Petit script pour configurer le client WIFI du NodeMCU
print("\n wifi_cli_conf.lua zf190227.1723 \n")
--credentials par défaut
--cli_ssid="3g-s7"
cli_ssid="3g-s7"
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}

View File

@@ -0,0 +1,14 @@
-- 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")
local zmodewifi=wifi.getmode()
if zmodewifi == wifi.NULLMODE then
print("WIFI mode CLI only")
wifi.setmode(wifi.STATION)
elseif zmodewifi == wifi.SOFTAP then
print("WIFI mode AP+CLI")
wifi.setmode(wifi.STATIONAP)
end
wifi.sta.autoconnect(1)
wifi.sta.connect()
f= "wifi_get_ip.lua" if file.exists(f) then dofile(f) end

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