-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfan-rpi.py
executable file
·44 lines (40 loc) · 1.46 KB
/
fan-rpi.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
#!/usr/bin/python3
import RPi.GPIO as IO
import time
import subprocess
import logging
servo = 18
logging.basicConfig(format='%(message)s', level=logging.DEBUG)
IO.setwarnings(False)
IO.setmode (IO.BCM)
IO.setup(servo,IO.OUT)
fan = IO.PWM(servo,2000)
fan.start(0)
def get_temp():
output = subprocess.run(['cat', '/sys/class/thermal/thermal_zone0/temp'], capture_output=True)
temp_str = output.stdout.decode()
try:
return float(temp_str)/1000
except (IndexError, ValueError):
raise RuntimeError('Could not get temperature')
while 1:
temp = get_temp() # Get the current CPU temperature
if temp > 60: # Check temperature threshhold, in degrees celcius
fan.ChangeDutyCycle(100) # Set fan duty based on temperature, 100 is max speed and 0 is min speed or off.
logging.debug("Temp: %d, speed: 100", temp)
elif temp > 50:
fan.ChangeDutyCycle(90)
logging.debug("Temp: %d, speed: 90", temp)
elif temp > 40:
fan.ChangeDutyCycle(85)
logging.debug("Temp: %d, speed: 85", temp)
elif temp > 30:
fan.ChangeDutyCycle(70)
logging.debug("Temp: %d, speed: 70", temp)
elif temp > 25:
fan.ChangeDutyCycle(60)
logging.debug("Temp: %d, speed: 60", temp)
else:
fan.ChangeDutyCycle(0)
logging.debug("Temp: %d, speed: 0", temp)
time.sleep(5) # Sleep for 5 seconds