commit
This commit is contained in:
parent
84036b5d43
commit
068d3a6f70
1 changed files with 55 additions and 0 deletions
55
dht22.lua
55
dht22.lua
|
@ -1,3 +1,5 @@
|
|||
-- écrit par Lukian Leizour le 22/10/2024
|
||||
|
||||
function print_temp()
|
||||
s = sensor.attach("DHT22", pio.GPIO18)
|
||||
tmr.delayms(500)
|
||||
|
@ -20,3 +22,56 @@ function humi_neo(min, max)
|
|||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue