forked from Howchoo/pi-fan-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfancontrol
executable file
·82 lines (60 loc) · 2.49 KB
/
fancontrol
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
78
79
80
81
82
#!/usr/bin/env python3
import logging
import sys
import subprocess
import time
from gpiozero import OutputDevice
from signal import signal, SIGABRT, SIGILL, SIGINT, SIGSEGV, SIGTERM
logger = logging.getLogger()
logger.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
ON_THRESHOLD = 65 # (degrees Celsius) Fan kicks on at this temperature.
OFF_THRESHOLD = 55 # (degress Celsius) Fan shuts off at this temperature.
SLEEP_INTERVAL = 5 # (seconds) How often we check the core temperature.
GPIO_PIN = 17 # Which GPIO pin you're using to control the fan.
def get_temp():
"""Get the core temperature.
Run a shell script to get the core temp and parse the output.
Raises:
RuntimeError: if response cannot be parsed.
Returns:
float: The core temperature in degrees Celsius.
"""
output = subprocess.run(['vcgencmd', 'measure_temp'], capture_output=True)
temp_str = output.stdout.decode()
try:
return float(temp_str.split('=')[1].split('\'')[0])
except (IndexError, ValueError):
raise RuntimeError('Could not parse temperature output.')
def main_loop(fan):
logging.info("Starting Fan Controller...")
# Validate the on and off thresholds
if OFF_THRESHOLD >= ON_THRESHOLD:
raise RuntimeError('OFF_THRESHOLD must be less than ON_THRESHOLD')
while True:
temp = get_temp()
logger.debug("Temperature: {}".format(temp))
# Start the fan if the temperature has reached the limit and the fan
# isn't already running.
# NOTE: `fan.value` returns 1 for "on" and 0 for "off"
if temp > ON_THRESHOLD and not fan.value:
logger.info("Temperature above threshold, setting fan on: {} > {}".format(temp, ON_THRESHOLD))
fan.on()
# Stop the fan if the fan is running and the temperature has dropped
# below the limit.
elif fan.value and temp < OFF_THRESHOLD:
logger.info("Temperature below threshold, setting fan off: {} > {}".format(temp, OFF_THRESHOLD))
fan.off()
time.sleep(SLEEP_INTERVAL)
if __name__ == '__main__':
fan = OutputDevice(GPIO_PIN)
def exit_handler(*args):
logging.info("Exiting: Setting fan off.")
fan.off()
sys.exit(0)
for sig in (SIGABRT, SIGILL, SIGINT, SIGSEGV, SIGTERM):
signal(sig, exit_handler)
main_loop(fan)