From c4aaeb3e826d67a1678a19d13f0ce7d3c7d7ef0a Mon Sep 17 00:00:00 2001 From: Christian Zufferey Date: Tue, 20 Nov 2018 07:31:14 +0100 Subject: [PATCH] =?UTF-8?q?-=20super=20bien=20avanc=C3=A9=20avec=20mon=20i?= =?UTF-8?q?nit.lua=20mini.=20-=20maintenant=20il=20s'endort=20pendant=2030?= =?UTF-8?q?=20secondes,=20se=20r=C3=A9veille,=20regarde=20s'il=20peut=20se?= =?UTF-8?q?=20connecter=20sur=20un=20wifi,=20si=20oui,=20d=C3=A9marre=20le?= =?UTF-8?q?=20web=20ide,=20autrement=20se=20rendort=20pour=2030=20secondes?= =?UTF-8?q?=20-=20aussi,=20le=20tout=20mini=20web=20ide=20fonctionne,=20c'?= =?UTF-8?q?est=20=C3=A0=20dire=20que=20je=20peux=20modifier=20depuis=20la?= =?UTF-8?q?=20connexion=20wifi=20le=20code=20lua=20sur=20le=20NodeMCU,=20s?= =?UTF-8?q?ans=20devoir=20brancher=20le=20NodeMCU=20sur=20le=20port=20USB?= =?UTF-8?q?=20-=20une=20sacr=C3=A9e=20avanc=C3=A9e=20que=20voici=20pour=20?= =?UTF-8?q?mon=20WPS/GPS=20que=20je=20suis=20en=20train=20d'=C3=A9crire=20?= =?UTF-8?q?:-)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Boot init.lua/mini/boot.lua | 21 +++- Boot init.lua/mini/cat.lua | 17 +++ Boot init.lua/mini/dir.lua | 15 +++ Boot init.lua/mini/dsleep.lua | 13 ++- Boot init.lua/mini/initz.lua | 8 +- Boot init.lua/mini/repair.lua | 6 +- Boot init.lua/mini/restart.lua | 5 + Boot init.lua/mini/test_file.lua | 4 - Boot init.lua/mini/web_ide2.lua | 157 ++++++++++++++++++++++++++ Boot init.lua/mini/wifi_ap_start.lua | 4 +- Boot init.lua/mini/wifi_cli_conf.lua | 4 + Boot init.lua/mini/wifi_cli_start.lua | 14 +++ Boot init.lua/mini/wifi_cli_stop.lua | 10 ++ Boot init.lua/mini/wifi_get_ip.lua | 12 ++ OS_utils/restart.lua | 5 + OS_utils/test_dofile.lua | 5 + Wifi/wifi_ap_start.lua | 4 +- Wifi/wifi_cli_conf.lua | 2 +- Wifi/wifi_cli_start.lua | 4 +- Wifi/wifi_get_ip.lua | 4 +- 20 files changed, 285 insertions(+), 29 deletions(-) create mode 100644 Boot init.lua/mini/cat.lua create mode 100644 Boot init.lua/mini/dir.lua create mode 100644 Boot init.lua/mini/restart.lua delete mode 100644 Boot init.lua/mini/test_file.lua create mode 100644 Boot init.lua/mini/web_ide2.lua create mode 100644 Boot init.lua/mini/wifi_cli_conf.lua create mode 100644 Boot init.lua/mini/wifi_cli_start.lua create mode 100644 Boot init.lua/mini/wifi_cli_stop.lua create mode 100644 Boot init.lua/mini/wifi_get_ip.lua create mode 100644 OS_utils/restart.lua create mode 100644 OS_utils/test_dofile.lua diff --git a/Boot init.lua/mini/boot.lua b/Boot init.lua/mini/boot.lua index 83730db..b7c2795 100644 --- a/Boot init.lua/mini/boot.lua +++ b/Boot init.lua/mini/boot.lua @@ -1,11 +1,24 @@ -- Scripts à charger après le boot pour démarrer son appli -print("\n boot.lua zf181118.2329 \n") +print("\n boot.lua zf181120.0016 \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 +f= "wifi_ap_stop.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_srv.lua" if file.exists(f) then dofile(f) end +f= "web_ide2.lua" if file.exists(f) then dofile(f) end +f= "dsleep.lua" if file.exists(f) then dofile(f) end +x_dsleep=7 y_dsleep=30 dsleep() + +i=1 jobtimer1=tmr.create() tmr.alarm(jobtimer1, 1*1000, tmr.ALARM_AUTO, function() - print("coucou...") + print(i) i=i+1 + if wifi.sta.getip() ~= nil then + tmr.stop(jobtimer1) + tmr.stop(ztmr_SLEEP) + x_dsleep=300 y_dsleep=30 dsleep() + print("c'est connecté...") + end end) diff --git a/Boot init.lua/mini/cat.lua b/Boot init.lua/mini/cat.lua new file mode 100644 index 0000000..7c309b9 --- /dev/null +++ b/Boot init.lua/mini/cat.lua @@ -0,0 +1,17 @@ +-- fonction cat() pour afficher le contenu d'un fichier dans la flash +print("\n cat.lua zf180826.1109 \n") + +function cat(zfile) + print("\n"..zfile.."\n-------------------------------") + + zfilei = file.open(zfile, "r") + i=1 + zline=file.readline() + repeat + print(i..": "..string.sub(zline,1,string.len(zline)-1)) + i=i+1 zline=file.readline() + until zline== nil + file.close(zfilei) + + print("-------------------------------") +end diff --git a/Boot init.lua/mini/dir.lua b/Boot init.lua/mini/dir.lua new file mode 100644 index 0000000..8bf9414 --- /dev/null +++ b/Boot init.lua/mini/dir.lua @@ -0,0 +1,15 @@ +-- fonction dir() pour afficher les fichiers dans la flash +print("\n dir.lua zf180826.1019 \n") + +function dir() + print("\n-------------------------------") + 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("-------------------------------") + print('\nUsed: '..i..' bytes\nusage: dofile("file.lua")\n') +end + +dir() diff --git a/Boot init.lua/mini/dsleep.lua b/Boot init.lua/mini/dsleep.lua index ab364f7..43edb7d 100644 --- a/Boot init.lua/mini/dsleep.lua +++ b/Boot init.lua/mini/dsleep.lua @@ -2,10 +2,13 @@ -- 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") +print("\n dsleep.lua zf181119.2343 \n") ztmr_SLEEP = tmr.create() -tmr.alarm(ztmr_SLEEP, 2*1000, tmr.ALARM_SINGLE, function () - print("Je dors...") - node.dsleep(15*1000*1000) -end) + +function dsleep() + tmr.alarm(ztmr_SLEEP, x_dsleep*1000, tmr.ALARM_SINGLE, function () + print("Je dors...") + node.dsleep(y_dsleep*1000*1000) + end) +end \ No newline at end of file diff --git a/Boot init.lua/mini/initz.lua b/Boot init.lua/mini/initz.lua index 5fa54c8..474ee0c 100644 --- a/Boot init.lua/mini/initz.lua +++ b/Boot init.lua/mini/initz.lua @@ -2,17 +2,17 @@ -- 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") +print("\n init.lua zf181120.0002 \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 + f= "repair.lua" if file.exists(f) then dofile(f) end initalarme=tmr.create() tmr.alarm(initalarme, 30*1000, tmr.ALARM_SINGLE, function() - if file.exists("boot.lua") then dofile("boot.lua") end + f= "boot.lua" if file.exists(f) then dofile(f) end end) else - if file.exists("boot.lua") then dofile("boot.lua") end + f= "boot.lua" if file.exists(f) then dofile(f) end end diff --git a/Boot init.lua/mini/repair.lua b/Boot init.lua/mini/repair.lua index b727ccf..400e952 100644 --- a/Boot init.lua/mini/repair.lua +++ b/Boot init.lua/mini/repair.lua @@ -1,9 +1,9 @@ -- Scripts de seconde chance pour réparer une boucle dans le restart -print("\n repair.lua zf181119.006 \n") +print("\n repair.lua zf181119.2356 \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 +f= "wifi_ap_start.lua" if file.exists(f) then dofile(f) end +f= "telnet_srv.lua" if file.exists(f) then dofile(f) end jobtimer1=tmr.create() tmr.alarm(jobtimer1, 5*1000, tmr.ALARM_AUTO, function() diff --git a/Boot init.lua/mini/restart.lua b/Boot init.lua/mini/restart.lua new file mode 100644 index 0000000..50a1ebd --- /dev/null +++ b/Boot init.lua/mini/restart.lua @@ -0,0 +1,5 @@ +--Redémarre le NodeMCU + +print("\n restart.lua zf181120.002 \n") + +node.restart() diff --git a/Boot init.lua/mini/test_file.lua b/Boot init.lua/mini/test_file.lua deleted file mode 100644 index 9053170..0000000 --- a/Boot init.lua/mini/test_file.lua +++ /dev/null @@ -1,4 +0,0 @@ ---test si un fichier existe ---zf181118.2237 - -if file.exists("boot.lua") then dofile("boot.lua") end diff --git a/Boot init.lua/mini/web_ide2.lua b/Boot init.lua/mini/web_ide2.lua new file mode 100644 index 0000000..d2bcee3 --- /dev/null +++ b/Boot init.lua/mini/web_ide2.lua @@ -0,0 +1,157 @@ +-- Petit WEB IDE tout simple autonome + +print("\n web_ide2.lua zf181119.1505 \n") + +--[[ +XChip's NodeMCU IDE + +Create, Edit and run NodeMCU files using your webbrowser. +Examples: +http:/// will list all the files in the MCU +http:///newfile.lua displays the file on your browser +http:///newfile.lua?edit allows to creates or edits the specified script in your browser +http:///newfile.lua?run it will run the specified script and will show the returned value +]]-- + +srv=net.createServer(net.TCP) +srv:listen(80,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("

NodeMCU IDE

") + + if vars=="edit" then + conn:send("") + conn:send("Back to file list


") + conn:send("run") + end + + if vars=="run" then + conn:send("") + local st, result=pcall(dofile, url) + conn:send(tostring(result)) + conn:send("") + end + + if url=="" then + local l = file.list(); + for k,v in pairs(l) do + conn:send(""..k..", size:"..v.."
") + end + end + + conn:send("") + + 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()) + diff --git a/Boot init.lua/mini/wifi_ap_start.lua b/Boot init.lua/mini/wifi_ap_start.lua index 5934835..60b5793 100644 --- a/Boot init.lua/mini/wifi_ap_start.lua +++ b/Boot init.lua/mini/wifi_ap_start.lua @@ -1,6 +1,6 @@ -- Démarre le WIFI en mode AP -print("\n wifi_ap_start.lua zf181118.2331 \n") +print("\n wifi_ap_start.lua zf181119.2359 \n") local zmodewifi=wifi.getmode() if zmodewifi == wifi.NULLMODE then @@ -11,4 +11,4 @@ elseif zmodewifi == wifi.STATION then 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 +f= "wifi_info.lua" if file.exists(f) then dofile(f) end diff --git a/Boot init.lua/mini/wifi_cli_conf.lua b/Boot init.lua/mini/wifi_cli_conf.lua new file mode 100644 index 0000000..f01a799 --- /dev/null +++ b/Boot init.lua/mini/wifi_cli_conf.lua @@ -0,0 +1,4 @@ +-- 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} diff --git a/Boot init.lua/mini/wifi_cli_start.lua b/Boot init.lua/mini/wifi_cli_start.lua new file mode 100644 index 0000000..f1091ac --- /dev/null +++ b/Boot init.lua/mini/wifi_cli_start.lua @@ -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 diff --git a/Boot init.lua/mini/wifi_cli_stop.lua b/Boot init.lua/mini/wifi_cli_stop.lua new file mode 100644 index 0000000..9429aa5 --- /dev/null +++ b/Boot init.lua/mini/wifi_cli_stop.lua @@ -0,0 +1,10 @@ +-- Démarre le WIFI en mode AP +print("\n wifi_cli_stop.lua zf180824.2000 \n") + +local zmodewifi=wifi.getmode() +if zmodewifi == wifi.STATION then + wifi.setmode(wifi.NULLMODE) +elseif zmodewifi == wifi.STATIONAP then + wifi.setmode(wifi.SOFTAP) +end +print("WIFI CLI arrêté") diff --git a/Boot init.lua/mini/wifi_get_ip.lua b/Boot init.lua/mini/wifi_get_ip.lua new file mode 100644 index 0000000..82bcdc2 --- /dev/null +++ b/Boot init.lua/mini/wifi_get_ip.lua @@ -0,0 +1,12 @@ +-- Petit script pour obtenir l'adresse IP du NodeMCU connecté sur un AP Wifi +print("\n wifi_get_ip.lua zf181119.2318 \n") + +wifitimer1=tmr.create() +tmr.alarm(wifitimer1, 1000, tmr.ALARM_AUTO , function() + if wifi.sta.getip() == nil then + print("Connecting to AP...") + else + tmr.stop(wifitimer1) + f= "wifi_info.lua" if file.exists(f) then dofile(f) end + end +end) diff --git a/OS_utils/restart.lua b/OS_utils/restart.lua new file mode 100644 index 0000000..50a1ebd --- /dev/null +++ b/OS_utils/restart.lua @@ -0,0 +1,5 @@ +--Redémarre le NodeMCU + +print("\n restart.lua zf181120.002 \n") + +node.restart() diff --git a/OS_utils/test_dofile.lua b/OS_utils/test_dofile.lua new file mode 100644 index 0000000..74cb579 --- /dev/null +++ b/OS_utils/test_dofile.lua @@ -0,0 +1,5 @@ +--test si un fichier existe + +print("\n test_dofile.lua zf181119.2356 \n") + +f= "boot.lua" if file.exists(f) then dofile(f) end diff --git a/Wifi/wifi_ap_start.lua b/Wifi/wifi_ap_start.lua index 5934835..60b5793 100644 --- a/Wifi/wifi_ap_start.lua +++ b/Wifi/wifi_ap_start.lua @@ -1,6 +1,6 @@ -- Démarre le WIFI en mode AP -print("\n wifi_ap_start.lua zf181118.2331 \n") +print("\n wifi_ap_start.lua zf181119.2359 \n") local zmodewifi=wifi.getmode() if zmodewifi == wifi.NULLMODE then @@ -11,4 +11,4 @@ elseif zmodewifi == wifi.STATION then 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 +f= "wifi_info.lua" if file.exists(f) then dofile(f) end diff --git a/Wifi/wifi_cli_conf.lua b/Wifi/wifi_cli_conf.lua index 2ed3fe9..f01a799 100644 --- a/Wifi/wifi_cli_conf.lua +++ b/Wifi/wifi_cli_conf.lua @@ -1,4 +1,4 @@ -- Petit script pour configurer le client WIFI du NodeMCU print("\n wifi_cli_conf.lua zf180824.2000 \n") -wifi.sta.config{ssid="apzuzu6", pwd="12234567", save=true} +wifi.sta.config{ssid="3g-s7", pwd="12234567", save=true} diff --git a/Wifi/wifi_cli_start.lua b/Wifi/wifi_cli_start.lua index aac8bea..f1091ac 100644 --- a/Wifi/wifi_cli_start.lua +++ b/Wifi/wifi_cli_start.lua @@ -1,5 +1,5 @@ -- Petit script pour connecter le NodeMCU sur un AP Wifi avec l'accompte sauvé en EEPROM -print("\n wifi_cli_start.lua zf180824.2000 \n") +print("\n wifi_cli_start.lua zf181119.2359 \n") local zmodewifi=wifi.getmode() if zmodewifi == wifi.NULLMODE then @@ -11,4 +11,4 @@ elseif zmodewifi == wifi.SOFTAP then end wifi.sta.autoconnect(1) wifi.sta.connect() -dofile("wifi_get_ip.lua") +f= "wifi_get_ip.lua" if file.exists(f) then dofile(f) end diff --git a/Wifi/wifi_get_ip.lua b/Wifi/wifi_get_ip.lua index 83b6369..82bcdc2 100644 --- a/Wifi/wifi_get_ip.lua +++ b/Wifi/wifi_get_ip.lua @@ -1,5 +1,5 @@ -- Petit script pour obtenir l'adresse IP du NodeMCU connecté sur un AP Wifi -print("\n wifi_get_ip.lua zf180824.2000 \n") +print("\n wifi_get_ip.lua zf181119.2318 \n") wifitimer1=tmr.create() tmr.alarm(wifitimer1, 1000, tmr.ALARM_AUTO , function() @@ -7,6 +7,6 @@ tmr.alarm(wifitimer1, 1000, tmr.ALARM_AUTO , function() print("Connecting to AP...") else tmr.stop(wifitimer1) - dofile("wifi_info.lua") + f= "wifi_info.lua" if file.exists(f) then dofile(f) end end end)