diff --git a/fancontrol.py b/fancontrol.py index 92f0b00..d23bf35 100755 --- a/fancontrol.py +++ b/fancontrol.py @@ -2,13 +2,16 @@ import time -from gpiozero import OutputDevice - +from gpiozero import PWMOutputDevice +SLEEP_INTERVAL = 3 # (seconds) How often we check the core temperature. ON_THRESHOLD = 65 # (degrees Celsius) Fan kicks on at this temperature. OFF_THRESHOLD = 55 # (degress Celsius) Fan shuts off at this temperature. -SLEEP_INTERVAL = 5 # (seconds) How often we check the core temperature. -GPIO_PIN = 17 # Which GPIO pin you're using to control the fan. +OUTPUTS = [0.5, 0.6, 0.7, 0.8, 0.9, 1] +TEMPS = [40.0, 45.0, 50.0, 55.0, 60.0, 65.0] +USE_PWM = True +GPIO_PIN = 18 if USE_PWM else 17 # Which GPIO pin you're using to control the fan. + def get_temp(): @@ -27,14 +30,32 @@ def get_temp(): except (IndexError, ValueError,) as e: raise RuntimeError('Could not parse temperature output.') from e +def get_speed(): + """ + Gets the temp of the board and assigns percentage of max max speed correspondingly + Returns: A value between 0-1. Can be configured by adjusting OUTPUTS and TEMPS. + """ + speed = 0.0 + for i in range(len(TEMPS)): # loop through list of temps + if get_temp() > TEMPS[i]: + speed = OUTPUTS[i] # set speed to the corresponding temp + return speed + + if __name__ == '__main__': + # Validate the on and off thresholds - if OFF_THRESHOLD >= ON_THRESHOLD: + if not USE_PWM and OFF_THRESHOLD >= ON_THRESHOLD: raise RuntimeError('OFF_THRESHOLD must be less than ON_THRESHOLD') - fan = OutputDevice(GPIO_PIN) + fan = PWMOutputDevice(GPIO_PIN) - while True: + if USE_PWM: + while True: + fan.value = get_speed() + + time.sleep(SLEEP_INTERVAL) + else: temp = get_temp() # Start the fan if the temperature has reached the limit and the fan @@ -46,6 +67,4 @@ def get_temp(): # Stop the fan if the fan is running and the temperature has dropped # to 10 degrees below the limit. elif fan.value and temp < OFF_THRESHOLD: - fan.off() - - time.sleep(SLEEP_INTERVAL) + fan.off() \ No newline at end of file