-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpufan.py
59 lines (42 loc) · 1.3 KB
/
cpufan.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
#!/usr/bin/env python3
# Author: Edoardo Paolo Scalafiotti <[email protected]>
# Use with a PNP transistor like the P2222A
# Add line in /etc/rc.local to execute file on boot.
import os
from time import sleep
import RPi.GPIO as GPIO
pin = 15 # The pin ID, edit here to change it
maxTMP = 45 # The maximum temperature in Celsius after which we trigger the fan
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
GPIO.setwarnings(False)
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 temp
def fanON():
setPin(True)
return()
def fanOFF():
setPin(False)
return()
def getTEMP():
CPU_temp = float(getCPUtemperature())
if CPU_temp>maxTMP:
fanON()
else:
fanOFF()
return()
def setPin(mode): # A little redundant function but useful if you want to add logging
GPIO.output(pin, mode)
return()
try:
setup()
while True:
getTEMP()
sleep(10) # Read the temperature every 10 sec, increase or decrease this limit if you want
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
GPIO.cleanup() # resets all GPIO ports used by this program