- refactorisé le script flash_led_xfois

- ajouté l'exemple web_srv pour le workshop
This commit is contained in:
Christian Zufferey
2018-10-18 14:32:51 +02:00
parent 5776cfbdfd
commit 3d2e7363ec
20 changed files with 454 additions and 52 deletions

View File

@@ -1,33 +1,32 @@
-- programme pour faire clignoter x fois une LED avec un rapport on/off
print("\n flash_led_xfois.lua zf181015.1641 \n")
print("\n flash_led_xfois.lua zf181018.1428 \n")
zLED=0
zTm_On_LED = 100 --> en ms
zTm_On_LED = 50 --> en ms
zTm_Off_LED = 100 --> en ms
zFlag_LED = 0
zxfois = 0
zxfois_max =2
function blink_LED ()
if zxfois > zxfois_max-1 then
print(zxfois)
tmr.stop(ztmr_LED)
zFlag_LED=gpio.HIGH
else
if zFlag_LED==gpio.LOW then
zFlag_LED=gpio.HIGH
tmr.alarm(ztmr_LED, zTm_Off_LED, tmr.ALARM_SINGLE, blink_LED)
else
zFlag_LED=gpio.LOW
tmr.alarm(ztmr_LED, zTm_On_LED, tmr.ALARM_SINGLE, blink_LED)
zxfois = zxfois+1
end
end
gpio.write(zLED, zFlag_LED)
end
nbfois = 0
gpio.write(zLED, gpio.HIGH)
gpio.mode(zLED, gpio.OUTPUT)
ztmr_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_LED, zTm_Off_LED, tmr.ALARM_SINGLE, blink_LED)
else
gpio.write(zLED, gpio.HIGH)
nbfois = nbfois+1
tmr.alarm(ztmr_LED, zTm_On_LED, tmr.ALARM_SINGLE, blink_LED)
end
end
end
xfois =2
blink_LED ()

View File

@@ -1,33 +1,32 @@
-- programme pour faire clignoter x fois une LED avec un rapport on/off
print("\n flash_led_xfois.lua zf181015.1641 \n")
print("\n flash_led_xfois.lua zf181018.1428 \n")
zLED=0
zTm_On_LED = 100 --> en ms
zTm_On_LED = 50 --> en ms
zTm_Off_LED = 100 --> en ms
zFlag_LED = 0
zxfois = 0
zxfois_max =2
function blink_LED ()
if zxfois > zxfois_max-1 then
print(zxfois)
tmr.stop(ztmr_LED)
zFlag_LED=gpio.HIGH
else
if zFlag_LED==gpio.LOW then
zFlag_LED=gpio.HIGH
tmr.alarm(ztmr_LED, zTm_Off_LED, tmr.ALARM_SINGLE, blink_LED)
else
zFlag_LED=gpio.LOW
tmr.alarm(ztmr_LED, zTm_On_LED, tmr.ALARM_SINGLE, blink_LED)
zxfois = zxfois+1
end
end
gpio.write(zLED, zFlag_LED)
end
nbfois = 0
gpio.write(zLED, gpio.HIGH)
gpio.mode(zLED, gpio.OUTPUT)
ztmr_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_LED, zTm_Off_LED, tmr.ALARM_SINGLE, blink_LED)
else
gpio.write(zLED, gpio.HIGH)
nbfois = nbfois+1
tmr.alarm(ztmr_LED, zTm_On_LED, tmr.ALARM_SINGLE, blink_LED)
end
end
end
xfois =2
blink_LED ()

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 zf181018.1428 \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_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_LED, zTm_Off_LED, tmr.ALARM_SINGLE, blink_LED)
else
gpio.write(zLED, gpio.HIGH)
nbfois = nbfois+1
tmr.alarm(ztmr_LED, zTm_On_LED, tmr.ALARM_SINGLE, blink_LED)
end
end
end
xfois =2
blink_LED ()

View File

@@ -0,0 +1,13 @@
-- Exemple de programme à ne PAS faire sur NodeMCU Lua script
-- programme pour faire clignoter une LED avec un rapport on/off
--zf20181004.1430
zLED=0
gpio.mode(zLED, gpio.OUTPUT)
while true do
gpio.write(zLED, 0)
tmr.delay(1000*500)
gpio.write(zLED, 1)
tmr.delay(1000*500)
end

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 zf181018.1428 \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_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_LED, zTm_Off_LED, tmr.ALARM_SINGLE, blink_LED)
else
gpio.write(zLED, gpio.HIGH)
nbfois = nbfois+1
tmr.alarm(ztmr_LED, zTm_On_LED, tmr.ALARM_SINGLE, blink_LED)
end
end
end
xfois =2
blink_LED ()

View File

@@ -0,0 +1,43 @@
-- a simple telnet server
print("\ntelnet_srv.lua zf180906.0904 \n")
-- restart server if needed
if telnet_srv ~= nil then
telnet_srv:close()
end
telnet_srv = net.createServer(net.TCP, 180)
telnet_srv:listen(23, function(socket)
local fifo = {}
local fifo_drained = true
local function sender(c)
if #fifo > 0 then
c:send(table.remove(fifo, 1))
else
fifo_drained = true
end
end
local function s_output(str)
table.insert(fifo, str)
if socket ~= nil and fifo_drained then
fifo_drained = false
sender(socket)
end
end
node.output(s_output, 0) -- re-direct output to function s_ouput.
socket:on("receive", function(c, l)
node.input(l) -- works like pcall(loadstring(l)) but support multiple separate line
end)
socket:on("disconnection", function(c)
node.output(nil) -- un-regist the redirect output function, output goes to serial
end)
socket:on("sent", sender)
print("Welcome to NodeMCU world.")
end)
print("Telnet server running...\nUsage: telnet -r ip\n")

View File

@@ -0,0 +1,51 @@
--Petit serveur WEB pour allumer/éteindre une LED en mode client WIFI
print("\n web_led_onoff.lua zf181018.1031 \n")
--wifi.sta.disconnect()
--wifi.setmode(wifi.STATION)
--print("set mode=STATION (mode="..wifi.getmode()..")")
--wifi.sta.config{ssid="Hugo", pwd="tototutu"}
--[[wifi.sta.connect()
tmr.alarm(0, 1000, tmr.ALARM_AUTO , function()
if wifi.sta.getip() == nil then
print("Connecting to AP...")
else
print("Connected! IP: ",wifi.sta.getip())
tmr.stop(0)
end
end)
]]
zLED=0
gpio.mode(zLED, gpio.OUTPUT)
gpio.write(zLED, gpio.HIGH)
srv = net.createServer(net.TCP)
srv:listen(80, function(conn)
conn:on("receive", function(client, request)
local buf = ""
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP")
if (method == nil) then
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP")
end
local _GET = {}
if (vars ~= nil) then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
end
end
buf = buf .. "<!DOCTYPE html><html><body><h1>Hello, this is NodeMCU.</h1><form src=\"/\">Turn PIN <select name=\"pin\" onchange=\"form.submit()\">"
local _on, _off = "", ""
if (_GET.pin == "ON") then
_on = " selected=\"true\""
gpio.write(zLED, gpio.LOW)
elseif (_GET.pin == "OFF") then
_off = " selected=\"true\""
gpio.write(zLED, gpio.HIGH)
end
buf = buf .. "<option" .. _off .. ">OFF</option><option" .. _on .. ">ON</option></select></form></body></html>"
client:send(buf)
end)
conn:on("sent", function(c) c:close() end)
end)

View File

@@ -0,0 +1,15 @@
-- Petit script pour démarrer le mode configuration WIFI du NodeMCU
print("\n wifi_cnf_start.lua zf180906.1610 \n")
print("\nwifi config http://192.168.4.1\n")
--dofile("wifi_ap_stop.lua")
--dofile("wifi_cli_stop.lua")
srv:close()
telnet_srv:close()
--wificnftimer1=tmr.create()
--tmr.alarm(wificnftimer1, 3000, tmr.ALARM_SINGLE, function()
print("coucou")
enduser_setup.start()
--end)

View File

@@ -0,0 +1,23 @@
--Script de bootstrap, en appuyant sur le bouton ça démarre start_boot,
-- autrement en attendant 8 secondes cela démarre start_boot
print("\n init.lua zf181017.1026\n")
zswitch=3 --switch flash
gpio.mode(zswitch, gpio.INT, gpio.PULLUP)
initalarme=tmr.create()
function hvbouton()
gpio.trig(zswitch, "none")
tmr.unregister(initalarme)
dofile("start_boot.lua")
-- dofile("start_job.lua")
end
gpio.trig(zswitch, "both", hvbouton)
tmr.alarm(initalarme, 8000, tmr.ALARM_SINGLE, function()
print("\nStart\n")
dofile("start_boot.lua")
-- dofile("start_job.lua")
end)

View File

@@ -0,0 +1,32 @@
--Petit script pour la gestion de la LED, juste pour comprendre la prog ;-)
print("\n led_job.lua zf181018.1104 \n")
zLED=0
gpio.mode(zLED, gpio.OUTPUT)
gpio.write(zLED, gpio.HIGH)
function led_on()
gpio.write(zLED, gpio.LOW)
fled="ON"
end
function led_off()
gpio.write(zLED, gpio.HIGH)
fled="ON"
end
led_on()
led_off()
local _on, _off = "", ""
if (_GET.pin == "ON") then
_on = " selected=\"true\""
gpio.write(zLED, gpio.LOW)
elseif (_GET.pin == "OFF") then
_off = " selected=\"true\""
gpio.write(zLED, gpio.HIGH)
end
buf = buf .. "<option" .. _off .. ">OFF</option><option" .. _on .. ">ON</option></select></form></body></html>"

View File

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

View File

@@ -0,0 +1,16 @@
-- Scripts à charger au moment du boot
print("\n start_boot.lua zf181018.1116 \n")
--dofile("wifi_ap_stop.lua")
--dofile("wifi_cli_conf.lua")
dofile("wifi_cli_start.lua")
dofile("led_job.lua")
dofile("web_srv.lua")
--dofile("flash_led_xfois.lua")

View File

@@ -0,0 +1,12 @@
-- petit script pour la gestion du GET du serveur web
print("\n web_get.lua zf181018.1142 \n")
function web_get()
if (_GET.led == "on") then
led_on()
elseif (_GET.led == "off") then
led_off()
end
end

View File

@@ -0,0 +1,16 @@
-- petit script pour le HTML du serveur web
print("\n web_html.lua zf181018.1142 \n")
--Partie HTML et CSS pour la page web
function web_html()
buf = "<!DOCTYPE html><html><body><meta charset='utf-8' name='viewport' content='width=device-width, initial-scale=1.0'>\n"
buf = buf .. "<h1>Hello, this is NodeMCU. 1142 </h1>\n"
buf = buf .. "Usage: <br><br>\n"
buf = buf .. "pour allumer la LED, http://xxx/?led=on<br>\n"
buf = buf .. "pour éteindre la LED, http://xxx/?led=off<br>\n"
buf = buf .. "</body></html>"
end

View File

@@ -0,0 +1,39 @@
-- petit script de serveur WEB Wifi
print("\n web_srv.lua zf181018.1031 \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()
web_html()
--print("send html...")
client:send(buf)
buf=nil
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,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,12 @@
-- Petit script pour obtenir l'adresse IP du NodeMCU connecté sur un AP Wifi
print("\n wifi_get_ip.lua zf180824.2000 \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)
dofile("wifi_info.lua")
end
end)

View File

@@ -0,0 +1,28 @@
-- Petit script pour afficher les infos actuel du WIFI
print("\n wifi_info.lua zf180824.2000 \n")
local zmodewifi=wifi.getmode()
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\t"..wifi.ap.getmac())
print("AP IP:\n\t"..wifi.ap.getip())
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