diff --git a/Workshop/181015/web_srv/web_srv.lua b/Workshop/181015/web_srv/web_srv.lua index 9cb7b73..ca0f751 100644 --- a/Workshop/181015/web_srv/web_srv.lua +++ b/Workshop/181015/web_srv/web_srv.lua @@ -2,22 +2,22 @@ print("\n web_srv.lua zf181018.1610 \n") -dofile("web_get.lua") -dofile("web_html.lua") +--dofile("web_get.lua") +--dofile("web_html.lua") srv = net.createServer(net.TCP) srv:listen(80, function(conn) conn:on("receive", function(client, request) _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP") - --print("\n\nweb_srv") - --print("method: ",method) - --print("path: ",path) - --print("request: ",request) - --print("vars: ",vars) + print("\n\nweb_srv debug") + print("request: \n---\n"..request.."---") + print("method: ",method) + print("path: ",path) + print("vars: ",vars) if not string.find(request, "/favicon.ico") then - --print("coucou") + print("coucou") if (method == nil) then _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP") end @@ -28,9 +28,11 @@ srv:listen(80, function(conn) print(k..": "..v) end end - web_get() - client:send(buf) - buf=nil + file_html=string.gsub(path, "/", "") + print("file_html: ",file_html) + + conn:send("

ESP8266
Server is working!

\n\n") + end end) conn:on("sent", function(c) c:close() end) diff --git a/zyx-framework/a_test_ram.lua b/zyx-framework/a_test_ram.lua new file mode 100644 index 0000000..de9d601 --- /dev/null +++ b/zyx-framework/a_test_ram.lua @@ -0,0 +1,69 @@ +-- Scripts pour tester l'occupation méroire des différents modules +-- source: + +print("\n a_test_ram.lua zf190126.1117 \n") + +test_ram_alarm1=tmr.create() +test_ram_alarm2=tmr.create() +zram0=node.heap() zram1=node.heap() + +tmr.alarm(test_ram_alarm1, 1*1000, tmr.ALARM_AUTO, function() + zram2=node.heap() print(zram0, zram1, zram2, zram1-zram2, zram0-zram2) +end) + +--[[ +file.remove("telnet_srv2.lc") +file.remove("web_ide2.lc") +file.remove("web_srv.lc") +file.remove("set_time.lc") + +node.compile("telnet_srv2.lua") +node.compile("web_ide2.lua") +node.compile("web_srv.lua") +node.compile("set_time.lua") + +file.remove("telnet_srv2.lua") +file.remove("web_ide2.lua") +file.remove("web_srv.lua") +file.remove("set_time.lua") +]] + +tmr.alarm(test_ram_alarm2, 5*1000, tmr.ALARM_SINGLE, function() + zram1=node.heap() +-- dofile("telnet_srv2.lua") +-- dofile("web_ide2.lua") +-- dofile("web_srv.lua") +-- dofile("set_time.lua") + dofile("web_html.lua") + + +-- dofile("telnet_srv2.lc") +-- dofile("web_ide2.lc") +-- dofile("web_srv.lc") + dofile("set_time.lc") + + zram2=node.heap() +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 + +--[[ +tmr.stop(test_ram_alarm1) +package.loaded ["telnet_srv2.lua.lua"] = nil +package.loaded ["wifi_ap_start.lua"] = nil + + +]] + + +--[[ +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_srv.lua" if file.exists(f) then dofile(f) end +f= "set_time.lua" if file.exists(f) then dofile(f) end +]] diff --git a/zyx-framework/b.lua b/zyx-framework/b.lua new file mode 100644 index 0000000..801a343 --- /dev/null +++ b/zyx-framework/b.lua @@ -0,0 +1,53 @@ +-- Scripts pour tester le sniffer de smartphone qui essaient de se connecter sur des AP WIFI +-- source: https://nodemcu.readthedocs.io/en/dev/modules/wifi/#wifieventmonregister + +print("\n b.lua zf190119.1920 \n") + +--f= "set_time.lua" if file.exists(f) then dofile(f) end + +-- apzuzu6 38:2c:4a:4e:d3:d8 + +zmac_adrs={} +zmac_adrs["b8:d7:af:a6:bd:86"]={["zname"]="S7 zf"} +zmac_adrs["cc:c0:79:7d:f5:d5"]={["zname"]="S7 Mélanie"} +zmac_adrs["5c:f9:38:a1:f7:f0"]={["zname"]="MAC zf"} +zmac_adrs["d8:30:62:5a:d6:3a"]={["zname"]="IMAC Maman"} + +function zshow() + for k, v in pairs(zmac_adrs) do + print(k,zmac_adrs[k]["zname"],zmac_adrs[k]["zrssi"],zmac_adrs[k]["ztime"]) + end +end +--[[ +zshow() +]] + +function zround(num, dec) + local mult = 10^(dec or 0) + return math.floor(num * mult + 0.5) / mult +end + +function zsniff(T) + print("\n\tMAC: ".. T.MAC.."\n\tRSSI: "..T.RSSI) + ztime() + if zmac_adrs[T.MAC] == nil then + print("Oh une inconnue !") + zmac_adrs[T.MAC]={} + end + zmac_adrs[T.MAC]["ztime"]=string.format("%04d/%02d/%02d %02d:%02d:%02d", tm["year"], tm["mon"], tm["day"], tm["hour"], tm["min"], tm["sec"]) + if zmac_adrs[T.MAC]["zrssi"] == nil then + zmac_adrs[T.MAC]["zrssi"]=T.RSSI + else + zmac_adrs[T.MAC]["zrssi"]=zround((4*zmac_adrs[T.MAC]["zrssi"]+T.RSSI)/5, 0) + end + if zmac_adrs[T.MAC]["zname"] ~= nil then + print("Bonjour "..zmac_adrs[T.MAC]["zname"].." !") + end +end + +wifi.eventmon.register(wifi.eventmon.AP_PROBEREQRECVED, zsniff) +--[[ +wifi.eventmon.unregister(wifi.eventmon.AP_PROBEREQRECVED) +]] + + diff --git a/zyx-framework/boot.lua b/zyx-framework/boot.lua new file mode 100644 index 0000000..a83edb8 --- /dev/null +++ b/zyx-framework/boot.lua @@ -0,0 +1,30 @@ +-- Scripts à charger après le boot pour démarrer son appli + +print("\n boot.lua zf190119.1933 \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_srv.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 + + + +heartbeat() + + + + diff --git a/zyx-framework/flash_led_xfois.lua b/zyx-framework/flash_led_xfois.lua new file mode 100644 index 0000000..c50e5ab --- /dev/null +++ b/zyx-framework/flash_led_xfois.lua @@ -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 () + + + diff --git a/zyx-framework/initz.lua b/zyx-framework/initz.lua new file mode 100644 index 0000000..356caa1 --- /dev/null +++ b/zyx-framework/initz.lua @@ -0,0 +1,35 @@ +--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 zf181210.2358 \n") + +function second_chance() + print("seconde chance...") + f= "repair.lua" if file.exists(f) then dofile(f) end + initalarme=tmr.create() + tmr.alarm(initalarme, 8*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") + f= "boot.lua" if file.exists(f) then dofile(f) end +else + print("autre raison") + second_chance() +end + diff --git a/zyx-framework/repair.lua b/zyx-framework/repair.lua new file mode 100644 index 0000000..691c123 --- /dev/null +++ b/zyx-framework/repair.lua @@ -0,0 +1,16 @@ +-- Scripts de seconde chance pour réparer une boucle dans le restart + +print("\n repair.lua zf181210.1456 \n") + +--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 + +--f= "az_init_led.lua" if file.exists(f) then dofile(f) end + + +--[[ +jobtimer1=tmr.create() +tmr.alarm(jobtimer1, 5*1000, tmr.ALARM_AUTO, function() + print("repair...") +end) +]] diff --git a/zyx-framework/restart.lua b/zyx-framework/restart.lua new file mode 100644 index 0000000..85f28eb --- /dev/null +++ b/zyx-framework/restart.lua @@ -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") + + diff --git a/zyx-framework/rm_files.lua b/zyx-framework/rm_files.lua new file mode 100644 index 0000000..81241cb --- /dev/null +++ b/zyx-framework/rm_files.lua @@ -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") diff --git a/zyx-framework/set_time.lua b/zyx-framework/set_time.lua new file mode 100644 index 0000000..71bea27 --- /dev/null +++ b/zyx-framework/set_time.lua @@ -0,0 +1,24 @@ +-- Scripts pour régler l'horloge quand on est connecté en WIFI + +print("\n set_time.lua zf190119.1728 \n") + +--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 + + + +function set_time() + sntp.sync(nil, nil, nil, 1) +end + +function ztime() + tm = rtctime.epoch2cal(rtctime.get()+3600) + print(string.format("%04d/%02d/%02d %02d:%02d:%02d", tm["year"], tm["mon"], tm["day"], tm["hour"], tm["min"], tm["sec"])) +end + +set_time() +ztime() diff --git a/zyx-framework/telnet_srv2.lua b/zyx-framework/telnet_srv2.lua new file mode 100644 index 0000000..821358c --- /dev/null +++ b/zyx-framework/telnet_srv2.lua @@ -0,0 +1,87 @@ +-- Serveur telnet pour connexion en remote WIFI, NOUVELLE VERSION ! +-- source: https://github.com/nodemcu/nodemcu-firmware/blob/master/lua_examples/telnet/telnet.lua + +print("\n telnet_srv2.lua zf181215.1326 \n") + +local node, table, tmr, wifi, uwrite, tostring = + node, table, tmr, wifi, 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 + node.output(nil) + end + + 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("Telnet server running...\nUsage: telnet -rN ip\n") diff --git a/zyx-framework/tests/FKT6N22IJEMN0VK/bob.htm b/zyx-framework/tests/FKT6N22IJEMN0VK/bob.htm new file mode 100755 index 0000000..5bf3107 --- /dev/null +++ b/zyx-framework/tests/FKT6N22IJEMN0VK/bob.htm @@ -0,0 +1,28 @@ + + +ESP8266 Bob information page + + + +

ESP8266 server

+Home
+___________________

+

Employee Information:

+
+Bob Brown
+1234 Onix Dr.
+Barstow, Ca 52266
+905-333-3333 ext 125
+
+Bob@someemail.com
+
+Currently out of office. + +
___________________
+ + diff --git a/zyx-framework/tests/FKT6N22IJEMN0VK/index.htm b/zyx-framework/tests/FKT6N22IJEMN0VK/index.htm new file mode 100755 index 0000000..4567421 --- /dev/null +++ b/zyx-framework/tests/FKT6N22IJEMN0VK/index.htm @@ -0,0 +1,22 @@ + + + ESP8266 home page + + + +

ESP8266 server


+___________________

+

Employee Information:

+

+Bob
+Jill
+Barb
+

+
___________________
+ + diff --git a/zyx-framework/tests/FKT6N22IJEMN0VK/newserver.lua b/zyx-framework/tests/FKT6N22IJEMN0VK/newserver.lua new file mode 100755 index 0000000..a76211d --- /dev/null +++ b/zyx-framework/tests/FKT6N22IJEMN0VK/newserver.lua @@ -0,0 +1,23 @@ +print("tutu") +srv=net.createServer(net.TCP) +srv:listen(80,function(conn) + conn:on("receive", function(client,payload) + tgtfile = string.sub(payload,string.find(payload,"GET /") + +5,string.find(payload,"HTTP/")-2) + print(payload) + if tgtfile == "" then tgtfile = "index.htm" end + + local f = file.open(tgtfile,"r") + if f ~= nil then + client:send(file.read()) + file.close() + else + client:send(""..tgtfile.." not found - 404 error.
Home
") + end +-- client:close(); +-- collectgarbage(); + conn:on("sent", function(c) c:close() end) + f = nil + tgtfile = nil + end) +end) diff --git a/zyx-framework/tests/server.lua b/zyx-framework/tests/server.lua new file mode 100644 index 0000000..5ea35d4 --- /dev/null +++ b/zyx-framework/tests/server.lua @@ -0,0 +1,9 @@ +srv=net.createServer(net.TCP) +srv:listen(80,function(conn) + conn:on("receive",function(conn,payload) + print(payload) + conn:send("

ESP8266
Server is working!

") + conn:close() + end) +end) + diff --git a/zyx-framework/web_html.lua b/zyx-framework/web_html.lua new file mode 100644 index 0000000..648ff29 --- /dev/null +++ b/zyx-framework/web_html.lua @@ -0,0 +1,32 @@ +-- petit script pour le HTML du serveur web + +print("\n web_html.lua zf190119.2018 \n") + +--Partie HTML et CSS pour la page web +function html_home() + buf = "\n" + buf = buf .. "

Hello, this is NodeMCU WIFI Sniffer 2008

\n" + buf = buf .. string.format("%04d/%02d/%02d %02d:%02d:%02d", tm["year"], tm["mon"], tm["day"], tm["hour"], tm["min"], tm["sec"]) .. "

\n" + buf = buf .. "\n" + for k, v in pairs(zmac_adrs) do + buf = buf .. "\n" + buf = buf .. "\n" + buf = buf .. "\n" + buf = buf .. "\n" + buf = buf .. "\n" + end + buf = buf .. "
MACNameRSSITime
" .. k .. "" .. tostring(zmac_adrs[k]["zname"]) .. "" .. tostring(zmac_adrs[k]["zrssi"]) .. "" .. tostring(zmac_adrs[k]["ztime"]) .. "
\n" + buf = buf .. "" +end + +function html_status() + buf = "\n" + buf = buf .. "

Hello, this is NodeMCU. 1608

\n" + buf = buf .. "Toto\n" + if gpio.read(zLED) == 1 then + buf = buf .. "

Led is off

\n" + else + buf = buf .. "

Led is on

\n" + end + buf = buf .. "" +end diff --git a/zyx-framework/web_ide2.lua b/zyx-framework/web_ide2.lua new file mode 100644 index 0000000..16d00f2 --- /dev/null +++ b/zyx-framework/web_ide2.lua @@ -0,0 +1,159 @@ +-- Petit WEB IDE tout simple autonome +-- ATTENTION: tourne sur le port 88 ! + +print("\n _web_ide2.lua zf181210.1516 \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(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("

NodeMCU IDE

") + + if vars=="edit" then + conn:send("") + conn:send("

Back to file list\n") + conn:send("

run") + conn:send("



") + 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/zyx-framework/web_srv.lua b/zyx-framework/web_srv.lua new file mode 100644 index 0000000..16ad45e --- /dev/null +++ b/zyx-framework/web_srv.lua @@ -0,0 +1,38 @@ +-- petit script de serveur WEB Wifi + +print("\n web_srv.lua zf190119.1932 \n") + +--dofile("web_get.lua") +dofile("web_html.lua") + +srv = net.createServer(net.TCP) +srv:listen(80, function(conn) + conn:on("receive", function(client, request) + _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP") + + --print("\n\nweb_srv") + --print("method: ",method) + --print("path: ",path) + --print("request: ",request) + --print("vars: ",vars) + + if not string.find(request, "/favicon.ico") then + --print("coucou") + if (method == nil) then + _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP") + end + _GET = {} + if (vars ~= nil) then + for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do + _GET[k] = v + print(k..": "..v) + end + end +-- web_get() + html_home() + client:send(buf) + buf=nil + end + end) + conn:on("sent", function(c) c:close() end) +end) diff --git a/zyx-framework/web_srv2.lua b/zyx-framework/web_srv2.lua new file mode 100644 index 0000000..16ad45e --- /dev/null +++ b/zyx-framework/web_srv2.lua @@ -0,0 +1,38 @@ +-- petit script de serveur WEB Wifi + +print("\n web_srv.lua zf190119.1932 \n") + +--dofile("web_get.lua") +dofile("web_html.lua") + +srv = net.createServer(net.TCP) +srv:listen(80, function(conn) + conn:on("receive", function(client, request) + _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP") + + --print("\n\nweb_srv") + --print("method: ",method) + --print("path: ",path) + --print("request: ",request) + --print("vars: ",vars) + + if not string.find(request, "/favicon.ico") then + --print("coucou") + if (method == nil) then + _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP") + end + _GET = {} + if (vars ~= nil) then + for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do + _GET[k] = v + print(k..": "..v) + end + end +-- web_get() + html_home() + client:send(buf) + buf=nil + end + end) + conn:on("sent", function(c) c:close() end) +end) diff --git a/zyx-framework/wifi_ap_start.lua b/zyx-framework/wifi_ap_start.lua new file mode 100644 index 0000000..2916ccb --- /dev/null +++ b/zyx-framework/wifi_ap_start.lua @@ -0,0 +1,15 @@ +-- Démarre le WIFI en mode AP + +print("\n wifi_ap_start.lua zf190125.1715 \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" }) + +--f= "wifi_info.lua" if file.exists(f) then dofile(f) end diff --git a/zyx-framework/wifi_cli_conf.lua b/zyx-framework/wifi_cli_conf.lua new file mode 100644 index 0000000..365c595 --- /dev/null +++ b/zyx-framework/wifi_cli_conf.lua @@ -0,0 +1,12 @@ +-- Petit script pour configurer le client WIFI du NodeMCU + +print("\n wifi_cli_conf.lua zf190126.1533 \n") + +--credentials par défaut +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} diff --git a/zyx-framework/wifi_cli_start.lua b/zyx-framework/wifi_cli_start.lua new file mode 100644 index 0000000..f1091ac --- /dev/null +++ b/zyx-framework/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/zyx-framework/wifi_info.lua b/zyx-framework/wifi_info.lua new file mode 100644 index 0000000..5062fcf --- /dev/null +++ b/zyx-framework/wifi_info.lua @@ -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