Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # ~ RaspBerry PI ~
- # fan control script
- # -----------------------------------------
- # Author: Bagheera
- # Created: 28.12.2013
- # Last change: 31.12.2013
- # Version 1.2
- # -----------------------------------------
- # 1.2: 31.12.2013
- # - traps for common signals added
- # - commentary
- # - logfile beautification
- #
- # 1.1: 30.12.2013
- # - logfile changed
- # - logturn added
- # - verbose by global setting
- #
- # 1.0: 28.12.2013
- # - control_fan.sh created
- # -----------------------------------------
- #1.3: Author: Snydeaps
- # Aenderung des GPIO von 14 auf 21
- # Aenderung Logfile Ordner auf luefterscript
- test -n "$DEBUG" && set -x
- cmd="$(basename "$0")"
- lockfile="/var/run/$cmd"
- exec 9<>"$lockfile"
- flock -n 9 || {
- echo "ERROR: already running $(cat <&9) - exiting." >&2
- exit 1
- }
- echo $$ >&9
- echo "running as $$"
- sleep 5
- echo "finished $$"
- VERBOSE=1; # show info on console (0=no; 1=yes)
- MAX_THRESH=60; # start above xx degrees
- MIN_THRESH=55; # stop below xx degrees
- SLEEPER=20; # scan every xx seconds
- LOGTURN=6; # log temp every LOGTURN * SLEEPER seconds
- # logfile destination
- LOGFILE="/recalbox/scripts/activecooling/control_fan.log";
- # prepare signal trap function
- function finish {
- echo $(date '+%F %T %Z') "## 0:CONTROL: Script about to stop" >> $LOGFILE
- echo $(date '+%F %T %Z') "## 2:GPIO_INIT: Stopping fan" >> $LOGFILE
- echo "0" > /sys/class/gpio/gpio21/value
- echo $(date '+%F %T %Z') "## 2:GPIO_INIT: Unexport of GPIO port 21" >> $LOGFILE
- echo "21" > /sys/class/gpio/unexport
- echo $(date '+%F %T %Z') "## 0:CONTROL: Going down... bye bye" >> $LOGFILE
- exit;
- }
- # trap exit
- trap finish EXIT
- # initialize GPIO port 21
- if [ ! -e "/sys/class/gpio/gpio21" ]; then
- echo $(date '+%F %T %Z') "## ==================================================================" >> $LOGFILE
- echo $(date '+%F %T %Z') "## 0:CONTROL: Starting up..." >> $LOGFILE
- echo $(date '+%F %T %Z') "## 1:CONFIG: Maximum threshold $MAX_THRESH C" >> $LOGFILE
- echo $(date '+%F %T %Z') "## 1:CONFIG: Minimum threshold $MIN_THRESH C" >> $LOGFILE
- echo $(date '+%F %T %Z') "## 1:CONFIG: Scan interval $SLEEPER sec" >> $LOGFILE
- echo $(date '+%F %T %Z') "## 1:CONFIG: Log temperature interval $(($SLEEPER*$LOGTURN)) sec" >> $LOGFILE
- echo $(date '+%F %T %Z') "## 2:GPIO_INIT: Export of GPIO port 21" >> $LOGFILE
- echo "21" > /sys/class/gpio/export
- echo $(date '+%F %T %Z') "## 2:GPIO_INIT: Direction: out" >> $LOGFILE
- echo "out" > /sys/class/gpio/gpio21/direction
- echo $(date '+%F %T %Z') "## 2:GPIO_INIT: Port 21: init value 0" >> $LOGFILE
- echo "0" > /sys/class/gpio/gpio21/value
- fi
- i=0;
- while [ true ]; do
- # get current recalculated temperature and fan status
- cpuTemp=$(cat /sys/class/thermal/thermal_zone0/temp)
- cpuTemp=$(($cpuTemp/1000))
- fanState=$(cat /sys/class/gpio/gpio21/value)
- # echo current temperature to logfile if logturn met
- i=$(($i+1))
- if [ $i -eq $LOGTURN ]; then
- echo $(date '+%F %T %Z') "## 0:CONTROL: Current temperature: $cpuTemp C" >> $LOGFILE
- i=0;
- fi
- # do some info stuff
- # [ $VERBOSE -eq 1 ] && echo -e "CPU: \033[36m$cpuTemp\033[0mC | FAN: $fanState | THRESHOLD: $MIN_THRESH-$MAX_THRESH"
- # too hot and fan not running? start!
- if [ $cpuTemp -gt $MAX_THRESH ] && [ $fanState -eq 0 ]; then
- echo "1" > /sys/class/gpio/gpio21/value
- [ $VERBOSE -eq 1 ] && echo -e "====> [ \033[32mON\033[0m ]"
- echo $(date '+%F %T %Z') "## 0:CONTROL: Starting fan ($cpuTemp C)" >> $LOGFILE
- fi
- # everything cool and fan running? stop!
- if [ $cpuTemp -lt $MIN_THRESH ] && [ $fanState -eq 1 ]; then
- echo "0" > /sys/class/gpio/gpio21/value
- [ $VERBOSE -eq 1 ] && echo -e "====> [ \033[31mOFF\033[0m ]"
- echo $(date '+%F %T %Z') "## 0:CONTROL: Stopping fan ($cpuTemp C)" >> $LOGFILE
- fi
- sleep $SLEEPER
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement