From 561dbf41b4c3a6815428584cf435ce6477304343 Mon Sep 17 00:00:00 2001 From: Boia11 Date: Tue, 10 May 2022 01:04:57 +0200 Subject: [PATCH] First release --- README.md | 21 ++++++++++++++++++-- fancontrol.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ fancontrol.sh | 27 +++++++++++++++++++++++++ script/install | 18 +++++++++++++++++ 4 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 fancontrol.py create mode 100644 fancontrol.sh create mode 100644 script/install diff --git a/README.md b/README.md index 582f569..2130c6d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,19 @@ -# orange-pi-fan-controller -Fan controller for Orange Pi using GPIO +# Orange Pi Fan Controller + +Orange Pi fan controller. + +## Description + +This repository provides scripts that can be run on the Orange Pi that will +monitor the core temperature and start the fan when the temperature reaches +a certain threshold. + +To use this code, you'll have to install a fan. + +The instructions for do that you can be found on this guide for RPi: [Control Your Raspberry Pi Fan (and Temperature) with Python](https://howchoo.com/g/ote2mjkzzta/control-raspberry-pi-fan-temperature-python). + +**THIS SCRIPT IS SET TO CONTROL FAN WITH PIN 9 (Tested on board: Orange Pi 3 LTS)** + +## Requirements + +**This script require [wiringOP](https://github.com/orangepi-xunlong/wiringOP) install on OS.** diff --git a/fancontrol.py b/fancontrol.py new file mode 100644 index 0000000..b65785a --- /dev/null +++ b/fancontrol.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +import os +import sys +import time + +ON_THRESHOLD = 60 #(°C) Fan kicks on at this temperature. +OFF_THRESHOLD = 50 #(°C) Fan shuts off at this temperature. +SLEEP_INTERVAL = 5 #[seconds] How often we check the core temperature. + +# GPIO PIN is 9 using to control the fan. [Physical PIN 16 on board] + +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 + +if __name__ == '__main__': + # Validate the on and off thresholds + if OFF_THRESHOLD >= ON_THRESHOLD: + raise RuntimeError('OFF_THRESHOLD must be less than ON_THRESHOLD') + + os.system('gpio mode 9 out') + os.system('gpio write 9 0') + + + while True: + fan = os.system('gpio read 9') + temp = get_temp() + print(temp) + + # Start the fan if the temperature has reached the limit and the fan + # isn't already running. + # NOTE: `fan` returns 1 for "on" and 0 for "off" + if temp > ON_THRESHOLD and fan==0: + os.system('gpio write 9 1') + # Stop the fan if the fan is running and the temperature has dropped + # to 10 degrees below the limit. + if temp <= OFF_THRESHOLD: + os.system('gpio write 9 0') + + time.sleep(SLEEP_INTERVAL) \ No newline at end of file diff --git a/fancontrol.sh b/fancontrol.sh new file mode 100644 index 0000000..3de5b7c --- /dev/null +++ b/fancontrol.sh @@ -0,0 +1,27 @@ +#! /bin/sh + +### BEGIN INIT INFO +# Provides: fancontrol.py +# Required-Start: $remote_fs $syslog +# Required-Stop: $remote_fs $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +### END INIT INFO + +# Carry out specific functions when asked to by the system +case "$1" in + start) + echo "Starting fancontrol.py" + /usr/local/bin/fancontrol.py & + ;; + stop) + echo "Stopping fancontrol.py" + pkill -f /usr/local/bin/fancontrol.py + ;; + *) + echo "Usage: /etc/init.d/fancontrol.sh {start|stop}" + exit 1 + ;; +esac + +exit 0 diff --git a/script/install b/script/install new file mode 100644 index 0000000..db2f73d --- /dev/null +++ b/script/install @@ -0,0 +1,18 @@ +#! /bin/sh + +set -e + +cd "$(dirname "$0")/.." + +echo "=> Installing fan controller...\n" +sudo cp fancontrol.py /usr/local/bin/ +sudo chmod +x /usr/local/bin/fancontrol.py + +echo "=> Starting fan controller...\n" +sudo cp fancontrol.sh /etc/init.d/ +sudo chmod +x /etc/init.d/fancontrol.sh + +sudo update-rc.d fancontrol.sh defaults +sudo /etc/init.d/fancontrol.sh start + +echo "Fan controller installed."