generated from egvimo/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfan_control.py
40 lines (27 loc) · 1.03 KB
/
fan_control.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
import logging
import time
from gpiozero import PWMOutputDevice, CPUTemperature
from gpiozero.pins.rpigpio import RPiGPIOFactory
_FACTORY = RPiGPIOFactory()
_FAN = PWMOutputDevice(18, pin_factory=_FACTORY)
logging.basicConfig(level=logging.INFO)
def _read_temperature() -> float:
return CPUTemperature().value * 100
def _adjust_speed(adjustment: int):
current_speed: float = _FAN.value * 100
target_speed = min(max(current_speed + adjustment, 0), 100)
logging.info("Adjusting speed from %.1f to %.1f", current_speed, target_speed)
_FAN.value = target_speed / 100
def start() -> None:
logging.info("Starting fan controller with initial speed of 50.0")
_FAN.on()
_FAN.value = 0.5
while True:
temperature = _read_temperature()
if abs(45 - temperature) > 3:
logging.info("Temperature deviates: %.1f", temperature)
speed_adjustment = 5 if temperature > 45 else -5
_adjust_speed(speed_adjustment)
time.sleep(30)
if __name__ == "__main__":
start()