From 27a1ea032459d9dbde983bddb84154590e431f2c Mon Sep 17 00:00:00 2001 From: Ben Greene Date: Sun, 26 Jan 2020 19:06:20 +0000 Subject: [PATCH 01/23] Replace shell script and parse for temp with reading from sys Remove code that ran shell and parsed output Add code to read temp directly from /sys (as integer) Change temp thresholds from C to C/1000 to keep all arithmetic in int --- fancontrol.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index 0bb0307..ea08fde 100755 --- a/fancontrol.py +++ b/fancontrol.py @@ -6,8 +6,8 @@ from gpiozero import OutputDevice -ON_THRESHOLD = 65 # (degrees Celsius) Fan kicks on at this temperature. -OFF_THRESHOLD = 55 # (degress Celsius) Fan shuts off at this temperature. +ON_THRESHOLD = 65*1000 # (degrees Celsius) Fan kicks on at this temperature. +OFF_THRESHOLD = 55*1000 # (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. @@ -15,21 +15,15 @@ def get_temp(): """Get the core temperature. - Run a shell script to get the core temp and parse the output. - - Raises: - RuntimeError: if response cannot be parsed. + Read file from /sys to get CPU temp in temp in C *1000 Returns: - float: The core temperature in degrees Celsius. + int: The core temperature in thousanths of degrees Celsius. """ - output = subprocess.run(['vcgencmd', 'measure_temp'], capture_output=True) - temp_str = output.stdout.decode() - try: - return float(temp_str.split('=')[1].split('\'')[0]) - except (IndexError, ValueError): - raise RuntimeError('Could not parse temperature output.') - + with open("/sys/class/thermal/thermal_zone0/temp") as f: + temp_data = f.read() + + return int(temp_data) if __name__ == '__main__': # Validate the on and off thresholds From 25cd8289f095226df4ba80a94ee98785975ba770 Mon Sep 17 00:00:00 2001 From: Michael Sulyak <6431441+expert-m@users.noreply.github.com> Date: Sat, 13 Jun 2020 20:12:12 +0700 Subject: [PATCH 02/23] Read the core temperature from the file --- fancontrol.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index 0bb0307..9ee3243 100755 --- a/fancontrol.py +++ b/fancontrol.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 -import subprocess import time from gpiozero import OutputDevice @@ -23,12 +22,14 @@ def get_temp(): Returns: float: The core temperature in degrees Celsius. """ - output = subprocess.run(['vcgencmd', 'measure_temp'], capture_output=True) - temp_str = output.stdout.decode() + + with open('/sys/class/thermal/thermal_zone0/temp') as file: + temp_str = file.read() + try: - return float(temp_str.split('=')[1].split('\'')[0]) - except (IndexError, ValueError): - raise RuntimeError('Could not parse temperature output.') + return int(temp_str) / 1000 + except (IndexError, ValueError,) as e: + raise RuntimeError('Could not parse temperature output.') from e if __name__ == '__main__': From 5c39db67f970a4eb8b5314b1491eb66369326e44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Sat, 22 Aug 2020 09:28:12 +0200 Subject: [PATCH 03/23] Update fancontrol.py Fan speed control functionality added --- fancontrol.py | 66 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index 0bb0307..07356af 100755 --- a/fancontrol.py +++ b/fancontrol.py @@ -5,12 +5,14 @@ from gpiozero import OutputDevice - -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. - +MAX_THRESHOLD = 60.0 # (degrees Celsius) Fan kicks on at full speed at this temperature. +MIN_THRESHOLD = 50.0 # (degress Celsius) Fan kicks on at minimum speed at this temperature. +SLEEP_INTERVAL = 1.0 # (miliseconds) How long one tick last +GPIO_PIN = 17 # Which GPIO pin you're using to control the fan. +MIN_TICKS = 200 # Number of min ticks in cycle +MAX_TICKS = 1000 # Number of max ticks in cycle +FAN_ON = 1 +FAN_OFF = 0 def get_temp(): """Get the core temperature. @@ -30,26 +32,52 @@ def get_temp(): except (IndexError, ValueError): raise RuntimeError('Could not parse temperature output.') +def normalize_temp(temp): + temp = ((float(temp) - MIN_THRESHOLD) / (MAX_THRESHOLD - MIN_THRESHOLD)) + if temp < 0.0: + temp = 0.0 + if temp > 1.0: + temp = 1.0 + return float(temp) + +def count_fire_tick(temp): + temp = int((MAX_TICKS / MIN_TICKS) - (MAX_TICKS / MIN_TICKS) * float(temp)) + if temp <= 0: + temp = 1 + return int(temp) + +def fan_command(command, fan): + if command == FAN_ON and not fan.value: + fan.on() + if command == FAN_OFF and fan.value: + fan.off() + +def run_cycle(fire_tick, fan): + i = 0 + while i < MAX_TICKS: + i += 1 + if (i % fire_tick) == 0: + fan_command(FAN_ON, fan) + time.sleep(SLEEP_INTERVAL / 1000.0) + if (i % fire_tick) != 0: + fan_command(FAN_OFF, fan) if __name__ == '__main__': - # Validate the on and off thresholds - if OFF_THRESHOLD >= ON_THRESHOLD: - raise RuntimeError('OFF_THRESHOLD must be less than ON_THRESHOLD') + # Validate the min and max thresholds + if MIN_THRESHOLD >= MAX_THRESHOLD: + raise RuntimeError('MIN_THRESHOLD must be less than MAX_THRESHOLD') + + # Validate the min and max ticks + if MIN_TICKS >= MAX_TICKS: + raise RuntimeError('MIN_TICKS must be less than MAX_TICKS') fan = OutputDevice(GPIO_PIN) while True: temp = get_temp() - # Start the fan if the temperature has reached the limit and the fan - # isn't already running. - # NOTE: `fan.value` returns 1 for "on" and 0 for "off" - if temp > ON_THRESHOLD and not fan.value: - fan.on() + temp = normalize_temp(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() + temp = count_fire_tick(temp) - time.sleep(SLEEP_INTERVAL) + run_cycle(temp, fan) From 833880b3d8ad887691379240d2f2cd1f073695b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Mon, 24 Aug 2020 22:27:16 +0200 Subject: [PATCH 04/23] Update fancontrol.py Low fan speed adjustments --- fancontrol.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index 07356af..01d6dd3 100755 --- a/fancontrol.py +++ b/fancontrol.py @@ -6,10 +6,10 @@ from gpiozero import OutputDevice MAX_THRESHOLD = 60.0 # (degrees Celsius) Fan kicks on at full speed at this temperature. -MIN_THRESHOLD = 50.0 # (degress Celsius) Fan kicks on at minimum speed at this temperature. +MIN_THRESHOLD = 40.0 # (degress Celsius) Fan kicks on at minimum speed at this temperature. SLEEP_INTERVAL = 1.0 # (miliseconds) How long one tick last GPIO_PIN = 17 # Which GPIO pin you're using to control the fan. -MIN_TICKS = 200 # Number of min ticks in cycle +MIN_TICKS = 100 # Number of min ticks in cycle MAX_TICKS = 1000 # Number of max ticks in cycle FAN_ON = 1 FAN_OFF = 0 From b87e6480e6292997119de1dd09f4230b615f0725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Tue, 25 Aug 2020 12:45:46 +0200 Subject: [PATCH 05/23] Update fancontrol.py Final adjustments --- fancontrol.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fancontrol.py b/fancontrol.py index 01d6dd3..0b84ff2 100755 --- a/fancontrol.py +++ b/fancontrol.py @@ -6,7 +6,7 @@ from gpiozero import OutputDevice MAX_THRESHOLD = 60.0 # (degrees Celsius) Fan kicks on at full speed at this temperature. -MIN_THRESHOLD = 40.0 # (degress Celsius) Fan kicks on at minimum speed at this temperature. +MIN_THRESHOLD = 50.0 # (degress Celsius) Fan kicks on at minimum speed at this temperature. SLEEP_INTERVAL = 1.0 # (miliseconds) How long one tick last GPIO_PIN = 17 # Which GPIO pin you're using to control the fan. MIN_TICKS = 100 # Number of min ticks in cycle From 8c32a10c6cf680e67fc978dbb4bda4a5764afcde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Wed, 28 Jul 2021 06:13:27 +0200 Subject: [PATCH 06/23] Update fancontrol.py Optimizations --- fancontrol.py | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index 0b84ff2..254e297 100755 --- a/fancontrol.py +++ b/fancontrol.py @@ -15,16 +15,6 @@ FAN_OFF = 0 def get_temp(): - """Get the core temperature. - - Run a shell script to get the core temp and parse the output. - - Raises: - RuntimeError: if response cannot be parsed. - - Returns: - float: The core temperature in degrees Celsius. - """ output = subprocess.run(['vcgencmd', 'measure_temp'], capture_output=True) temp_str = output.stdout.decode() try: @@ -41,10 +31,10 @@ def normalize_temp(temp): return float(temp) def count_fire_tick(temp): - temp = int((MAX_TICKS / MIN_TICKS) - (MAX_TICKS / MIN_TICKS) * float(temp)) + temp = ((MAX_TICKS / MIN_TICKS) - (MAX_TICKS / MIN_TICKS) * float(temp)) if temp <= 0: temp = 1 - return int(temp) + return float(temp) def fan_command(command, fan): if command == FAN_ON and not fan.value: @@ -56,20 +46,13 @@ def run_cycle(fire_tick, fan): i = 0 while i < MAX_TICKS: i += 1 - if (i % fire_tick) == 0: - fan_command(FAN_ON, fan) time.sleep(SLEEP_INTERVAL / 1000.0) - if (i % fire_tick) != 0: + if i % (int(fire_tick) + int((i * fire_tick) % 1)) == 0: + fan_command(FAN_ON, fan) + else: fan_command(FAN_OFF, fan) if __name__ == '__main__': - # Validate the min and max thresholds - if MIN_THRESHOLD >= MAX_THRESHOLD: - raise RuntimeError('MIN_THRESHOLD must be less than MAX_THRESHOLD') - - # Validate the min and max ticks - if MIN_TICKS >= MAX_TICKS: - raise RuntimeError('MIN_TICKS must be less than MAX_TICKS') fan = OutputDevice(GPIO_PIN) From 351545556f68b1fd08eb7a7c4224d6b77b21907e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Wed, 28 Jul 2021 07:15:19 +0200 Subject: [PATCH 07/23] Update fancontrol.py Optimization fixes --- fancontrol.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index 254e297..5058641 100755 --- a/fancontrol.py +++ b/fancontrol.py @@ -9,7 +9,7 @@ MIN_THRESHOLD = 50.0 # (degress Celsius) Fan kicks on at minimum speed at this temperature. SLEEP_INTERVAL = 1.0 # (miliseconds) How long one tick last GPIO_PIN = 17 # Which GPIO pin you're using to control the fan. -MIN_TICKS = 100 # Number of min ticks in cycle +MIN_TICKS = 200 # Number of min ticks in cycle MAX_TICKS = 1000 # Number of max ticks in cycle FAN_ON = 1 FAN_OFF = 0 @@ -47,7 +47,7 @@ def run_cycle(fire_tick, fan): while i < MAX_TICKS: i += 1 time.sleep(SLEEP_INTERVAL / 1000.0) - if i % (int(fire_tick) + int((i * fire_tick) % 1)) == 0: + if i % int(fire_tick) == 0: fan_command(FAN_ON, fan) else: fan_command(FAN_OFF, fan) From be8b073c797a8277dd4188fc0a45b8fddb3aad01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Wed, 28 Jul 2021 07:32:47 +0200 Subject: [PATCH 08/23] Update fancontrol.py Revert --- fancontrol.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index 5058641..fa0ea8a 100755 --- a/fancontrol.py +++ b/fancontrol.py @@ -45,11 +45,10 @@ def fan_command(command, fan): def run_cycle(fire_tick, fan): i = 0 while i < MAX_TICKS: - i += 1 - time.sleep(SLEEP_INTERVAL / 1000.0) - if i % int(fire_tick) == 0: + if (i % fire_tick) == 0: fan_command(FAN_ON, fan) - else: + time.sleep(SLEEP_INTERVAL / 1000.0) + if (i % fire_tick) != 0: fan_command(FAN_OFF, fan) if __name__ == '__main__': From ff592b16a79aeea9fec4b07fbf5f1ef000c835e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Wed, 28 Jul 2021 07:34:03 +0200 Subject: [PATCH 09/23] Update fancontrol.py --- fancontrol.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index fa0ea8a..d4ddde4 100755 --- a/fancontrol.py +++ b/fancontrol.py @@ -45,10 +45,10 @@ def fan_command(command, fan): def run_cycle(fire_tick, fan): i = 0 while i < MAX_TICKS: - if (i % fire_tick) == 0: + if (i % int(fire_tick)) == 0: fan_command(FAN_ON, fan) time.sleep(SLEEP_INTERVAL / 1000.0) - if (i % fire_tick) != 0: + if (i % int(fire_tick)) != 0: fan_command(FAN_OFF, fan) if __name__ == '__main__': From f65c964403839e7cdccdcdf12c012563fb45f1f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Wed, 28 Jul 2021 07:38:46 +0200 Subject: [PATCH 10/23] Update fancontrol.py --- fancontrol.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index d4ddde4..b904b58 100755 --- a/fancontrol.py +++ b/fancontrol.py @@ -9,7 +9,7 @@ MIN_THRESHOLD = 50.0 # (degress Celsius) Fan kicks on at minimum speed at this temperature. SLEEP_INTERVAL = 1.0 # (miliseconds) How long one tick last GPIO_PIN = 17 # Which GPIO pin you're using to control the fan. -MIN_TICKS = 200 # Number of min ticks in cycle +MIN_TICKS = 100 # Number of min ticks in cycle MAX_TICKS = 1000 # Number of max ticks in cycle FAN_ON = 1 FAN_OFF = 0 @@ -32,8 +32,8 @@ def normalize_temp(temp): def count_fire_tick(temp): temp = ((MAX_TICKS / MIN_TICKS) - (MAX_TICKS / MIN_TICKS) * float(temp)) - if temp <= 0: - temp = 1 + if temp <= 0.0: + temp = 1.0 return float(temp) def fan_command(command, fan): From 402bd6410678eb43b22d98bd2dc37c998bbaafbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Wed, 28 Jul 2021 07:44:37 +0200 Subject: [PATCH 11/23] Update fancontrol.py Full revert --- fancontrol.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index b904b58..0b84ff2 100755 --- a/fancontrol.py +++ b/fancontrol.py @@ -15,6 +15,16 @@ FAN_OFF = 0 def get_temp(): + """Get the core temperature. + + Run a shell script to get the core temp and parse the output. + + Raises: + RuntimeError: if response cannot be parsed. + + Returns: + float: The core temperature in degrees Celsius. + """ output = subprocess.run(['vcgencmd', 'measure_temp'], capture_output=True) temp_str = output.stdout.decode() try: @@ -31,10 +41,10 @@ def normalize_temp(temp): return float(temp) def count_fire_tick(temp): - temp = ((MAX_TICKS / MIN_TICKS) - (MAX_TICKS / MIN_TICKS) * float(temp)) - if temp <= 0.0: - temp = 1.0 - return float(temp) + temp = int((MAX_TICKS / MIN_TICKS) - (MAX_TICKS / MIN_TICKS) * float(temp)) + if temp <= 0: + temp = 1 + return int(temp) def fan_command(command, fan): if command == FAN_ON and not fan.value: @@ -45,13 +55,21 @@ def fan_command(command, fan): def run_cycle(fire_tick, fan): i = 0 while i < MAX_TICKS: - if (i % int(fire_tick)) == 0: + i += 1 + if (i % fire_tick) == 0: fan_command(FAN_ON, fan) time.sleep(SLEEP_INTERVAL / 1000.0) - if (i % int(fire_tick)) != 0: + if (i % fire_tick) != 0: fan_command(FAN_OFF, fan) if __name__ == '__main__': + # Validate the min and max thresholds + if MIN_THRESHOLD >= MAX_THRESHOLD: + raise RuntimeError('MIN_THRESHOLD must be less than MAX_THRESHOLD') + + # Validate the min and max ticks + if MIN_TICKS >= MAX_TICKS: + raise RuntimeError('MIN_TICKS must be less than MAX_TICKS') fan = OutputDevice(GPIO_PIN) From 9115d2ff38d8bf23627f0b3232530c75243996eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Thu, 2 Sep 2021 04:18:33 +0200 Subject: [PATCH 12/23] Get temp method update --- fancontrol.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index 0b84ff2..ce236f5 100755 --- a/fancontrol.py +++ b/fancontrol.py @@ -17,20 +17,18 @@ def get_temp(): """Get the core temperature. - Run a shell script to get the core temp and parse the output. - - Raises: - RuntimeError: if response cannot be parsed. + Read file from /sys to get CPU temp in temp in C *1000 Returns: - float: The core temperature in degrees Celsius. + int: The core temperature in thousanths of degrees Celsius. """ - output = subprocess.run(['vcgencmd', 'measure_temp'], capture_output=True) - temp_str = output.stdout.decode() + with open('/sys/class/thermal/thermal_zone0/temp') as f: + temp_str = f.read() + try: - return float(temp_str.split('=')[1].split('\'')[0]) - except (IndexError, ValueError): - raise RuntimeError('Could not parse temperature output.') + return int(temp_str) / 1000 + except (IndexError, ValueError,) as e: + raise RuntimeError('Could not parse temperature output.') from e def normalize_temp(temp): temp = ((float(temp) - MIN_THRESHOLD) / (MAX_THRESHOLD - MIN_THRESHOLD)) From b3d1bebb6685275a2f7031fb85bb62c01eae5f9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Sat, 22 Aug 2020 09:28:12 +0200 Subject: [PATCH 13/23] Update fancontrol.py Fan speed control functionality added --- fancontrol.py | 70 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 23 deletions(-) mode change 100755 => 100644 fancontrol.py diff --git a/fancontrol.py b/fancontrol.py old mode 100755 new mode 100644 index 92f0b00..5fe9aa9 --- a/fancontrol.py +++ b/fancontrol.py @@ -4,12 +4,14 @@ from gpiozero import OutputDevice - -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. - +MAX_THRESHOLD = 60.0 # (degrees Celsius) Fan kicks on at full speed at this temperature. +MIN_THRESHOLD = 50.0 # (degress Celsius) Fan kicks on at minimum speed at this temperature. +SLEEP_INTERVAL = 1.0 # (miliseconds) How long one tick last +GPIO_PIN = 17 # Which GPIO pin you're using to control the fan. +MIN_TICKS = 200 # Number of min ticks in cycle +MAX_TICKS = 1000 # Number of max ticks in cycle +FAN_ON = 1 +FAN_OFF = 0 def get_temp(): """Get the core temperature. @@ -22,30 +24,52 @@ def get_temp(): with open('/sys/class/thermal/thermal_zone0/temp') as f: temp_str = f.read() - try: - return int(temp_str) / 1000 - except (IndexError, ValueError,) as e: - raise RuntimeError('Could not parse temperature output.') from e +def normalize_temp(temp): + temp = ((float(temp) - MIN_THRESHOLD) / (MAX_THRESHOLD - MIN_THRESHOLD)) + if temp < 0.0: + temp = 0.0 + if temp > 1.0: + temp = 1.0 + return float(temp) + +def count_fire_tick(temp): + temp = int((MAX_TICKS / MIN_TICKS) - (MAX_TICKS / MIN_TICKS) * float(temp)) + if temp <= 0: + temp = 1 + return int(temp) + +def fan_command(command, fan): + if command == FAN_ON and not fan.value: + fan.on() + if command == FAN_OFF and fan.value: + fan.off() + +def run_cycle(fire_tick, fan): + i = 0 + while i < MAX_TICKS: + i += 1 + if (i % fire_tick) == 0: + fan_command(FAN_ON, fan) + time.sleep(SLEEP_INTERVAL / 1000.0) + if (i % fire_tick) != 0: + fan_command(FAN_OFF, fan) if __name__ == '__main__': - # Validate the on and off thresholds - if OFF_THRESHOLD >= ON_THRESHOLD: - raise RuntimeError('OFF_THRESHOLD must be less than ON_THRESHOLD') + # Validate the min and max thresholds + if MIN_THRESHOLD >= MAX_THRESHOLD: + raise RuntimeError('MIN_THRESHOLD must be less than MAX_THRESHOLD') + + # Validate the min and max ticks + if MIN_TICKS >= MAX_TICKS: + raise RuntimeError('MIN_TICKS must be less than MAX_TICKS') fan = OutputDevice(GPIO_PIN) while True: temp = get_temp() - # Start the fan if the temperature has reached the limit and the fan - # isn't already running. - # NOTE: `fan.value` returns 1 for "on" and 0 for "off" - if temp > ON_THRESHOLD and not fan.value: - fan.on() + temp = normalize_temp(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() + temp = count_fire_tick(temp) - time.sleep(SLEEP_INTERVAL) + run_cycle(temp, fan) From dd8b1ebf8db47e2341c978a670c9b00d3da99811 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Mon, 24 Aug 2020 22:27:16 +0200 Subject: [PATCH 14/23] Update fancontrol.py Low fan speed adjustments --- fancontrol.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index 5fe9aa9..00b09b4 100644 --- a/fancontrol.py +++ b/fancontrol.py @@ -5,10 +5,10 @@ from gpiozero import OutputDevice MAX_THRESHOLD = 60.0 # (degrees Celsius) Fan kicks on at full speed at this temperature. -MIN_THRESHOLD = 50.0 # (degress Celsius) Fan kicks on at minimum speed at this temperature. +MIN_THRESHOLD = 40.0 # (degress Celsius) Fan kicks on at minimum speed at this temperature. SLEEP_INTERVAL = 1.0 # (miliseconds) How long one tick last GPIO_PIN = 17 # Which GPIO pin you're using to control the fan. -MIN_TICKS = 200 # Number of min ticks in cycle +MIN_TICKS = 100 # Number of min ticks in cycle MAX_TICKS = 1000 # Number of max ticks in cycle FAN_ON = 1 FAN_OFF = 0 From 6021499c6ad252f9352ddc0f7a525ba0a2ca2452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Tue, 25 Aug 2020 12:45:46 +0200 Subject: [PATCH 15/23] Update fancontrol.py Final adjustments --- fancontrol.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fancontrol.py b/fancontrol.py index 00b09b4..2b00c55 100644 --- a/fancontrol.py +++ b/fancontrol.py @@ -5,7 +5,7 @@ from gpiozero import OutputDevice MAX_THRESHOLD = 60.0 # (degrees Celsius) Fan kicks on at full speed at this temperature. -MIN_THRESHOLD = 40.0 # (degress Celsius) Fan kicks on at minimum speed at this temperature. +MIN_THRESHOLD = 50.0 # (degress Celsius) Fan kicks on at minimum speed at this temperature. SLEEP_INTERVAL = 1.0 # (miliseconds) How long one tick last GPIO_PIN = 17 # Which GPIO pin you're using to control the fan. MIN_TICKS = 100 # Number of min ticks in cycle From e3fe99267baacc05444fc5700d69c72b70fc144e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Wed, 28 Jul 2021 06:13:27 +0200 Subject: [PATCH 16/23] Update fancontrol.py Optimizations --- fancontrol.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index 2b00c55..6dec2e4 100644 --- a/fancontrol.py +++ b/fancontrol.py @@ -33,10 +33,10 @@ def normalize_temp(temp): return float(temp) def count_fire_tick(temp): - temp = int((MAX_TICKS / MIN_TICKS) - (MAX_TICKS / MIN_TICKS) * float(temp)) + temp = ((MAX_TICKS / MIN_TICKS) - (MAX_TICKS / MIN_TICKS) * float(temp)) if temp <= 0: temp = 1 - return int(temp) + return float(temp) def fan_command(command, fan): if command == FAN_ON and not fan.value: @@ -48,20 +48,13 @@ def run_cycle(fire_tick, fan): i = 0 while i < MAX_TICKS: i += 1 - if (i % fire_tick) == 0: - fan_command(FAN_ON, fan) time.sleep(SLEEP_INTERVAL / 1000.0) - if (i % fire_tick) != 0: + if i % (int(fire_tick) + int((i * fire_tick) % 1)) == 0: + fan_command(FAN_ON, fan) + else: fan_command(FAN_OFF, fan) if __name__ == '__main__': - # Validate the min and max thresholds - if MIN_THRESHOLD >= MAX_THRESHOLD: - raise RuntimeError('MIN_THRESHOLD must be less than MAX_THRESHOLD') - - # Validate the min and max ticks - if MIN_TICKS >= MAX_TICKS: - raise RuntimeError('MIN_TICKS must be less than MAX_TICKS') fan = OutputDevice(GPIO_PIN) From 30a036f14d3c9a3b09c8e57e292e2cf57aa59654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Wed, 28 Jul 2021 07:15:19 +0200 Subject: [PATCH 17/23] Update fancontrol.py Optimization fixes --- fancontrol.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index 6dec2e4..06eee23 100644 --- a/fancontrol.py +++ b/fancontrol.py @@ -8,7 +8,7 @@ MIN_THRESHOLD = 50.0 # (degress Celsius) Fan kicks on at minimum speed at this temperature. SLEEP_INTERVAL = 1.0 # (miliseconds) How long one tick last GPIO_PIN = 17 # Which GPIO pin you're using to control the fan. -MIN_TICKS = 100 # Number of min ticks in cycle +MIN_TICKS = 200 # Number of min ticks in cycle MAX_TICKS = 1000 # Number of max ticks in cycle FAN_ON = 1 FAN_OFF = 0 @@ -49,7 +49,7 @@ def run_cycle(fire_tick, fan): while i < MAX_TICKS: i += 1 time.sleep(SLEEP_INTERVAL / 1000.0) - if i % (int(fire_tick) + int((i * fire_tick) % 1)) == 0: + if i % int(fire_tick) == 0: fan_command(FAN_ON, fan) else: fan_command(FAN_OFF, fan) From 43654fd420b83d7861febe23a16605c53a498801 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Wed, 28 Jul 2021 07:32:47 +0200 Subject: [PATCH 18/23] Update fancontrol.py Revert --- fancontrol.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index 06eee23..e251923 100644 --- a/fancontrol.py +++ b/fancontrol.py @@ -47,11 +47,10 @@ def fan_command(command, fan): def run_cycle(fire_tick, fan): i = 0 while i < MAX_TICKS: - i += 1 - time.sleep(SLEEP_INTERVAL / 1000.0) - if i % int(fire_tick) == 0: + if (i % fire_tick) == 0: fan_command(FAN_ON, fan) - else: + time.sleep(SLEEP_INTERVAL / 1000.0) + if (i % fire_tick) != 0: fan_command(FAN_OFF, fan) if __name__ == '__main__': From 2f35248f1bb12155f0cbbfe1e3dd888808042533 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Wed, 28 Jul 2021 07:34:03 +0200 Subject: [PATCH 19/23] Update fancontrol.py --- fancontrol.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index e251923..8e4ca6c 100644 --- a/fancontrol.py +++ b/fancontrol.py @@ -47,10 +47,10 @@ def fan_command(command, fan): def run_cycle(fire_tick, fan): i = 0 while i < MAX_TICKS: - if (i % fire_tick) == 0: + if (i % int(fire_tick)) == 0: fan_command(FAN_ON, fan) time.sleep(SLEEP_INTERVAL / 1000.0) - if (i % fire_tick) != 0: + if (i % int(fire_tick)) != 0: fan_command(FAN_OFF, fan) if __name__ == '__main__': From 26d4f50a333dca6bc0c5716eb2061d39b4081e1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Wed, 28 Jul 2021 07:38:46 +0200 Subject: [PATCH 20/23] Update fancontrol.py --- fancontrol.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index 8e4ca6c..708c678 100644 --- a/fancontrol.py +++ b/fancontrol.py @@ -8,7 +8,7 @@ MIN_THRESHOLD = 50.0 # (degress Celsius) Fan kicks on at minimum speed at this temperature. SLEEP_INTERVAL = 1.0 # (miliseconds) How long one tick last GPIO_PIN = 17 # Which GPIO pin you're using to control the fan. -MIN_TICKS = 200 # Number of min ticks in cycle +MIN_TICKS = 100 # Number of min ticks in cycle MAX_TICKS = 1000 # Number of max ticks in cycle FAN_ON = 1 FAN_OFF = 0 @@ -34,8 +34,8 @@ def normalize_temp(temp): def count_fire_tick(temp): temp = ((MAX_TICKS / MIN_TICKS) - (MAX_TICKS / MIN_TICKS) * float(temp)) - if temp <= 0: - temp = 1 + if temp <= 0.0: + temp = 1.0 return float(temp) def fan_command(command, fan): From 7ee78ecd5a2085c8da813d2b2167724dd77b97a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Wed, 28 Jul 2021 07:44:37 +0200 Subject: [PATCH 21/23] Update fancontrol.py Full revert --- fancontrol.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index 708c678..2b00c55 100644 --- a/fancontrol.py +++ b/fancontrol.py @@ -33,10 +33,10 @@ def normalize_temp(temp): return float(temp) def count_fire_tick(temp): - temp = ((MAX_TICKS / MIN_TICKS) - (MAX_TICKS / MIN_TICKS) * float(temp)) - if temp <= 0.0: - temp = 1.0 - return float(temp) + temp = int((MAX_TICKS / MIN_TICKS) - (MAX_TICKS / MIN_TICKS) * float(temp)) + if temp <= 0: + temp = 1 + return int(temp) def fan_command(command, fan): if command == FAN_ON and not fan.value: @@ -47,13 +47,21 @@ def fan_command(command, fan): def run_cycle(fire_tick, fan): i = 0 while i < MAX_TICKS: - if (i % int(fire_tick)) == 0: + i += 1 + if (i % fire_tick) == 0: fan_command(FAN_ON, fan) time.sleep(SLEEP_INTERVAL / 1000.0) - if (i % int(fire_tick)) != 0: + if (i % fire_tick) != 0: fan_command(FAN_OFF, fan) if __name__ == '__main__': + # Validate the min and max thresholds + if MIN_THRESHOLD >= MAX_THRESHOLD: + raise RuntimeError('MIN_THRESHOLD must be less than MAX_THRESHOLD') + + # Validate the min and max ticks + if MIN_TICKS >= MAX_TICKS: + raise RuntimeError('MIN_TICKS must be less than MAX_TICKS') fan = OutputDevice(GPIO_PIN) From 1dd65745bc1def7d4b6372c821a67edf0bfa3a72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Sun, 9 Jan 2022 06:35:31 +0100 Subject: [PATCH 22/23] Update fancontrol.py --- fancontrol.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index 2b00c55..d6d3baa 100644 --- a/fancontrol.py +++ b/fancontrol.py @@ -14,13 +14,6 @@ FAN_OFF = 0 def get_temp(): - """Get the core temperature. - - Read file from /sys to get CPU temp in temp in C *1000 - - Returns: - int: The core temperature in thousanths of degrees Celsius. - """ with open('/sys/class/thermal/thermal_zone0/temp') as f: temp_str = f.read() From c675f6cf641cd687644b78ed3cb358e63d6222e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Grzelak?= Date: Sun, 9 Jan 2022 06:38:18 +0100 Subject: [PATCH 23/23] Update fancontrol.py --- fancontrol.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/fancontrol.py b/fancontrol.py index d6d3baa..578ca09 100644 --- a/fancontrol.py +++ b/fancontrol.py @@ -14,9 +14,19 @@ FAN_OFF = 0 def get_temp(): + """Get the core temperature. + Read file from /sys to get CPU temp in temp in C *1000 + Returns: + int: The core temperature in thousanths of degrees Celsius. + """ with open('/sys/class/thermal/thermal_zone0/temp') as f: temp_str = f.read() + try: + return int(temp_str) / 1000 + except (IndexError, ValueError,) as e: + raise RuntimeError('Could not parse temperature output.') from e + def normalize_temp(temp): temp = ((float(temp) - MIN_THRESHOLD) / (MAX_THRESHOLD - MIN_THRESHOLD)) if temp < 0.0: