Sandbird

Automated Cooling Fan for Pi v.2

May 28th, 2019
178
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/env python3
  2. import os
  3. import RPi.GPIO as GPIO
  4. import time
  5. import datetime
  6. import sys
  7.  
  8. # 5 * * * * sudo python /home/pi/fan.py
  9. # A crontab will run every hour and check the temp. If the temp is > 49 the script will start the fan
  10. # until the temperature goes down to 28. When it does, the script will end, shutting down the fan as well.
  11. # If the script executes again while a previous script is running, the latter will exit
  12. # ... meaning the pi is in hell, and will never get bellow FAN_END value :P
  13.  
  14. # Identify which pin controls the relay
  15. FAN_PIN = 18 # the yellow box ex: GPIO18
  16. # Temperature check. Start fan if temp > 49C
  17. FAN_START = 49
  18. # Temperature check. Shut down under 28C
  19. FAN_END = 28
  20.  
  21. # Get what action. If you manually turning on/off the fan
  22. action = sys.argv.pop()
  23.  
  24.  
  25. def GPIOsetup():
  26.     GPIO.setwarnings(False)
  27.     GPIO.setmode(GPIO.BCM)
  28.     GPIO.setup(FAN_PIN, GPIO.OUT)
  29.    
  30. def fanON():
  31.     GPIOsetup()
  32.     GPIO.output(FAN_PIN, 0) #fan on
  33.     return()
  34. def fanOFF():
  35.     GPIOsetup()
  36.     GPIO.output(FAN_PIN, 1) #fan off
  37.     return()
  38.    
  39. def get_temp_from_system():
  40.     res = os.popen('vcgencmd measure_temp').readline()
  41.     return(res.replace("temp=","").replace("'C\n",""))
  42.  
  43. def check_fan(pin):
  44.     GPIOsetup()
  45.     return GPIO.input(pin)
  46.  
  47. def run(pin):
  48.     current_date = datetime.datetime.now()
  49.     temp = get_temp_from_system()
  50.     if float(temp) >= FAN_START:
  51.         print(temp+' @ '+str(current_date))
  52.         if check_fan(pin) == 1:
  53.             print('Fan is Off...Starting Fan')
  54.             fanON()
  55.         else:
  56.             time.sleep(5) # Remove, if you want real-time checking
  57.             print('Fan is ON')
  58.     elif float(temp) <= FAN_END:
  59.         print(temp+' @ '+str(current_date))
  60.         if check_fan(pin) == 0:
  61.             print('Fan is on...Shuting it Down')
  62.             fanOFF()
  63.             GPIO.cleanup()
  64.             return 1 # exit script. The pi has cooled down
  65.         else:
  66.             time.sleep(5) # Remove, if you want real-time checking
  67.             print('Fan is OFF')
  68.     else:
  69.             pass # while the script is passing through here, there will be no output on screen
  70.            
  71.            
  72. if action == "on" :
  73.    print('Turning fan on')
  74.    fanON()
  75. elif action == "off" :
  76.    print('Turning fan off')
  77.    fanOFF()
  78.    GPIO.cleanup()
  79.  
  80. # first check if script is already running
  81. if check_fan(FAN_PIN) == 0:
  82.     print('Fan is on, script must be running from another instance...')
  83. else:
  84.     temp = get_temp_from_system()
  85.     if float(temp) < FAN_START:
  86.         print('Pi is operating under normal temperatures.')
  87.     else:
  88.         try:
  89.             while(True):
  90.                 tmp = run(FAN_PIN)
  91.                 if tmp == 1: # value returned from line 60
  92.                     break
  93.         except KeyboardInterrupt:
  94.             fanOFF()
  95.             GPIO.cleanup()
  96.         finally:
  97.             fanOFF()
  98.             GPIO.cleanup()
RAW Paste Data