Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import time
- import RPi.GPIO as GPIO
- import logging
- logging.basicConfig(filename='fanControl.log', filemode='w', level=logging.DEBUG)
- logging.info("fanControl Log. Started: " + time.strftime("%d/%m/%Y") + " " + time.strftime("%H:%M:%S"))
- print "Fan Control Started"
- GPIO.setwarnings(False)
- GPIO.setmode(GPIO.BCM)
- #Set max. temperature of CPU
- TEMP_MAX = 65
- #Set ON-Time for the fan when the max. temperature has reached
- ON_TIME = 4
- #Set pin 4 as transistor gate
- GATE = 4
- #Set pin 4 as an output and shut down
- GPIO.setup(GATE, GPIO.OUT)
- GPIO.output(GATE, False)
- logging.info("GPIO Output: " + str(GATE))
- logging.info("Fan On Time: " + str(ON_TIME))
- logging.info("Fan on Temp: " + str(TEMP_MAX))
- logging.info("================================")
- #pulse at start to show operative
- InitFlash = 0
- while InitFlash < 3 :
- GPIO.output(GATE, True)
- time.sleep(4)
- GPIO.output(GATE, False)
- time.sleep(4)
- InitFlash += 1
- def getCPUtemperature():
- res = os.popen('vcgencmd measure_temp').readline()
- return(res.replace("temp=","").replace("'C\n",""))
- while True:
- temp_float = float(getCPUtemperature())
- try:
- if (temp_float > TEMP_MAX):
- logging.info("-------------------------------------")
- logging.info(time.strftime("%M:%S: ") + "Fan on at: " + str(temp_float))
- on_temp = temp_float
- # ein
- GPIO.output(GATE, True)
- time.sleep(ON_TIME)
- # aus
- temp_float = float(getCPUtemperature())
- logging.info(time.strftime("%M:%S: ") + "Temp at end of Fan period: " + str(temp_float))
- logging.info(time.strftime("%M:%S: ") + "Temp difference: " + str(on_temp-temp_float))
- if on_temp - temp_float < 0.5:
- ON_TIME = ON_TIME + 2
- logging.info(time.strftime("%M:%S: ") + "Cooling small. Increased Fan on time to: " + str(ON_TIME))
- GPIO.output(GATE, False)
- else:
- GPIO.output(GATE, False)
- except KeyboardInterrupt:
- print float(getCPUtemperature())
- print "power off fan..."
- GPIO.output(GATE, False)
- print "cancelling..."
- time.sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement