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 .. "