code-lua-iot/dht22.lua
2024-10-22 23:25:38 +02:00

77 lines
1.8 KiB
Lua

-- écrit par Lukian Leizour le 22/10/2024
function print_temp()
s = sensor.attach("DHT22", pio.GPIO18)
tmr.delayms(500)
while true do
print("Temperature : "..s:read("temperature").." Humitité : "..s:read("humidity"))
tmr.delayms(500)
end
end
function humi_neo(min, max)
s = sensor.attach("DHT22", pio.GPIO18)
neo = neopixel.attach(neopixel.WS2812B, pio.GPIO19, 8)
tmr.delayms(500)
pos = 0
while true do
neo:setPixel(pos, 0, 0, 0)
pos = math.floor((s:read("humidity") - min)/(max - min) * 8)
neo:setPixel(pos, 240, 150, 140)
neo:update()
tmr.delayms(500)
end
end
function min(tab)
local min = tab[1]
for i=1,#tab do
if tab[i] < min then min = tab[i] end
end
return min
end
function max(tab)
local max = tab[1]
for i=1,#tab do
if tab[i] > max then max = tab[i] end
end
return max
end
function avg(tab)
local sum = 0
for i=1,#tab do
sum = sum + tab[i]
end
return sum / #tab
end
function temp_humi_avg()
temps = {}
humis = {}
s = sensor.attach("DHT22", pio.GPIO18)
device = pwm.attach(pio.GPIO2, 3, 0.5)
blinking = false
tmr.delayms(500)
while true do
temp = s:read("temperature")
humi = s:read("humidity")
if #temps == 10 then
for i=1,9 do
temps[i] = temps[i + 1]
humis[i] = humis[i + 1]
end
temps[10] = temp
humis[10] = humi
else
temps[#temps + 1] = temp
humis[#humis + 1] = humi
end
print("temp: "..temp..", min temp: "..min(temps)..", max temp: "..max(temps)..", avg temp: "..avg(temps))
print("humi: "..humi..", min humi: "..min(humis)..", max humi: "..max(humis)..", avg humi: "..avg(humis).."\n")
if humi > avg(humis) and not blinking then device:start(); blinking = true
elseif humi <= avg(humis) and blinking then device:stop(); blinking = false end
tmr.delayms(10000)
end
end