Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
Boia11 committed May 9, 2022
1 parent 1e83689 commit 561dbf4
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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).

**<u>THIS SCRIPT IS SET TO CONTROL FAN WITH PIN 9 (Tested on board: Orange Pi 3 LTS)</u>**

## Requirements

**This script require [wiringOP](https://github.com/orangepi-xunlong/wiringOP) install on OS.**
53 changes: 53 additions & 0 deletions fancontrol.py
Original file line number Diff line number Diff line change
@@ -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)
27 changes: 27 additions & 0 deletions fancontrol.sh
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions script/install
Original file line number Diff line number Diff line change
@@ -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."

0 comments on commit 561dbf4

Please sign in to comment.