-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy paththermalmonitor.py
50 lines (41 loc) · 1.24 KB
/
thermalmonitor.py
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
#
#
#
import os
import time
import gpiozero
import db
import threading
from datetime import datetime
from consts import FAN_GPIO
from consts import TEMP_POLL_INTERVAL
from consts import TEMP_UPPER_LIMIT
from consts import TEMP_COOLDOWN
fanRelay = gpiozero.OutputDevice(FAN_GPIO, active_high = False,
initial_value = False)
def heartbeat():
entry = { 'timestamp': str(datetime.now().timestamp()),
'event': 'Heartbeat', 'temp': measure_temp()}
db.logFanRun(entry)
threading.Timer(600, heartbeat).start()
def measure_temp():
temp = os.popen('vcgencmd measure_temp').readline()
return temp[5:-3]
heartbeat()
fanState = False
while True:
currTemp = float(measure_temp())
if (currTemp > TEMP_UPPER_LIMIT and fanState is False):
fanRelay.on()
fanState = True
# Log this run
entry = { 'timestamp': str(datetime.now().timestamp()),
'event': 'Switch On', 'temp': str(currTemp)}
db.logFanRun(entry)
elif (currTemp < TEMP_COOLDOWN and fanState is True):
fanRelay.off()
fanState = False
entry = { 'timestamp': str(datetime.now().timestamp()),
'event': 'Switch Off', 'temp': str(currTemp)}
db.logFanRun(entry)
time.sleep(TEMP_POLL_INTERVAL)