This commit is contained in:
Christian Zufferey
2018-12-16 00:37:08 +01:00
parent 126ee5c082
commit 5a437287e8
14 changed files with 578 additions and 62 deletions

View File

@@ -2,15 +2,17 @@
-- tout sur la couleur: https://www.w3schools.com/colors/default.asp
-- roue des couleurs: https://iro.js.org/?ref=oldsite
print("\n a_guirlande.lua zf181215.2027 \n")
print("\n a_guirlande.lua zf181216.0020 \n")
znbleds_max=300
znbleds=300
zstep=5
zpower=0.5
zstep=1
zpower=1
function RGB_clear()
ws2812.init()
buffer = ws2812.newBuffer(znbleds, 3)
buffer = ws2812.newBuffer(znbleds_max, 3)
buffer_reg = ws2812.newBuffer(znbleds_max, 3)
buffer:fill(0, 0, 0)
ws2812.write(buffer)
end
@@ -31,23 +33,13 @@ function zfaderando()
end
end
function zlog(l)
a=2
l=0.3+l^a/310^a
return l
end
print(zlog(0))
print(zlog(128))
print(zlog(255))
function zrando()
a=10
zr=math.random(a)+5
if math.random()<0.5 then
zr=zr*-1
end
print(zr)
-- print(zr)
return zr
end
@@ -58,19 +50,6 @@ function zincfill()
end
end
function zledfill()
for i=1, znbleds, zstep do
a=30
l=math.random(0,255/a)*a
-- l=i*(255/znbleds)
k=zlog(l)
-- print(i,l,l*k,k)
l=l*k
l=255
buffer:set(i, l*zpower, l*zpower, l*zpower)
end
end
function zeffect_stop()
tmr.unregister(effect_tmr1)
RGB_clear()
@@ -78,24 +57,39 @@ end
function zeffect_start()
effect_tmr1=tmr.create()
tmr.alarm(effect_tmr1, 300, tmr.ALARM_AUTO, function()
tmr.alarm(effect_tmr1, 200, tmr.ALARM_AUTO, function()
zfaderando()
ws2812.write(buffer)
zpower_reg()
ws2812.write(buffer_reg)
end)
end
function zpower_reg()
psu_current_ma = 1000
led_current_ma = 35
led_sum_current_ma = (znbleds/zstep) * led_current_ma
-- print(znbleds, zstep, led_sum_current_ma)
led_sum = (znbleds/zstep) * 255 * 3
-- print(led_sum)
led_sum=led_sum*psu_current_ma/led_sum_current_ma
-- print(led_sum)
p = buffer:power()
-- print(p,led_sum)
if p > led_sum then
buffer_reg:mix(256 * led_sum / p, buffer) -- power is now limited
else
buffer_reg:mix(255, buffer)
end
end
RGB_clear()
zledfill()
ws2812.write(buffer)
zincfill()
zeffect_start()

View File

@@ -1,33 +1,27 @@
-- Scripts à charger après le boot pour démarrer son appli
print("\n boot.lua zf181125.1635 \n")
print("\n boot.lua zf181216.0028 \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 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= "a_test_train.lua" if file.exists(f) then dofile(f) end
--f= "a_test_fill.lua" if file.exists(f) then dofile(f) 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_srv2.lua" if file.exists(f) then dofile(f) end
f= "web_ide2.lua" if file.exists(f) then dofile(f) end
--f= "az_init_led.lua" if file.exists(f) then dofile(f) end
--f= "webleds.lua" if file.exists(f) then dofile(f) end
f= "a_guirlande.lua" if file.exists(f) then dofile(f) end
heartbeat()
--[[
x_dsleep=7 y_dsleep=30 dsleep()
i=1
jobtimer1=tmr.create()
tmr.alarm(jobtimer1, 1*1000, tmr.ALARM_AUTO, function()
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,9 @@
-- Petit script pour configurer les choses secrètes que l'on n'aimerait
-- pas être exportées sur Internet (github)
-- faut donc le mettre ailleurs que dans le dépôt !
print("\n credentials.lua zf181215.1328 \n")
cli_ssid="3g-s7"
cli_pwd="12234567"

View File

@@ -0,0 +1,32 @@
-- programme pour faire clignoter x fois une LED avec un rapport on/off
print("\n flash_led_xfois.lua zf181204.1955 \n")
zLED=0
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 ()

View File

@@ -0,0 +1,75 @@
<!DOCTYPE html>
<!-- code HTML pour piloter les effets des LED RGB -->
<html>
<head>
<title>LED RGB</title>
<style>
body {
font-family: Bitstream Vera Sans, Verdana, Helvetica, Arial, sans-serif;
font-size: 12px;
background-color: #dddddd;
}
h1 {
font-size: 32px;
}
h2 {
font-size: 24px;
}
/* this matches 'retina' displays */
@media screen and (-webkit-min-device-pixel-ratio: 2) {
body {
font-size: 24px;
}
h1 {
font-size: 64px;
}
h2 {
font-size: 48px;
}
}
</style>
</head>
<body>
<meta charset='utf-8'>
<h1>Contrôle des effets des LED's RGB du chapeau</h1>
<a href="/">Home</a>
<h2>Start & Stop:</h2>
<ul>
<li><a href="/?fonction=start">Start</a></li>
<li><a href="/?fonction=stop">Stop</a></li>
</ul>
<h2>Vitesse de l'effet:</h2>
<ul>
<li><a href="/?set=speed&speed=inc">Plus vite</a></li>
<li><a href="/?set=speed&speed=tresvite">Très vite</a></li>
<li><a href="/?set=speed&speed=dec">Plus lent</a></li>
<li><a href="/?set=speed&speed=treslent">Très lent</a></li>
</ul>
<h2>Couleurs des trains:</h2>
<ul>
<li><a href="/?set=color&R1=255&G1=255&B1=255&R2=255&G2=255&B2=255">Blanc</a></li>
<li><a href="/?set=color&R1=255&G1=0&B1=0&R2=255&G2=0&B2=0">Rouge</a></li>
<li><a href="/?set=color&R1=255&G1=0&B1=0&R2=0&G2=0&B2=255">Brexit</a></li>
<li><a href="/?set=color&R1=0&G1=255&B1=0&R2=0&G2=255&B2=0">Ecolo</a></li>
</ul>
<a href=":88"
onclick="window.open(`//${window.location.hostname}:88/`)">
WEBIDE
</a><br>
<a href="/?restart=1">Restart</a><br><br>
zf181215.1451<br>
</body>
</html>

View File

@@ -0,0 +1,88 @@
-- 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")

View File

@@ -0,0 +1,2 @@
--fichier vide

View File

@@ -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://<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(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("<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("<h2><a href='/'>Back to file list</a>\n")
conn:send("<br><br><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>")
conn:send("</h2><br><textarea name=t cols=110 rows=50></textarea></br>")
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,84 @@
-- Petit script de serveur WEB pour piloter les effets des LED RGB
-- source: https://github.com/nodemcu/nodemcu-firmware/blob/master/lua_examples/webap_toggle_pin.lua
print("\n webleds.lua zf181215.1447 \n")
-- send a file from memory to the client; max. line length = 1024 bytes!
function send_file(client, filename)
if file.open(filename, "r") then
repeat
local line=file.read('\n')
if line then
client:send(line)
end
until not line
file.close()
end
end
-- web server
srv = net.createServer(net.TCP)
srv:listen(80, function(conn)
conn:on("receive", function(client, request)
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP")
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
if (_GET.fonction == "start") then
train_start()
end
if (_GET.fonction == "stop") then
train_stop()
end
if (_GET.set == "speed") then
train_stop()
if (_GET.speed == "inc") then
train_speed=train_speed*0.8
elseif (_GET.speed == "dec") then
train_speed=train_speed*1.2
elseif (_GET.speed == "tresvite") then
train_speed=7
elseif (_GET.speed == "treslent") then
train_speed=77
end
train_start()
end
if (_GET.set == "color") then
print("setting color (RGB)", _GET.R1, _GET.G1, _GET.B1)
train_stop()
R1=_GET.R1 G1=_GET.G1 B1=_GET.B1
R2=_GET.R2 G2=_GET.G2 B2=_GET.B2
train1_fill()
train2_fill()
train_start()
end
if (_GET.restart == "1") then
restarttimer1=tmr.create()
tmr.alarm(restarttimer1, 2*1000, tmr.ALARM_SINGLE, function()
node.restart()
end)
end
print("send html...")
send_file(client, 'index.html')
end
end)
conn:on("sent", function(c) c:close() 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,12 @@
-- Petit script pour configurer le client WIFI du NodeMCU
print("\n wifi_cli_conf.lua zf181205.1931 \n")
--credentials par défaut
cli_ssid="3g-s7"
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}

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