Advertisement
stigzler

Untitled

Aug 14th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. import os
  2. import time
  3. import RPi.GPIO as GPIO
  4. import logging
  5.  
  6. logging.basicConfig(filename='fanControl.log', filemode='w', level=logging.DEBUG)
  7. logging.info("fanControl Log. Started: " + time.strftime("%d/%m/%Y") + " " + time.strftime("%H:%M:%S"))
  8.  
  9. print "Fan Control Started"
  10.  
  11. GPIO.setwarnings(False)
  12. GPIO.setmode(GPIO.BCM)
  13. #Set max. temperature of CPU
  14. TEMP_MAX = 65
  15. #Set ON-Time for the fan when the max. temperature has reached
  16. ON_TIME = 4
  17. #Set pin 4 as transistor gate
  18. GATE = 4
  19. #Set pin 4 as an output and shut down
  20. GPIO.setup(GATE, GPIO.OUT)
  21. GPIO.output(GATE, False)
  22.  
  23. logging.info("GPIO Output: " + str(GATE))
  24. logging.info("Fan On Time: " + str(ON_TIME))
  25. logging.info("Fan on Temp: " + str(TEMP_MAX))
  26. logging.info("================================")
  27.  
  28. #pulse at start to show operative
  29. InitFlash = 0
  30.  
  31. while InitFlash < 3 :
  32.     GPIO.output(GATE, True)
  33.     time.sleep(4)
  34.     GPIO.output(GATE, False)
  35.     time.sleep(4)
  36.     InitFlash += 1
  37.  
  38. def getCPUtemperature():
  39.     res = os.popen('vcgencmd measure_temp').readline()
  40.     return(res.replace("temp=","").replace("'C\n",""))
  41.  
  42. while True:
  43.  
  44.     temp_float = float(getCPUtemperature())
  45.    
  46.     try:
  47.         if (temp_float > TEMP_MAX):
  48.             logging.info("-------------------------------------")
  49.             logging.info(time.strftime("%M:%S: ") + "Fan on at: " + str(temp_float))
  50.             on_temp = temp_float
  51.             # ein
  52.             GPIO.output(GATE, True)
  53.             time.sleep(ON_TIME)
  54.             # aus
  55.             temp_float = float(getCPUtemperature())
  56.             logging.info(time.strftime("%M:%S: ") + "Temp at end of Fan period: " + str(temp_float))
  57.             logging.info(time.strftime("%M:%S: ") + "Temp difference: " + str(on_temp-temp_float))
  58.             if on_temp - temp_float < 0.5:
  59.                 ON_TIME = ON_TIME + 2
  60.                 logging.info(time.strftime("%M:%S: ") + "Cooling small. Increased Fan on time to: " + str(ON_TIME))
  61.             GPIO.output(GATE, False)
  62.         else:
  63.             GPIO.output(GATE, False)
  64.        
  65.     except KeyboardInterrupt:
  66.         print float(getCPUtemperature())
  67.         print "power off fan..."
  68.         GPIO.output(GATE, False)
  69.         print "cancelling..."
  70.        
  71.     time.sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement