-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprogram.py
More file actions
executable file
·113 lines (90 loc) · 2.88 KB
/
program.py
File metadata and controls
executable file
·113 lines (90 loc) · 2.88 KB
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
#!/usr/bin/env python3
from __future__ import division
import os
from time import sleep
import signal
import sys
import RPi.GPIO as GPIO
import random
import configparser
Config = configparser.ConfigParser()
Config.read("/home/pi/Programs/config.ini")
section = "Fan"
if not Config:
pin = 19
waitTime_sec = 5
avg_list_num = 4
minTMP = 46 # The temperature where the fan will has the minimum speed.
maxTMP = 48 # The temperature where the fan will has the maximum speed.
else:
pin = int(Config.get(section,"pin"))
waitTime_sec = int(Config.get(section,"waitTime_sec"))
avg_list_num = int(Config.get(section,"avg_list_num"))
# The temperature where the fan will has the minimum speed.
minTMP = float(Config.get(section,"minTMP"))
# The temperature where the fan will has the maximum speed.
maxTMP = float(Config.get(section,"maxTMP"))
global temp_list
temp_list = []
verbose=True
global pwm_pin
pwm_pin = None
pwm_speeds = [0,25,50,75,100]
freq= 4 #in Hz
# Do not change this values. They are used to implement the appropiate speed.
global allocate_const
allocate_const = None
def setup():
global allocate_const
allocate_const = (maxTMP-minTMP)/(len(pwm_speeds)-2)
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
GPIO.setwarnings(False)
global pwm_pin
pwm_pin = GPIO.PWM(pin, freq)
pwm_pin.start(0)
global temp_list
for i in range(avg_list_num):
temp_list.append(getCPUtemperature())
return()
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
temp =(res.replace("temp=","").replace("'C\n",""))
#print(“temp is {0}”.format(temp)) #Uncomment here for testing
return float(temp)
def fanPWM(temp):
# temp = round(random.uniform(minTMP-1, maxTMP+1), 2)
global temp_list
temp_list.append(temp)
temp_list.pop(0)
temp_avg = round(sum(temp_list) / len(temp_list),2)
if (temp_avg <= minTMP):
setSpeed = pwm_speeds[0]
elif (temp_avg >= maxTMP):
setSpeed = pwm_speeds[-1]
else:
aux_temp = temp_avg - minTMP
global allocate_const
setSpeed = pwm_speeds[(int)(aux_temp/allocate_const) + 1]
global pwm_pin
pwm_pin.ChangeDutyCycle(setSpeed)
if verbose==True:
print("Current: ", temp, "\tAvg: ", temp_avg, "\tSpeed(%): ", setSpeed)
return()
def getTEMP():
CPU_temp = float(getCPUtemperature())
fanPWM(CPU_temp)
return()
def main():
try:
setup()
while True:
getTEMP()
sleep(waitTime_sec) # Read the temperature every 5 sec, increase or decrease this limit if you want
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
print("Stoping CPU cooler controler.")
global pwm_pin
pwm_pin.stop()
GPIO.cleanup() # resets all GPIO ports used by this program
if __name__ == "__main__":
main()