Advertisement
Sandbird

Untitled

Aug 26th, 2024 (edited)
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  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 below FAN_END value :P
  13.  
  14. # Configuration
  15. FAN_PIN = 3 # Pin controlling the relay
  16. FAN_START = 53 # Start the fan if temperature > 49C
  17. FAN_END = 50 # Stop the fan if temperature < 28C
  18.  
  19. # Get manual action from command line
  20. action = sys.argv.pop() if len(sys.argv) > 1 else None
  21.  
  22. def GPIOsetup():
  23. GPIO.setwarnings(False)
  24. GPIO.setmode(GPIO.BCM)
  25. GPIO.setup(FAN_PIN, GPIO.OUT)
  26.  
  27. def fanON():
  28. GPIOsetup()
  29. GPIO.output(FAN_PIN, 0) # Fan on (relay closed)
  30.  
  31. def fanOFF():
  32. GPIOsetup()
  33. GPIO.output(FAN_PIN, 1) # Fan off (relay opened)
  34. GPIO.cleanup()
  35.  
  36. def get_temp_from_system():
  37. res = os.popen('vcgencmd measure_temp').readline()
  38. return res.replace("temp=", "").replace("'C\n", "")
  39.  
  40. def check_fan(pin):
  41. GPIOsetup()
  42. return GPIO.input(pin)
  43.  
  44. def run(pin):
  45. current_date = datetime.datetime.now()
  46. temp = float(get_temp_from_system())
  47.  
  48. if temp >= FAN_START:
  49. print(f"{temp}C @ {current_date}")
  50. if check_fan(pin) == 1: # Fan is OFF
  51. print('Fan is Off... Starting Fan')
  52. fanON()
  53. else:
  54. print('Fan is already ON')
  55.  
  56. elif temp <= FAN_END:
  57. print(f"{temp}C @ {current_date}")
  58. if check_fan(pin) == 0: # Fan is ON
  59. print('Fan is on... Shutting it Down')
  60. fanOFF()
  61. return 1 # Exit script, Pi has cooled down
  62. else:
  63. print('Fan is already OFF')
  64.  
  65. # Handle manual actions
  66. if action == "on":
  67. print('Turning fan on')
  68. fanON()
  69. sys.exit(0)
  70. elif action == "off":
  71. print('Turning fan off')
  72. fanOFF()
  73. sys.exit(0)
  74.  
  75. # First check if the script is already running by checking fan state
  76. fan_state = check_fan(FAN_PIN)
  77.  
  78. if fan_state == 0:
  79. print('Fan is on, script must be running from another instance...')
  80. sys.exit(0)
  81. else:
  82. temp = float(get_temp_from_system())
  83.  
  84. if temp < FAN_START:
  85. print(f"Pi is operating under normal temperatures: {temp}C")
  86. fanOFF()
  87. sys.exit(0)
  88. else:
  89. try:
  90. while True:
  91. tmp = run(FAN_PIN)
  92. if tmp == 1: # Fan turned off because the temperature dropped
  93. break
  94. time.sleep(5) # Sleep to prevent excessive CPU usage
  95. except KeyboardInterrupt:
  96. fanOFF()
  97. finally:
  98. fanOFF()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement