Refactorisé encore le HelloWorld du Workshop

This commit is contained in:
Christian Zufferey
2020-07-18 12:10:38 +02:00
parent 5f864b0d3d
commit 6570ab5287
29 changed files with 34 additions and 440 deletions

View File

@@ -1,8 +1,9 @@
-- 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
print("\n blink_led0_ko.lua zf200718.1204 \n")
zLED=4
gpio.mode(zLED, gpio.OUTPUT)
while true do

View File

@@ -1,8 +1,8 @@
-- programme pour faire clignoter une LED version simplifiée
print("\n blink_led1.lua zf200302.2243 \n")
print("\n blink_led1.lua zf200718.1204 \n")
zLED=0
zLED=4
gpio.mode(zLED, gpio.OUTPUT)
ztmr_LED = tmr.create()
value = true

View File

@@ -1,9 +1,9 @@
-- programme pour faire clignoter une LED avec un rapport on/off
print("\n blink_led2.lua zf200302.2243 \n")
print("\n blink_led2.lua zf200718.1204 \n")
zLED=0
zTm_On_LED = 50 --> en ms
zLED=4
zTm_On_LED = 50 --> en ms
zTm_Off_LED = 500 --> en ms
zFlag_LED = 0

View File

@@ -1,12 +1,11 @@
-- programme pour faire clignoter x fois une LED avec un rapport on/off
print("\n flash_led_xfois.lua zf200302.2316 \n")
print("\n flash_led_xfois.lua zf200717.1748 \n")
zLED=0
zLED=4
zTm_On_LED = 50 --> en ms
zTm_Off_LED = 100 --> en ms
nbfois = 0
gpio.write(zLED, gpio.HIGH)
gpio.mode(zLED, gpio.OUTPUT)
gpio.write(zLED, gpio.HIGH) gpio.mode(zLED, gpio.OUTPUT)
ztmr_LED = tmr.create()
function blink_LED ()
@@ -14,7 +13,7 @@ function blink_LED ()
print(nbfois)
nbfois = 0
else
if gpio.read(zLED)==gpio.HIGH then
if gpio.read(zLED) == gpio.HIGH then
gpio.write(zLED, gpio.LOW)
ztmr_LED:alarm(zTm_Off_LED, tmr.ALARM_SINGLE, blink_LED)
else

View File

@@ -1,23 +1,17 @@
-- programme pour faire clignoter une LED avec un rapport on/off
-- programme pour faire clignoter une LED version simplifiée
print("\n blink_led1.lua zf200302.2243 \n")
zLED=0
zTm_On_LED = 50 --> en ms
zTm_Off_LED = 500 --> en ms
zFlag_LED = 0
function blink_LED ()
if zFlag_LED==gpio.LOW then
zFlag_LED=gpio.HIGH
ztmr_LED:alarm(zTm_Off_LED, tmr.ALARM_SINGLE, blink_LED)
else
zFlag_LED=gpio.LOW
ztmr_LED:alarm(zTm_On_LED, tmr.ALARM_SINGLE, blink_LED)
end
gpio.write(zLED, zFlag_LED)
end
print("\n blink_led1.lua zf200718.1204 \n")
zLED=4
gpio.mode(zLED, gpio.OUTPUT)
ztmr_LED = tmr.create()
blink_LED ()
value = true
ztmr_LED:alarm(100, tmr.ALARM_AUTO, function ()
if value then
gpio.write(zLED, gpio.HIGH)
else
gpio.write(zLED, gpio.LOW)
end
value = not value
end)

View File

@@ -1,7 +1,7 @@
--Script de bootstrap, en appuyant sur le bouton ça démarre start_boot,
-- 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 zf200302.2248\n")
print("\n init.lua zf200717.1625\n")
zswitch=3 --switch flash
gpio.mode(zswitch, gpio.INT, gpio.PULLUP)
@@ -9,9 +9,8 @@ initalarme=tmr.create()
function hvbouton()
gpio.trig(zswitch, "none")
tmr.unregister(initalarme)
initalarme:unregister()
dofile("start_boot.lua")
-- dofile("start_job.lua")
end
gpio.trig(zswitch, "both", hvbouton)
@@ -19,5 +18,4 @@ gpio.trig(zswitch, "both", hvbouton)
initalarme:alarm(8000, tmr.ALARM_SINGLE, function()
print("\nStart\n")
dofile("start_boot.lua")
-- dofile("start_job.lua")
end)

View File

@@ -1,7 +1,12 @@
-- Scripts à charger au moment du boot
print("\n start_boot.lua zf200718.1208 \n")
print("\n start_boot.lua zf181017.1021 \n")
zLED=4 gpio.write(zLED, gpio.HIGH) gpio.mode(zLED,gpio.OUTPUT)
gpio.write(zLED, gpio.LOW) tmr.delay(10000) gpio.write(zLED, gpio.HIGH)
dofile("blink_led1.lua")
gpio.write(zLED, gpio.LOW) tmr.delay(10000) gpio.write(zLED, gpio.HIGH)

View File

@@ -1,23 +0,0 @@
-- Programme qui allume la led bleue quand on appuie le bouton flash
-- zf181011.1749
print("\n btn_led.lua zf181016.1957 \n")
zledbleue=0 --led bleue
zswitch=3 --switch flash
gpio.mode(zswitch, gpio.INT, gpio.PULLUP)
function zbtn()
if gpio.read(zswitch)==0 then
zled_state="ON"
gpio.write(zledbleue, gpio.LOW)
else
zled_state="OFF"
gpio.write(zledbleue, gpio.HIGH)
end
print(zled_state)
end
gpio.trig(zswitch, "both", zbtn)

View File

@@ -1,17 +0,0 @@
-- fonction cat() pour afficher le contenu d'un fichier dans la flash
print("\n cat.lua zf180826.1109 \n")
function cat(zfile)
print("\n"..zfile.."\n-------------------------------")
zfilei = file.open(zfile, "r")
i=1
zline=file.readline()
repeat
print(i..": "..string.sub(zline,1,string.len(zline)-1))
i=i+1 zline=file.readline()
until zline== nil
file.close(zfilei)
print("-------------------------------")
end

View File

@@ -1,15 +0,0 @@
-- fonction dir() pour afficher les fichiers dans la flash
print("\n dir.lua zf180826.1019 \n")
function dir()
print("\n-------------------------------")
l=file.list() i=0
for k,v in pairs(l) do
i=i+v
print(k..string.rep(" ",19-string.len(k)).." : "..v.." bytes")
end
print("-------------------------------")
print('\nUsed: '..i..' bytes\nusage: dofile("file.lua")\n')
end
dir()

View File

@@ -1,32 +0,0 @@
-- 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

@@ -1,23 +0,0 @@
--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

@@ -1,12 +0,0 @@
-- 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

@@ -1,7 +0,0 @@
-- Scripts à charger au moment du boot
print("\n start_boot.lua zf181017.1021 \n")
dofile("blink_led1.lua")

View File

@@ -1,55 +0,0 @@
print("\n start_job.lua hv180907.1558 \n")
jobtimer1=tmr.create()
jobtimer2=tmr.create()
oled_line1="Job Start..."
oled_line2=""
oled_line3=""
oled_line4=""
oled_line5=""
disp_oled()
oled_line1="AUTO..."
oled_line2=""
oled_line3=""
oled_line4=""
oled_line5=""
disp_oled()
zpeed=50
turn_on = 700
function return_mesure()
print(zlength)
print("RAM: "..node.heap())
if zauto then
if zlength < 0.20 then
backward()
tmr.alarm(jobtimer2, 200, tmr.ALARM_SINGLE, function()
if math.random(1,2) == 1 then
right()
else
left()
end
tmr.alarm(jobtimer2, turn_on, tmr.ALARM_SINGLE, forward)
end)
end
tmr.alarm(jobtimer1, 300, tmr.ALARM_SINGLE, start_mesure)
else
if zmeter then
oled_line1=zlength.." m"
oled_line2=""
oled_line3=""
oled_line4="NodeMCU: "..wifi.ap.getmac()
disp_oled()
end
end
end
dofile("detector.lua")
zauto=true
tmr.alarm(jobtimer1, 300, tmr.ALARM_SINGLE, start_mesure)
forward()

View File

@@ -1,43 +0,0 @@
-- 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

@@ -1,4 +0,0 @@
a=3
b=4
c=a*b
print(c)

View File

@@ -1,51 +0,0 @@
--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 .. "<!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" .. _on .. ">ON</option><option" .. _off .. ">OFF</option></select></form></body></html>"
client:send(buf)
end)
conn:on("sent", function(c) c:close() end)
end)

View File

@@ -1,13 +0,0 @@
-- Démarre le WIFI en mode AP
print("\n wifi_ap_start.lua zf180824.2000 \n")
local zmodewifi=wifi.getmode()
if zmodewifi == wifi.NULLMODE then
print("WIFI mode AP only")
wifi.setmode(wifi.SOFTAP)
elseif zmodewifi == wifi.STATION then
print("WIFI mode AP+CLI")
wifi.setmode(wifi.STATIONAP)
end
wifi.ap.config({ ssid = "NodeMCU "..wifi.ap.getmac(), pwd = "12345678" })
dofile("wifi_info.lua")

View File

@@ -1,10 +0,0 @@
-- 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

@@ -1,4 +0,0 @@
-- 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="xxx", save=true}

View File

@@ -1,14 +0,0 @@
-- 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

@@ -1,10 +0,0 @@
-- Démarre le WIFI en mode AP
print("\n wifi_cli_stop.lua zf180824.2000 \n")
local zmodewifi=wifi.getmode()
if zmodewifi == wifi.STATION then
wifi.setmode(wifi.NULLMODE)
elseif zmodewifi == wifi.STATIONAP then
wifi.setmode(wifi.SOFTAP)
end
print("WIFI CLI arrêté")

View File

@@ -1,15 +0,0 @@
-- 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

@@ -1,6 +0,0 @@
-- Petit script pour arrêter le mode configuration WIFI du NodeMCU
print("\n wifi_cnf_stop.lua zf180824.2000 \n")
enduser_setup.stop()
wifi.sta.autoconnect(1)
wifi.sta.connect()-- Petit script pour arrêter le mode configuration WIFI du NodeMCU

View File

@@ -1,12 +0,0 @@
-- 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

@@ -1,28 +0,0 @@
-- 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

View File

@@ -1,4 +0,0 @@
-- Déconnecte le WIFI
print("\n wifi_off.lua zf180822.0959 \n")
wifi.setmode(wifi.NULLMODE)

View File

@@ -1,5 +0,0 @@
-- Exemple de petit script pour démarrer le WIFI
print("\n wifi_start.lua zf180824.2000 \n")
dofile("wifi_cli_start.lua")
dofile("wifi_ap_start.lua")