diff --git a/SonOff/SonOff_2/00test_cmd.lua b/SonOff/SonOff_2/00test_cmd.lua new file mode 100644 index 0000000..41972d0 --- /dev/null +++ b/SonOff/SonOff_2/00test_cmd.lua @@ -0,0 +1,14 @@ +print("getpeer",srv_rt:getpeer()) +print("srv_rt",srv_rt) +srv_rt=nil +rt_connect=nil +telnet_listener=nil +package=nil +d=nil +require=nil +pairs=nil +ipairs=nil +telnet_listener=nil + +rt_retry=3 +rt_connect() diff --git a/SonOff/SonOff_2/0_btn_flipflop.lua b/SonOff/SonOff_2/0_btn_flipflop.lua new file mode 100644 index 0000000..b455aa9 --- /dev/null +++ b/SonOff/SonOff_2/0_btn_flipflop.lua @@ -0,0 +1,27 @@ +-- 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 0_btn_flipflop.lua zf200216.1457 \n") + +gpio.write(zLED,1) gpio.mode(zLED,gpio.OUTPUT) +gpio.mode(zBTN,gpio.INT) + +d=tmr.now() + +function zled (a,b,c) + --print("a: "..a..",b: "..b..",c: "..c) + if b-d > 300*1000 then + if verbose then + gpio.write(zLED, gpio.LOW) tmr.delay(10000) gpio.write(zLED, gpio.HIGH) + verbose=false + else + gpio.write(zLED, gpio.LOW) tmr.delay(200000) gpio.write(zLED, gpio.HIGH) + verbose=true + end + d=b + print("verbose:",verbose) + end +end + +gpio.trig(zBTN, "down", zled) diff --git a/SonOff/SonOff_2/0_cron.lua b/SonOff/SonOff_2/0_cron.lua new file mode 100644 index 0000000..19925ce --- /dev/null +++ b/SonOff/SonOff_2/0_cron.lua @@ -0,0 +1,24 @@ +-- Petit script pour faire office de crontab pour les mesures +print("\n 0_cron.lua zf200229.1557 \n") + +cron1=tmr.create() +cron1:alarm(60*1000, tmr.ALARM_AUTO, function() + if verbose then print("cron1........................") end + if verbose then gpio.write(zLED, gpio.LOW) tmr.delay(10000) gpio.write(zLED, gpio.HIGH) end + + f = "0_zdyndns.lua" if file.exists(f) then dofile(f) end + +--[[ +f = "0_htu21d.lua" if file.exists(f) then dofile(f) end + + zurl = thingspeak_url.."field1="..tostring(ztemp1).."&field2="..tostring(zhum1) + + f = "0_send_data.lua" if file.exists(f) then dofile(f) end + + ztemp1=nil zhum1=nil +]] + + if verbose then print(node.heap()) end + collectgarbage() + if verbose then print(node.heap()) end +end) diff --git a/SonOff/SonOff_2/0_send_data.lua b/SonOff/SonOff_2/0_send_data.lua new file mode 100644 index 0000000..bb5e2fc --- /dev/null +++ b/SonOff/SonOff_2/0_send_data.lua @@ -0,0 +1,24 @@ +-- Petit script pour envoyer les valeurs de température sur un serveur WEB via un HTTP GET + +function send_data() + if verbose then print("\n 0_send_data.lua zf200119.1518 \n") end + + if verbose then print("send_data_web: ") end + if verbose then print(zurl) end + + http.get(zurl, nil, function(code, data) + if (code < 0) then + if verbose then print("HTTP request failed") end + if verbose then print("zuzu", code, data) end + else + if verbose then print(code, data) end + end + zurl=nil + end) + zurl=nil send_data=nil + if verbose then print(node.heap()) end + collectgarbage() + if verbose then print(node.heap()) end +end + +send_data() diff --git a/SonOff/SonOff_2/0_tst3_socat.lua b/SonOff/SonOff_2/0_tst3_socat.lua new file mode 100644 index 0000000..ca724aa --- /dev/null +++ b/SonOff/SonOff_2/0_tst3_socat.lua @@ -0,0 +1,182 @@ +--[[ +tests connection reverse telnet +commande à faire tourner sur le serveur + +1ere console +pour une liaison directe: +socat TCP-LISTEN:23047,fork,reuseaddr STDIO +pour une console sur un port: +socat TCP-LISTEN:23047,reuseaddr,fork TCP-LISTEN:23000,reuseaddr,bind=127.0.0.1 + +2e console +telnet -r localhost 23000 +]] + +print("\n 0_tst3_socat.lua zf200229.2243 \n") + +local node, table, tmr, uwrite, tostring = + node, table, tmr, uart.write, tostring + +local function telnet_listener(socket) + local insert, remove, concat, heap, gc = + table.insert, table.remove, table.concat, node.heap, collectgarbage + local fifo1, fifo1l, fifo2, fifo2l = {}, 0, {}, 0 + local s -- s is a copy of the TCP socket if and only if sending is in progress + local wdclr, cnt = tmr.wdclr, 0 + local function debug(fmt, ...) + if (...) then fmt = fmt:format(...) end + uwrite(0, "\r\nDBG: ",fmt,"\r\n" ) + cnt = cnt + 1 + if cnt % 10 then wdclr() end + end + + local function flushGarbage() + if heap() < 13440 then gc() end + end + + local function sendLine() + if not s then return end + + if fifo2l + fifo1l == 0 then -- both FIFOs empty, so clear down s + s = nil + return + end + flushGarbage() + if #fifo2 < 4 then -- Flush FIFO1 into FIFO2 + insert(fifo2,concat(fifo1)) + fifo2l, fifo1, fifo1l = fifo2l + fifo1l, {}, 0 + end + local rec = remove(fifo2,1) .. (remove(fifo2,1) or '') .. + (remove(fifo2,1) or '') .. (remove(fifo2,1) or '') + fifo2l = fifo2l - #rec + flushGarbage() + s:send(rec) + end + + local F1_SIZE = 256 + + local function queueLine(str) + while #str > 0 do -- this is because str might be longer than the packet size! + local k, l = F1_SIZE - fifo1l, #str + local chunk + if #fifo1 >= 32 or (k < l and k < 16) then + insert(fifo2, concat(fifo1)) + fifo2l, fifo1, fifo1l, k = fifo2l + fifo1l, {}, 0, F1_SIZE + end + if l > k+16 then -- also tolerate a size overrun of 16 bytes to avoid a split + chunk, str = str:sub(1,k), str:sub(k+1) + else + chunk, str = str, '' + end + insert(fifo1, chunk) + fifo1l = fifo1l + #chunk + end + if not s and socket then + s = socket + sendLine() + else + flushGarbage() + end + end + + local function receiveLine(s, line) + node.input(line) + end + + local function disconnect(s) + --fifo1, fifo1l, fifo2, fifo2l, s = {}, 0, {}, 0, nil + fifo1, fifo1l, fifo2, fifo2l, s = nil, nil, nil, nil, nil + --insert, remove, concat, heap, gc = nil, nil, nil, nil, nil + --wdclr, cnt = nil, nil + node.output(nil) + print("disconnected...") +-- print("rt_retry:",rt_retry) +-- rt_retry=rt_retry-1 +-- print("rt_retry:",rt_retry) +-- if rt_retry>=0 then + print("on ressaie en vitesse une fois ;-)") + rt_connect() +-- end +end + + --zzz + local function zconnection(s) + print("Welcome on ne devrait jamais passer par là to NodeMCU world.") + end + + socket:on("connection", zconnection) + socket:on("receive", receiveLine) + socket:on("disconnection", disconnect) + socket:on("sent", sendLine) + node.output(queueLine, 0) +end + +--net.createServer(net.TCP, 180):listen(23, telnet_listener) +print("Revers telnet server running...\n") + + + + + +function rt_connect() + if verbose then + gpio.write(zLED, gpio.HIGH) + print("trying connect to "..console_host..":"..console_port) + print(node.heap()) collectgarbage() print(node.heap()) + end + srv_rt = nil srv_rt = net.createConnection(net.TCP, 0) + srv_rt:on("connection", function(sck) + if verbose then + print("connected on "..console_host..":"..console_port) + print(node.heap()) collectgarbage() print(node.heap()) + end + telnet_listener(sck) + print("Welcome to NodeMCU world.") + end) + srv_rt:connect(console_port,console_host) +end + +tmr_socat1=tmr.create() +tmr_socat1:alarm(5*1000, tmr.ALARM_AUTO , function() + rt_retry=1 + if verbose then gpio.write(zLED, gpio.LOW) tmr.delay(10000) gpio.write(zLED, gpio.HIGH) end + + if srv_rt~=nil then + if console_port == srv_rt:getpeer() then + --cela tourne... + if verbose then gpio.write(zLED, gpio.LOW) end + else + --on relance... + rt_connect() + end + else + --on relance... + rt_connect() + end +end) + +rt_retry=3 +rt_connect() + + + + + + +--[[ +tmr_socat1:unregister() +for k,v in pairs(_G) do print(k,v) end + +print(srv_rt:getpeer()) + +srv_rt:connect(console_port,console_host) + +gpio.write(zLED, gpio.LOW) tmr.delay(10000) gpio.write(zLED, gpio.HIGH) +if console_port == srv_rt:getpeer() then + gpio.write(zLED, gpio.LOW) +else + gpio.write(zLED, gpio.HIGH) +end + + +]] diff --git a/SonOff/SonOff_2/0_zdyndns.lua b/SonOff/SonOff_2/0_zdyndns.lua new file mode 100644 index 0000000..a6357c1 --- /dev/null +++ b/SonOff/SonOff_2/0_zdyndns.lua @@ -0,0 +1,33 @@ +-- Petit script pour s'inregistrer sur zdyndns + +function send_zdyndns() + if verbose then print("\n 0_zdyndns.lua zf200229.1553 \n") end + + zip = wifi.sta.getip() + zdyndns_str = "s "..node_id..","..console_host..":"..tostring(console_port).." "..zip.."\n" + if verbose then print("zdyndns_str: /"..zdyndns_str.."/") end + + srv_zdyndns = net.createConnection(net.TCP, 0) + + srv_zdyndns:on("receive", function(conn, pl) + print("receiving...") + print(pl) + end) + + srv_zdyndns:on("connection", function(sck) + if verbose then print("connected & sending...") end + sck:send(zdyndns_str, function(sk) + sk:close() + if verbose then print("close...") end + zdyndns_str=nil zip=nil srv_zdyndns=nil send_zdyndns=nil + if verbose then print(node.heap()) end + collectgarbage() + if verbose then print(node.heap()) end + end) + end) + + srv_zdyndns:connect(zdyndns_port,zdyndns_host) + +end + +send_zdyndns() diff --git a/SonOff/SonOff_2/_zremote_cmd.txt b/SonOff/SonOff_2/_zremote_cmd.txt index c61719f..c421d69 100644 --- a/SonOff/SonOff_2/_zremote_cmd.txt +++ b/SonOff/SonOff_2/_zremote_cmd.txt @@ -1,36 +1,16 @@ # Quelques commandes remote (luatool) à envoyer avec le plugin Atom-IDE-terminal de l'éditeur Atom -# zf200116.1833 +# zf200301.1318 Todo à faire pour ce projet ! -- trouver un moyen pour configurer la lED dans initz.lua sans le rendre dangereux en cas de problème (éviter le reflashing en cas de reboot loop) -- lancement ou arrêt du WEBIDE via la home page +- refaire le fichier _secrets_project.lua_ à partir de secrets_project.lua +- faire la documentation pour le fonctionnement de concept de rtelnet dans un fichier à part .md + - flip/flop du relais depuis la page api_sonoff -- arrêter de demander de connecter le WIFI automatiquement en mode station juste après le boot dans la config wifiinit (automatic connect) - ajouter le contrôle flip/flop du btn sur RELAY -- if verbose print partout dans *.lua -- le wifi setup ne fonctionne toujours pas quand il y a déjà un ap de connecté (problème du reboot quand adrs ip ok)! - -v- ajouter fonction restart dans z_index.html (ATTENTION avec un argument restart, pas besoin de faire une page restart.html !) -v- ajouter l'affichage du node_id dans z_index.html -v- ajouter le web_srv2 dans boot au lieu de boot2 -v- ajouter un état du RELAY et de la LED dans z_index.html -v- ajouter une page affichage variables globales -v- améliorer api_sonoff.html, il faut ajouter 'home' sur la page -v- améliorer le résultat de api_sonoff.html, il faut sortir l'état des IO lors d'un changement comme confirmation -v- copier secrets_project et secrets_wifi dans _secrets_project_ _secrets_wifi_ pour la documentation -v- déplacer le mode AP dans wifi_init -v- enlever la ligne wifi init dans z_index.html -v- faire la page web service pour allumer la LED -v- faire la page web service pour allumer le relay -v- garder variable node_id pour identification après poweron général, reattribution adrs ip sur modem 4G -v- passer à 10 secondes la seconde chance -v- passer à 15x les tentatives de connexions au WIFI -v- vérifier le fonctionnement d'allumer la LED, car ne marche plus ! -x- mettre ZLED2 et ZRELAY dans secrets_project -x-ajouter argument ok dans wifi_init.html (sécurité) - +- y regarder pour faire tourner en MEME temps le reverse et le forward telnet +- z lancement ou arrêt du WEBIDE via la home page @@ -45,11 +25,11 @@ x-ajouter argument ok dans wifi_init.html (sécurité) # définitions à faire AVANT ! export luatool_tty="/dev/cu.wchusbserial1410" -export zIP="192.168.0.182" +export zIP="192.168.0.150" export zport="23" export zIP="localhost" -export zport="2323" +export zport="23000" ATTENTION: voir les tunnels tout à la fin ! @@ -95,37 +75,58 @@ verbose=false zsort_rssi() zshow() -#commandes luatool pour ce projet le .137 est à jour avec la nouvelle version du wifi ! +################################ +# commandes lua pour ce projet # +################################ +ssh ubuntu@www.zuzutest.ml killall -9 socat +sleep 1 +killall -9 ssh +sleep 1 +ssh ubuntu@www.zuzutest.ml socat TCP-LISTEN:23001,reuseaddr,fork TCP-LISTEN:23000,reuseaddr,bind=127.0.0.1 & +watch -n 1 'ssh ubuntu@www.zuzutest.ml netstat -nat |grep 230' + +######## envoyer un CTRL-C dans le terminal ! + +ssh -N -L 23000:localhost:23000 ubuntu@www.zuzutest.ml & + +export zIP="localhost" +export zport="23000" +telnet -rN $zIP $zport +for k,v in pairs(_G) do print(k,v) end +~. +zdyn + +=node.heap() +collectgarbage() +=node.heap() + + +dofile("dir.lua") +dir() +for k,v in pairs(_G) do print(k,v) end +dofile("wifi_info.lua") + +node.restart() + + ~. ./luatool.py --ip $zIP:$zport -l ./luatool.py --ip $zIP:$zport -f wifi_init.lua - - -./luatool.py --ip $zIP:$zport -f secrets_wifi.lua -./luatool.py --ip $zIP:$zport -f initz.lua -t init.lua ./luatool.py --ip $zIP:$zport -f boot.lua -./luatool.py --ip $zIP:$zport -f boot2.lua -./luatool.py --ip $zIP:$zport -f wifi_init.lua -./luatool.py --ip $zIP:$zport -f set_time.lua -./luatool.py --ip $zIP:$zport -f wifi_info.lua -./luatool.py --ip $zIP:$zport -f c.lua -./luatool.py --ip $zIP:$zport -f cat.lua -./luatool.py --ip $zIP:$zport -f flash_led_xfois.lua -./luatool.py --ip $zIP:$zport -f head.lua +./luatool.py --ip $zIP:$zport -f 0_tst3_socat.lua +./luatool.py --ip $zIP:$zport -f 0_cron.lua +./luatool.py --ip $zIP:$zport -f secrets_project.lua +./luatool.py --ip $zIP:$zport -f 0_zdyndns.lua +./luatool.py --ip $zIP:$zport -f 0_btn_flipflop.lua -./luatool.py --ip $zIP:$zport -f b.lua -./luatool.py --ip $zIP:$zport -f web_srv2.lua +./luatool.py --ip $zIP:$zport -f api_sonoff.html ./luatool.py --ip $zIP:$zport -f z_index.html - -./luatool.py --ip $zIP:$zport -f wifi_get_conf.html -./luatool.py --ip $zIP:$zport -f wifi_set_conf.html +./luatool.py --ip $zIP:$zport -f initz.lua -t init.lua + + -./luatool.py --ip $zIP:$zport -f z_page1.html -./luatool.py --ip $zIP:$zport -f z_page2.html -./luatool.py --ip $zIP:$zport -f z_page3.html -./luatool.py --ip $zIP:$zport -f z_page4.html ./luatool.py --ip $zIP:$zport --delete wifi_ap_start.lua ./luatool.py --ip $zIP:$zport --delete wifi_cli_conf.lua @@ -183,7 +184,7 @@ for k,v in pairs(_G) do print(k,v) end # faire un cat d'un fichier sur le NodeMCU dofile("cat.lua") -cat("boot2.lua") +cat("secrets_project.lua") # commandes luatool diff --git a/SonOff/SonOff_2/api_sonoff.html b/SonOff/SonOff_2/api_sonoff.html index bd5203a..969d2f6 100644 --- a/SonOff/SonOff_2/api_sonoff.html +++ b/SonOff/SonOff_2/api_sonoff.html @@ -8,26 +8,31 @@