Code: Select all
#!/usr/bin/python
# SoloJW
#
# This script controls a fan based on CPU temperature.
#
# It expects a fan that's externally powered, and uses GPIO(BCM) pin 26 for control.
import RPi.GPIO as GPIO
import time
import os
# Return CPU temperature as float
def getCPUtemp():
cTemp = os.popen('vcgencmd measure_temp').readline()
return float(cTemp.replace("temp=","").replace("'C\n",""))
GPIO.setmode(GPIO.BCM)
GPIO.setup(26,GPIO.OUT)
#GPIO.setwarnings(False)
GPIO.output(26, GPIO.LOW)
time.sleep(2)
while True:
CPU_temp = getCPUtemp()
print'CPU TEMP=' , CPU_temp
if CPU_temp > 51.5:
GPIO.output(26, GPIO.HIGH)
print'FAN ON... CPU TEMP=' , CPU_temp
elif CPU_temp < 50.0:
GPIO.output(26, GPIO.LOW)
print'FAN OFF... CPU TEMP=' , CPU_temp
time.sleep(5)
except KeyboardInterrupt:
GPIO.cleanup()