code-lua-iot/dht22.lua
2024-10-23 22:46:16 +02:00

53 lines
1.4 KiB
Lua

-- écrit par Lukian Leizour le 22/10/2024x
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 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