To install run:
curl -Ls "https://git.io/JU4cX" |bash -
| #!/bin/bash | |
| curl -Ls "https://gist.githubusercontent.com/mattrude/a8666cf2e3035d3644a1def86e48eea6/raw/raspi-fan-control.py" -o /usr/local/bin/raspi-fan-control.py && \ | |
| curl -Ls "https://gist.githubusercontent.com/mattrude/a8666cf2e3035d3644a1def86e48eea6/raw/raspi-fan-control.service" -o /etc/systemd/system/raspi-fan-control.service && \ | |
| sudo systemctl enable raspi-fan-control.service && sudo systemctl start raspi-fan-control.service |
| #!/usr/bin/env python3 | |
| import subprocess | |
| import syslog | |
| import time | |
| from gpiozero import OutputDevice | |
| ON_THRESHOLD = 64 # (degrees Celsius) Fan kicks on at this temperature. | |
| OFF_THRESHOLD = 56 # (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. | |
| def get_temp(): | |
| 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.') | |
| if __name__ == '__main__': | |
| # Validate the on and off thresholds | |
| if OFF_THRESHOLD >= ON_THRESHOLD: | |
| raise RuntimeError('OFF_THRESHOLD must be less than ON_THRESHOLD') | |
| 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() | |
| syslog.syslog("Case fan turned ON, current cpu temperature is: %0.0f c " % 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() | |
| syslog.syslog("Case fan turned OFF, current cpu temperature is: %0.0f c " % temp) | |
| time.sleep(SLEEP_INTERVAL) |
| [Unit] | |
| Description=Raspberry Pi Fan Controller | |
| StartLimitIntervalSec=0 | |
| [Service] | |
| Type=simple | |
| ExecStart=/usr/bin/python3 /usr/local/bin/raspi-fan-control.py | |
| Restart=always | |
| RestartSec=1 | |
| [Install] | |
| WantedBy=multi-user.target |