Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 141 lines (112 sloc) 3.48 KB
#!/bin/bash
#
# Automatic fan control via GPIO
#
# Note:
# This script must be executed by root in order to be able
# to initialize the GPIO
#
#
# This source file is part of the follwoing repository:
# http://www.github.com/microfarad-de/nastia-server
#
# Please visit:
# http://www.microfarad.de
# http://www.github.com/microfarad-de
#
# Copyright (C) 2019 Karim Hraibi (khraibi@gmail.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Current directory where this script is located
DIR=$(dirname $(readlink -f "$BASH_SOURCE"))
# Include common configuration file
source "$DIR/common.sh"
THRESHOLD="$CFG_FAN_TEMPERATURE" # temparature threshold in degrees Celsius
INTERVAL="$CFG_FAN_INTERVAL" # temperature sampling interval in seconds
LOG="$CFG_LOG_DIR/fan.log" # Main log file
GPIO_PIN="$CFG_FAN_GPIO_PIN" # GPIO pin connected to the fan
# Print info message to log file
function infoLog {
_infoLog "$1" "fan" "$LOG" "ed+"
}
# Print warning message to log file
function warningLog {
_warningLog "$1" "fan" "$LOG" "ecd"
}
# Print error message to log file
function errorLog {
_errorLog "$1" "fan" "$LOG" "ecd"
exit 1
}
#################
#### START ####
#################
infoLog "fan control service started"
# Initialize GPIO
if [[ ! -e "/sys/class/gpio/gpio$GPIO_PIN" ]]; then
echo "$GPIO_PIN" > "/sys/class/gpio/export"
rv=$?
if [[ $rv -ne 0 ]]; then
errorLog "failed to initialize GPIO pin $GPIO_PIN (exit code $rv)"
else
infoLog "initialized GPIO pin $GPIO_PIN"
fi
else
infoLog "GPIO pin $GPIO_PIN already initialized"
fi
DIRECTION=$(cat "/sys/class/gpio/gpio$GPIO_PIN/direction")
# Set the GPIO direction
if [[ $DIRECTION != "out" ]]; then
echo "out" > "/sys/class/gpio/gpio$GPIO_PIN/direction"
rv=$?
if [[ $rv -ne 0 ]]; then
errorLog "failed to set GPIO pin $GPIO_PIN direction (exit code $rv)"
else
infoLog "GPIO pin $GPIO_PIN direction set to 'out'"
fi
else
infoLog "GPIO pin $GPIO_PIN direction already set to 'out'"
fi
# Periodically poll the CPU temperature
while true; do
TEMP=$(vcgencmd measure_temp | cut -c6,7)
rv=$?
if [[ $rv -ne 0 ]]; then
errorLog "failed to read temperature value (exit code $rv)"
fi
STATUS=$(cat "/sys/class/gpio/gpio$GPIO_PIN/value")
rv=$?
if [[ $rv -ne 0 ]]; then
errorLog "failed to read the status of GPIO pin $GPIO_PIN (exit code $rv)"
fi
if [[ $TEMP -ge $THRESHOLD && $STATUS -eq 0 ]]; then
echo "1" > "/sys/class/gpio/gpio$GPIO_PIN/value"
rv=$?
if [[ $rv -ne 0 ]]; then
errorLog "failed to turn on GPIO pin $GPIO_PIN (exit code $rv)"
else
infoLog "start ($TEMP'C)"
fi
elif [[ $TEMP -lt $THRESHOLD && $STATUS -eq 1 ]]; then
echo "0" > "/sys/class/gpio/gpio$GPIO_PIN/value"
rv=$?
if [[ $rv -ne 0 ]]; then
errorLog "failed to turn off GPIO pin $GPIO_PIN (exit code $rv)"
else
infoLog "stop ($TEMP'C)"
fi
fi
sleep $INTERVAL
done
You can’t perform that action at this time.