This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Humidity.lua
77 lines (72 loc) · 1.67 KB
/
Humidity.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function initGpio()
gpio.mode(dhtpin, gpio.INPUT)
gpio.mode(fanpin, gpio.OUTPUT)
gpio.write(fanpin, gpio.LOW)
gpio.mode(relaypin, gpio.OUTPUT)
gpio.write(relaypin, gpio.LOW)
end
function setLight(state)
if (state == 1) then
gpio.write(relaypin, gpio.HIGH)
else
gpio.write(relaypin, gpio.LOW)
end
end
--dht is humidtySensor (use float firmware)
function readDht()
status, temp, humi, temp_dec, humi_dec = dht.read(dhtpin)
dhtstate = status
if (status == dht.OK) then
sensorState = "ready"
dhttemp = temp
dhthum = humi
--print("read dht:" .. humi)
elseif (status == dht.ERROR_CHECKSUM) then
sensorState = "checksum error"
print("DHT Checksum error.")
elseif (status == dht.ERROR_TIMEOUT) then
sensorState = "timeout"
print("DHT timed out.")
end
end
function setFan(state)
if (state == 1) then
local tempTime = rtctime.get()
print("calc" .. fanLastRun + fanTimeout)
print("tempTime:" .. tempTime)
if (fanLastRun + fanTimeout < tempTime or fanLastRun == 0) then
fanLastRun = tempTime
fanState = 1
gpio.write(fanpin, gpio.HIGH)
gpio.write(relaypin, gpio.HIGH)
print("start fan")
return true
else
print("cannot start fan - wait")
return false
end
else
fanState = 0
gpio.write(fanpin, gpio.LOW)
gpio.write(relaypin, gpio.LOW)
print("stop fan")
return true
end
end
function logic()
if (sensorState ~= "ready") then
print("logic disabled sensor not ready")
return
end
if (dhthum > logichumtreshold) then
if (fanState == 0) then
print("hum high, start fan")
setFan(1)
end
elseif (dhthum <= logichumtreshold-logichumhysteresis) then
if (fanState == 1) then
print("hum ok, stop fan")
setFan(0)
end
end
end