-
Notifications
You must be signed in to change notification settings - Fork 0
/
fan_controller.py
116 lines (98 loc) · 3.29 KB
/
fan_controller.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
# (C) 2022 FUEL4EP (Creative Commons)
# license: https://creativecommons.org/licenses/by-nc-sa/4.0/
import sys
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')
import os
os.environ['LG_WD'] = '/tmp' #see https://forum.libreelec.tv/thread/29050-rpi4-le12-0-1-autostart-sh-with-python-script-for-fan-control-won-t-work/
import time
import RPi.GPIO as GPIO
import glob
minimum_always_ON=True
minimum_speed=30
turn_on_speed=65
target_temp=52
sleepTime=20
debugFlag=False
current_speed=0
thermal_zones_temp_dirs=glob.glob("/sys/class/thermal/thermal_zone*/temp")
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
#vcgencmd measure_temp -> res="temp=NN.N'C\n"
#temp =(res.replace("temp=","").replace("'C\n",""))
temp=res[5:9]
try :
temp= float(temp)
except :
print("vcgencmd measure_temp failed")
temp=getCPUTemperature2()
return temp
def getCPUTemperature2():
temps=[]
for file_dir in thermal_zones_temp_dirs:
with open(file_dir, "r") as file:
try :
temps.append(float(file.read()))
except :
print("Error reading from file {}".format(file_dir))
continue
if len(temps) == 0:
print("reading from sys files failed, no CPU temp detected using target_temp")
temps.append(target_temp * 1000)
return max(temps) / 1000
def setDutyCycle(new_duty):
global current_speed
if (current_speed != new_duty):
if (new_duty < 50 ):
if (new_duty > 0 ):
#set duty cycle to turn_on_speed % for 1 second after a change of the duty cycle in order to ensure the start of the fan
myPWM.ChangeDutyCycle(turn_on_speed)
time.sleep(1)
myPWM.ChangeDutyCycle(new_duty)
current_speed=new_duty
if debugFlag:
print("new PWM duty cycle = ", current_speed)
time.sleep(sleepTime)
return
try:
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.OUT)
#GPIO pin BCM24 is used for the fan PWM control
myPWM=GPIO.PWM(24,50) #PWM frequency is 50 Hz
myPWM.start(100)
current_speed=100
if debugFlag:
print("initial PWM duty cycle = ", current_speed)
while True:
temp = getCPUtemperature()
if debugFlag:
print("temperature = ", temp)
if(temp<target_temp and not minimum_always_ON):
setDutyCycle(0)
continue
if(temp<target_temp and minimum_always_ON):
setDutyCycle(minimum_speed)
continue
if(temp>=target_temp and temp<56):
setDutyCycle(40)
continue
if(temp>=56 and temp<60):
setDutyCycle(50)
continue
if(temp>=60 and temp<65):
setDutyCycle(60)
continue
if(temp>=65 and temp<70):
setDutyCycle(70)
continue
if(temp>=70 and temp<74):
setDutyCycle(80)
continue
if(temp>=74 and temp<76):
setDutyCycle(90)
continue
if(temp>=76):
setDutyCycle(100)
continue
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
GPIO.cleanup() # resets all GPIO ports used by this program