- tester pour voir si j'arrive à me connecter rapidement à un WIFI AP, le test est positif

- testé un seuper mini WEB IDE
This commit is contained in:
Christian Zufferey
2018-11-19 23:52:29 +01:00
parent 8b78b4aa04
commit d70ecc6173
20 changed files with 1567 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
-- programme pour faire clignoter une LED version simplifiée
print("\n blink_led2.lua zf181015.1138 \n")
zLED=0
gpio.mode(zLED, gpio.OUTPUT)
ztmr_LED = tmr.create()
value = true
tmr.alarm(ztmr_LED, 100, tmr.ALARM_AUTO, function ()
if value then
gpio.write(zLED, gpio.HIGH)
else
gpio.write(zLED, gpio.LOW)
end
value = not value
end)

View File

@@ -0,0 +1,13 @@
-- Scripts à charger après le boot pour démarrer son appli
print("\n boot.lua zf181119.1518 \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
--if file.exists("wifi_cli_start.lua") then dofile("wifi_cli_start.lua") end
if file.exists("repair.lua") then dofile("repair.lua") end
jobtimer1=tmr.create()
--tmr.alarm(jobtimer1, 1*1000, tmr.ALARM_AUTO, function()
-- print("coucou...")
--end)

View File

@@ -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

View File

@@ -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()

View File

@@ -0,0 +1,249 @@
-- Petit WEB IDE tout simple à tester
print("\n ide.lua zf181119.1419 \n")
--[[
XChip's NodeMCU IDE
Original Source: http://www.esp8266.com/viewtopic.php?f=19&t=1549
Petr Stehlik found the source in October 2016 and gave it a new home
at https://github.com/joysfera/nodemcu-web-ide under the GPL license.
Then updated it for new async socket send(), fixed, cleaned up,
added external editor with syntax highlighting and further improves it.
Create, Edit and run NodeMCU files using your web browser.
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
--]]
local function editor(aceEnabled) -- feel free to disable the shiny Ajax.org Cloud Editor
local AceEnabled = aceEnabled == nil and true or aceEnabled
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(sck, payload)
if Status == 0 then
_, _, method, url, vars = string.find(payload, "([A-Z]+) /([^?]*)%??(.*) HTTP")
-- print("Method, URL, vars: ", 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 then
DataToGet = tonumber(DataToGet)
-- print("DataToGet = "..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 then
DataToGet = DataToGet - string.len(payload)
--print("DataToGet:", DataToGet, "payload len:", string.len(payload))
file.open(url, "a+")
file.write(payload)
file.close()
else
sck:send("HTTP/1.1 200 OK\r\n\r\nERROR")
Status = 0
end
if DataToGet == 0 then
sck:send("HTTP/1.1 200 OK\r\n\r\nOK")
Status = 0
end
end
return
end
-- end of POST method handling
DataToGet = -1
if url == "favicon.ico" then
-- print("favicon.ico handler sends 404")
sck:send("HTTP/1.1 404 file not found")
return
end
local sen = "HTTP/1.1 200 OK\r\n\r\n"
-- it wants a file in particular
if url ~= "" and vars == "" then
DataToGet = 0
sck:send(sen)
return
end
sen = sen .. "<html><body><h1><a href='/'>NodeMCU IDE</a></h1>"
if vars == "edit" then
if AceEnabled then
local mode = 'ace/mode/'
if url:match(".css") then mode = mode .. 'css'
elseif url:match(".html") then mode = mode .. 'html'
elseif url:match(".json") then mode = mode .. 'json'
elseif url:match(".js") then mode = mode .. 'javascript'
else mode = mode .. 'lua'
end
sen = sen .. "<style type='text/css'>#editor{width: 100%; height: 80%}</style><div id='editor'></div><script src='//rawgit.com/ajaxorg/ace-builds/master/src-min-noconflict/ace.js'></script>"
.. "<script>var e=ace.edit('editor');e.setTheme('ace/theme/monokai');e.getSession().setMode('"..mode.."');function getSource(){return e.getValue();};function setSource(s){e.setValue(s);}</script>"
else
sen = sen .. "<textarea name=t cols=79 rows=17></textarea></br>"
.. "<script>function getSource() {return document.getElementsByName('t')[0].value;};function setSource(s) {document.getElementsByName('t')[0].value = s;};</script>"
end
sen = sen .. "<script>function tag(c){document.getElementsByTagName('w')[0].innerHTML=c};var x=new XMLHttpRequest();x.onreadystatechange=function(){if(x.readyState==4) setSource(x.responseText);};"
.. "x.open('GET',location.pathname);x.send()</script><button onclick=\"tag('Saving, wait!');x.open('POST',location.pathname);x.onreadystatechange=function(){console.log(x.readyState);"
.. "if(x.readyState==4) tag(x.responseText);};x.send(new Blob([getSource()],{type:'text/plain'}));\">Save</button> <a href='?run'>run</a> <w></w>"
elseif vars == "run" then
sen = sen .. "<verbatim>"
function s_output(str) sen = sen .. str end
node.output(s_output, 0) -- re-direct output to function s_output.
local st, result = pcall(dofile, url)
-- delay the output capture by 1000 milliseconds to give some time to the user routine in pcall()
tmr.alarm(0, 1000, tmr.ALARM_SINGLE, function()
node.output(nil)
if result then
local outp = tostring(result):sub(1,1300) -- to fit in one send() packet
result = nil
sen = sen .. "<br>Result of the run: " .. outp .. "<br>"
end
sen = sen .. "</verbatim></body></html>"
sck:send(sen)
end)
return
elseif vars == "compile" then
collectgarbage()
node.compile(url)
url = ""
elseif vars == "delete" then
file.remove(url)
url = ""
elseif vars == "restart" then
node.restart()
return
end
local message = {}
message[#message + 1] = sen
sen = nil
if url == "" then
local l = file.list();
message[#message + 1] = "<table border=1 cellpadding=3><tr><th>Name</th><th>Size</th><th>Edit</th><th>Compile</th><th>Delete</th></tr>"
for k,v in pairs(l) do
local line = "<tr><td><a href='" ..k.. "'>" ..k.. "</a></td><td>" ..v.. "</td><td>"
local editable = k:sub(-4, -1) == ".lua" or k:sub(-4, -1) == ".css" or k:sub(-5, -1) == ".html" or k:sub(-5, -1) == ".json"
if editable then
line = line .. "<a href='" ..k.. "?edit'>edit</a>"
end
line = line .. "</td><td>"
if k:sub(-4, -1) == ".lua" then
line = line .. "<a href='" ..k.. "?compile'>compile</a>"
end
line = line .. "</td><td><a href='" ..k.. "?delete'>delete</a></td></tr>"
message[#message + 1] = line
end
message[#message + 1] = "</table><a href='#' onclick='v=prompt(\"Filename\");if (v!=null) { this.href=\"/\"+v+\"?edit\"; return true;} else return false;'>Create new</a> &nbsp; &nbsp; "
message[#message + 1] = "<a href='#' onclick='var x=new XMLHttpRequest();x.open(\"GET\",\"/?restart\");x.send();setTimeout(function(){location.href=\"/\"},5000);this.innerText=\"Please wait\";return false'>Restart</a>"
end
message[#message + 1] = "</body></html>"
local function send_table(sk)
if #message > 0 then
sk:send(table.remove(message, 1))
else
sk:close()
message = nil
end
end
sck:on("sent", send_table)
send_table(sck)
end)
conn:on("sent", function(sck)
if DataToGet >= 0 and method == "GET" then
if file.open(url, "r") then
file.seek("set", DataToGet)
local chunkSize = 512
local line = file.read(chunkSize)
file.close()
if line then
sck:send(line)
DataToGet = DataToGet + chunkSize
if string.len(line) == chunkSize then return end
end
end
end
sck:close()
sck = nil
end)
end)
end
--[[if wifi.sta.status() == wifi.STA_GOTIP then
print("http://"..wifi.sta.getip().."/")
editor()
else
print("WiFi connecting...")
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function()
wifi.eventmon.unregister(wifi.eventmon.STA_GOT_IP)
print("http://"..wifi.sta.getip().."/")
editor()
end)
end
]]
editor()

View File

@@ -0,0 +1,158 @@
-- Petit WEB IDE tout simple à tester
print("\n ide2.lua zf181119.1505 \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(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("<html><body><h1>NodeMCU IDE</h1>")
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("<a href='/'>Back to file list</a><br><br><textarea name=t cols=79 rows=17></textarea></br>")
conn:send("<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>")
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,18 @@
--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 30 secondes avant de continuer
print("\n init.lua zf181119.1408 \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
initalarme=tmr.create()
tmr.alarm(initalarme, 300*1000, tmr.ALARM_SINGLE, function()
if file.exists("boot.lua") then dofile("boot.lua") end
end)
else
if file.exists("boot.lua") then dofile("boot.lua") end
end

View File

@@ -0,0 +1,15 @@
-- Scripts de seconde chance pour réparer une boucle dans le restart
print("\n repair.lua zf181119.1511 \n")
--if file.exists("wifi_ap_start.lua") then dofile("wifi_ap_start.lua") end
if file.exists("wifi_ap_stop.lua") then dofile("wifi_ap_stop.lua") end
if file.exists("wifi_cli_conf.lua") then dofile("wifi_cli_conf.lua") end
if file.exists("wifi_cli_start.lua") then dofile("wifi_cli_start.lua") end
if file.exists("ide2.lua") then dofile("ide2.lua") end
if file.exists("telnet_srv.lua") then dofile("telnet_srv.lua") end
jobtimer1=tmr.create()
tmr.alarm(jobtimer1, 5*1000, tmr.ALARM_AUTO, function()
-- print("repair...")
end)

View File

@@ -0,0 +1,21 @@
-- Scripts à charger après le boot pour démarrer son appli
print("\n boot.lua zf181119.2351 \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= "dsleep.lua" if file.exists(f) then dofile(f) end
x_dsleep=7 y_dsleep=5 dsleep()
jobtimer1=tmr.create()
tmr.alarm(jobtimer1, 1*1000, tmr.ALARM_AUTO, function()
print("coucou...")
if wifi.sta.getip() ~= nil then
tmr.stop(jobtimer1)
tmr.stop(ztmr_SLEEP)
x_dsleep=12 y_dsleep=5 dsleep()
print("c'est connecté...")
end
end)

View File

@@ -0,0 +1,14 @@
-- Teste le deep sleep !
-- 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.2343 \n")
ztmr_SLEEP = tmr.create()
function dsleep()
tmr.alarm(ztmr_SLEEP, x_dsleep*1000, tmr.ALARM_SINGLE, function ()
print("Je dors...")
node.dsleep(y_dsleep*1000*1000)
end)
end

View File

@@ -0,0 +1,18 @@
--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 30 secondes avant de continuer
print("\n init.lua zf181119.2314 \n")
_, reset_reason = node.bootreason()
print("reset_reason:",reset_reason)
if reset_reason == 0 or reset_reason == 6 then
print("seconde chance...")
f= "repair.lua" if file.exists(f) then dofile(f) end
initalarme=tmr.create()
tmr.alarm(initalarme, 300*1000, tmr.ALARM_SINGLE, function()
f= "boot.lua" if file.exists(f) then dofile(f) end
end)
else
f= "boot.lua" if file.exists(f) then dofile(f) end
end

View File

@@ -0,0 +1,10 @@
-- Démarre le WIFI en mode AP
print("\n wifi_ap_stop.lua zf180824.2000 \n")
local zmodewifi=wifi.getmode()
if zmodewifi == wifi.SOFTAP then
wifi.setmode(wifi.NULLMODE)
elseif zmodewifi == wifi.STATIONAP then
wifi.setmode(wifi.STATION)
end
print("WIFI AP arrêté")

View File

@@ -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}

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 zf180824.2000 \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()
dofile("wifi_get_ip.lua")

View File

@@ -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é")

View File

@@ -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)

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