From 287e6154bb5802d3f974e11d0aa90e790818190c Mon Sep 17 00:00:00 2001 From: Christian Zufferey Date: Fri, 5 Oct 2018 23:19:19 +0200 Subject: [PATCH] wip --- Workshop/181004/btn_led_remote/btn_led.lua | 21 ++++++++ Workshop/181004/btn_led_remote/web_cli.lua | 14 +++++ .../181004/btn_led_remote/web_led_onoff.lua | 51 +++++++++++++++++++ .../181004/btn_led_remote/wifi_cli_conf.lua | 4 ++ .../181004/btn_led_remote/wifi_cli_start.lua | 14 +++++ Workshop/181004/web_led_onoff.lua | 51 +++++++++++++++++++ Workshop/181004/wifi_cli_conf.lua | 2 +- 7 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 Workshop/181004/btn_led_remote/btn_led.lua create mode 100644 Workshop/181004/btn_led_remote/web_cli.lua create mode 100644 Workshop/181004/btn_led_remote/web_led_onoff.lua create mode 100644 Workshop/181004/btn_led_remote/wifi_cli_conf.lua create mode 100644 Workshop/181004/btn_led_remote/wifi_cli_start.lua create mode 100644 Workshop/181004/web_led_onoff.lua diff --git a/Workshop/181004/btn_led_remote/btn_led.lua b/Workshop/181004/btn_led_remote/btn_led.lua new file mode 100644 index 0000000..fcd333d --- /dev/null +++ b/Workshop/181004/btn_led_remote/btn_led.lua @@ -0,0 +1,21 @@ +-- Programme qui allume la led bleue quand on appuie le bouton flash +-- hv180711.1125 + +zledbleue=0 --led bleue +zswitch=3--switch flash + +gpio.mode(zswitch, gpio.INT, gpio.PULLUP) + +function bouton() + if gpio.read(zswitch)==0 then + zstate="OFF" + disp_send() + --gpio.write(zledbleue, gpio.LOW) + else + zstate="ON" + disp_send() + --gpio.write(zledbleue, gpio.HIGH) + end +end + +gpio.trig(zswitch, "both", bouton) diff --git a/Workshop/181004/btn_led_remote/web_cli.lua b/Workshop/181004/btn_led_remote/web_cli.lua new file mode 100644 index 0000000..e28d40d --- /dev/null +++ b/Workshop/181004/btn_led_remote/web_cli.lua @@ -0,0 +1,14 @@ +-- Petit script pour envoyer quelque chose sur un serveur WEB +print("\n web_cli.lua zf180828.1849 \n") + +function disp_send() +-- http.get("http://192.168.4.1/?line1="..zlength.."m", nil, function(code, data) +print(zstate) +http.get("http://192.168.0.177/?pin="..zstate, nil, function(code, data) + if (code < 0) then +-- print("HTTP request failed") + else +-- print(code, data) + end + end) +end diff --git a/Workshop/181004/btn_led_remote/web_led_onoff.lua b/Workshop/181004/btn_led_remote/web_led_onoff.lua new file mode 100644 index 0000000..7ff2709 --- /dev/null +++ b/Workshop/181004/btn_led_remote/web_led_onoff.lua @@ -0,0 +1,51 @@ +--Petit serveur WEB pour allumer/éteindre une LED en mode client WIFI +--hv20180711.1606 + +print("Démarrage") +--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/181004/btn_led_remote/wifi_cli_conf.lua b/Workshop/181004/btn_led_remote/wifi_cli_conf.lua new file mode 100644 index 0000000..13f5198 --- /dev/null +++ b/Workshop/181004/btn_led_remote/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-zf", pwd="12234567", save=true} diff --git a/Workshop/181004/btn_led_remote/wifi_cli_start.lua b/Workshop/181004/btn_led_remote/wifi_cli_start.lua new file mode 100644 index 0000000..aac8bea --- /dev/null +++ b/Workshop/181004/btn_led_remote/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/181004/web_led_onoff.lua b/Workshop/181004/web_led_onoff.lua new file mode 100644 index 0000000..7ff2709 --- /dev/null +++ b/Workshop/181004/web_led_onoff.lua @@ -0,0 +1,51 @@ +--Petit serveur WEB pour allumer/éteindre une LED en mode client WIFI +--hv20180711.1606 + +print("Démarrage") +--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/181004/wifi_cli_conf.lua b/Workshop/181004/wifi_cli_conf.lua index d3cd11e..13f5198 100644 --- a/Workshop/181004/wifi_cli_conf.lua +++ b/Workshop/181004/wifi_cli_conf.lua @@ -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="Hugo", pwd="tototutu", save=true} +wifi.sta.config{ssid="3G-zf", pwd="12234567", save=true}