forked from geekworm-com/x-c1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fan-rpi.py
41 lines (36 loc) · 1.21 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
#!/usr/bin/python3
# DON'T use pigpiod library on Nov. 24 2022 by Harry Huang
#import pigpio
import RPi.GPIO as IO
import time
import subprocess
servo = 18
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(['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 get temperature')
while 1:
temp = get_temp() # Get the current CPU temperature
if temp > 70: # 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.
elif temp > 60:
fan.ChangeDutyCycle(85)
elif temp > 50:
fan.ChangeDutyCycle(60)
elif temp > 40:
fan.ChangeDutyCycle(50)
elif temp > 32:
fan.ChangeDutyCycle(45)
elif temp > 25:
fan.ChangeDutyCycle(40)
else:
fan.ChangeDutyCycle(0)
time.sleep(5) # Sleep for 5 seconds