-
Notifications
You must be signed in to change notification settings - Fork 0
/
fan_service.py
45 lines (40 loc) · 1.44 KB
/
fan_service.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
import RPi.GPIO as GPIO
import time
import subprocess as sp
# initializing GPIO, setting mode to BOARD.
# Default pin of FAN Adapter is physical pin 32, GPIO12;
Fan = 32 #if you connect to pin txd physical pin 8, GPIO14,then set to :Fan = 8
GPIO.setmode(GPIO.BOARD)
GPIO.setup(Fan, GPIO.OUT)
p = GPIO.PWM(Fan, 50)
p.start(0)
try:
while True:
temp = sp.getoutput("vcgencmd measure_temp|egrep -o '[0-9]*\.[0-9]*'")
# print(temp)
cpu = float(temp)
#print('CPU temperature is: %2.2f' % cpu)
if float(temp) < 40.0:
p.ChangeDutyCycle(0)
# print('Fan is off as CPU temperature is %2.2f' % cpu)
time.sleep(10)
elif float(temp) > 40.0 and float(temp) < 45.0:
p.ChangeDutyCycle(30)
# print('Fan is on 30 as CPU temperature is %2.2f' % cpu)
time.sleep(10)
elif float(temp) > 45.0 and float(temp) < 50.0:
p.ChangeDutyCycle(50)
# print('Fan is on 50 as CPU temperature is %2.2f' % cpu)
time.sleep(10)
elif float(temp) > 50.0 and float(temp) < 55.0:
p.ChangeDutyCycle(75)
# print('Fan is on 75 as CPU temperature is %2.2f' % cpu)
time.sleep(10)
elif float(temp) > 55.0:
p.ChangeDutyCycle(100)
# print('Fan is on 100 as CPU temperature is %2.2f' % cpu)
time.sleep(10)
except KeyboardInterrupt:
pass
p.stop()
GPIO.cleanup()