diff --git a/Workshop/181015/btn_led_remote/btn/flash_led_xfois.lua b/Workshop/181015/btn_led_remote/btn/flash_led_xfois.lua index a9682d2..fc38772 100644 --- a/Workshop/181015/btn_led_remote/btn/flash_led_xfois.lua +++ b/Workshop/181015/btn_led_remote/btn/flash_led_xfois.lua @@ -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 () + + + diff --git a/Workshop/181015/btn_led_remote/led/flash_led_xfois.lua b/Workshop/181015/btn_led_remote/led/flash_led_xfois.lua index a9682d2..fc38772 100644 --- a/Workshop/181015/btn_led_remote/led/flash_led_xfois.lua +++ b/Workshop/181015/btn_led_remote/led/flash_led_xfois.lua @@ -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 () + + + diff --git a/Workshop/181015/flash_led_xfois.lua b/Workshop/181015/flash_led_xfois.lua new file mode 100644 index 0000000..fc38772 --- /dev/null +++ b/Workshop/181015/flash_led_xfois.lua @@ -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 () + + + diff --git a/Workshop/181015/script1.lua b/Workshop/181015/script1.lua new file mode 100644 index 0000000..6cb3532 --- /dev/null +++ b/Workshop/181015/script1.lua @@ -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 diff --git a/Workshop/181015/web_srv/flash_led_xfois.lua b/Workshop/181015/web_srv/flash_led_xfois.lua new file mode 100644 index 0000000..fc38772 --- /dev/null +++ b/Workshop/181015/web_srv/flash_led_xfois.lua @@ -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 () + + + diff --git a/Workshop/181015/web_srv/goodies/telnet_srv.lua b/Workshop/181015/web_srv/goodies/telnet_srv.lua new file mode 100644 index 0000000..21c8e7c --- /dev/null +++ b/Workshop/181015/web_srv/goodies/telnet_srv.lua @@ -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") diff --git a/Workshop/181015/web_srv/goodies/web_led_onoff.lua b/Workshop/181015/web_srv/goodies/web_led_onoff.lua new file mode 100644 index 0000000..6cf06f6 --- /dev/null +++ b/Workshop/181015/web_srv/goodies/web_led_onoff.lua @@ -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 .. "

Hello, this is NodeMCU.

Turn PIN
" + client:send(buf) + end) + conn:on("sent", function(c) c:close() end) +end) diff --git a/Workshop/181015/web_srv/goodies/wifi_cnf_start.lua b/Workshop/181015/web_srv/goodies/wifi_cnf_start.lua new file mode 100644 index 0000000..975af72 --- /dev/null +++ b/Workshop/181015/web_srv/goodies/wifi_cnf_start.lua @@ -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) diff --git a/Workshop/181015/web_srv/initz.lua b/Workshop/181015/web_srv/initz.lua new file mode 100644 index 0000000..97951ca --- /dev/null +++ b/Workshop/181015/web_srv/initz.lua @@ -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) diff --git a/Workshop/181015/web_srv/led_job.lua b/Workshop/181015/web_srv/led_job.lua new file mode 100644 index 0000000..11b19c3 --- /dev/null +++ b/Workshop/181015/web_srv/led_job.lua @@ -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 .. "OFFON" diff --git a/Workshop/181015/web_srv/rm_files.lua b/Workshop/181015/web_srv/rm_files.lua new file mode 100644 index 0000000..81241cb --- /dev/null +++ b/Workshop/181015/web_srv/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/Workshop/181015/web_srv/start_boot.lua b/Workshop/181015/web_srv/start_boot.lua new file mode 100644 index 0000000..46a4183 --- /dev/null +++ b/Workshop/181015/web_srv/start_boot.lua @@ -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") + + + + diff --git a/Workshop/181015/web_srv/web_get.lua b/Workshop/181015/web_srv/web_get.lua new file mode 100644 index 0000000..926bfd6 --- /dev/null +++ b/Workshop/181015/web_srv/web_get.lua @@ -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 + diff --git a/Workshop/181015/web_srv/web_html.lua b/Workshop/181015/web_srv/web_html.lua new file mode 100644 index 0000000..e27a5ae --- /dev/null +++ b/Workshop/181015/web_srv/web_html.lua @@ -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 = "\n" + buf = buf .. "

Hello, this is NodeMCU. 1142

\n" + + buf = buf .. "Usage:

\n" + buf = buf .. "pour allumer la LED, http://xxx/?led=on
\n" + buf = buf .. "pour éteindre la LED, http://xxx/?led=off
\n" + + buf = buf .. "" + +end diff --git a/Workshop/181015/web_srv/web_srv.lua b/Workshop/181015/web_srv/web_srv.lua new file mode 100644 index 0000000..4843fb5 --- /dev/null +++ b/Workshop/181015/web_srv/web_srv.lua @@ -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) diff --git a/Workshop/181015/web_srv/wifi_ap_stop.lua b/Workshop/181015/web_srv/wifi_ap_stop.lua new file mode 100644 index 0000000..2738627 --- /dev/null +++ b/Workshop/181015/web_srv/wifi_ap_stop.lua @@ -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é") diff --git a/Workshop/181015/web_srv/wifi_cli_conf.lua b/Workshop/181015/web_srv/wifi_cli_conf.lua new file mode 100644 index 0000000..f01a799 --- /dev/null +++ b/Workshop/181015/web_srv/wifi_cli_conf.lua @@ -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} diff --git a/Workshop/181015/web_srv/wifi_cli_start.lua b/Workshop/181015/web_srv/wifi_cli_start.lua new file mode 100644 index 0000000..aac8bea --- /dev/null +++ b/Workshop/181015/web_srv/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 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") diff --git a/Workshop/181015/web_srv/wifi_get_ip.lua b/Workshop/181015/web_srv/wifi_get_ip.lua new file mode 100644 index 0000000..83b6369 --- /dev/null +++ b/Workshop/181015/web_srv/wifi_get_ip.lua @@ -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) diff --git a/Workshop/181015/web_srv/wifi_info.lua b/Workshop/181015/web_srv/wifi_info.lua new file mode 100644 index 0000000..800b8d4 --- /dev/null +++ b/Workshop/181015/web_srv/wifi_info.lua @@ -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