- super bien avancé avec mon init.lua mini.

- maintenant il s'endort pendant 30 secondes, se réveille, regarde s'il peut se connecter sur un wifi, si oui, démarre le web ide, autrement se rendort pour 30 secondes
- aussi, le tout mini web ide fonctionne, c'est à dire que je peux modifier depuis la connexion wifi le code lua sur le NodeMCU, sans devoir brancher le NodeMCU sur le port USB
- une sacrée avancée que voici pour mon WPS/GPS que je suis en train d'écrire :-)
This commit is contained in:
Christian Zufferey
2018-11-20 07:31:14 +01:00
parent d70ecc6173
commit c4aaeb3e82
20 changed files with 285 additions and 29 deletions

View File

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

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

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

View File

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

View File

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

View File

@@ -0,0 +1,5 @@
--Redémarre le NodeMCU
print("\n restart.lua zf181120.002 \n")
node.restart()

View File

@@ -1,4 +0,0 @@
--test si un fichier existe
--zf181118.2237
if file.exists("boot.lua") then dofile("boot.lua") end

View File

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

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

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

5
OS_utils/restart.lua Normal file
View File

@@ -0,0 +1,5 @@
--Redémarre le NodeMCU
print("\n restart.lua zf181120.002 \n")
node.restart()

5
OS_utils/test_dofile.lua Normal file
View File

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

View File

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

View File

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

View File

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

View File

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