Advertisement
nidico

active cooling script recalbox

Jul 21st, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.17 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # ~ RaspBerry PI ~
  4. # fan control script
  5. # -----------------------------------------
  6. # Author:      Bagheera
  7. # Created:     28.12.2013
  8. # Last change: 31.12.2013
  9. # Version      1.2
  10. # -----------------------------------------
  11. # 1.2: 31.12.2013
  12. #      - traps for common signals added
  13. #      - commentary
  14. #      - logfile beautification
  15. #
  16. # 1.1: 30.12.2013
  17. #      - logfile changed
  18. #      - logturn added
  19. #      - verbose by global setting
  20. #
  21. # 1.0: 28.12.2013
  22. #      - control_fan.sh created
  23. # -----------------------------------------
  24. #1.3: Author: Snydeaps
  25. #   Aenderung des GPIO von 14 auf 21
  26. #   Aenderung Logfile Ordner auf luefterscript
  27.  
  28.  
  29. test -n "$DEBUG" && set -x
  30.  
  31. cmd="$(basename "$0")"
  32. lockfile="/var/run/$cmd"
  33.  
  34. exec 9<>"$lockfile"
  35.  
  36. flock -n 9 || {
  37.   echo "ERROR: already running $(cat <&9) - exiting." >&2
  38.   exit 1
  39. }
  40.  
  41. echo $$ >&9
  42.  
  43. echo "running as $$"
  44. sleep 5
  45. echo "finished $$"
  46.  
  47. VERBOSE=1;             # show info on console (0=no; 1=yes)
  48.  
  49. MAX_THRESH=60;         # start above xx degrees
  50. MIN_THRESH=55;         # stop below xx degrees
  51.  
  52. SLEEPER=20;            # scan every xx seconds
  53. LOGTURN=6;             # log temp every LOGTURN * SLEEPER seconds
  54.  
  55. # logfile destination
  56. LOGFILE="/recalbox/scripts/activecooling/control_fan.log";
  57.  
  58.  
  59. # prepare signal trap function
  60. function finish  {
  61.     echo $(date '+%F %T %Z') "##     0:CONTROL:       Script about to stop" >> $LOGFILE
  62.     echo $(date '+%F %T %Z') "##     2:GPIO_INIT:     Stopping fan" >> $LOGFILE
  63.     echo "0" > /sys/class/gpio/gpio21/value
  64.     echo $(date '+%F %T %Z') "##     2:GPIO_INIT:     Unexport of GPIO port 21" >> $LOGFILE
  65.     echo "21" > /sys/class/gpio/unexport
  66.     echo $(date '+%F %T %Z') "##     0:CONTROL:       Going down... bye bye" >> $LOGFILE
  67.     exit;
  68. }
  69.  
  70. # trap exit
  71. trap finish EXIT
  72.  
  73. # initialize GPIO port 21
  74. if [ ! -e "/sys/class/gpio/gpio21" ]; then
  75.     echo $(date '+%F %T %Z') "##     ==================================================================" >> $LOGFILE
  76.     echo $(date '+%F %T %Z') "##     0:CONTROL:       Starting up..." >> $LOGFILE
  77.     echo $(date '+%F %T %Z') "##     1:CONFIG:        Maximum threshold $MAX_THRESH C" >> $LOGFILE
  78.     echo $(date '+%F %T %Z') "##     1:CONFIG:        Minimum threshold $MIN_THRESH C" >> $LOGFILE
  79.     echo $(date '+%F %T %Z') "##     1:CONFIG:        Scan interval $SLEEPER sec" >> $LOGFILE
  80.     echo $(date '+%F %T %Z') "##     1:CONFIG:        Log temperature interval $(($SLEEPER*$LOGTURN)) sec" >> $LOGFILE
  81.     echo $(date '+%F %T %Z') "##     2:GPIO_INIT:     Export of GPIO port 21" >> $LOGFILE
  82.     echo "21" > /sys/class/gpio/export
  83.     echo $(date '+%F %T %Z') "##     2:GPIO_INIT:     Direction: out" >> $LOGFILE
  84.     echo "out" > /sys/class/gpio/gpio21/direction
  85.     echo $(date '+%F %T %Z') "##     2:GPIO_INIT:     Port 21: init value 0" >> $LOGFILE
  86.     echo "0" > /sys/class/gpio/gpio21/value
  87. fi
  88.  
  89. i=0;
  90. while [ true ]; do
  91.  
  92.     # get current recalculated temperature and fan status
  93.     cpuTemp=$(cat /sys/class/thermal/thermal_zone0/temp)
  94.     cpuTemp=$(($cpuTemp/1000))
  95.     fanState=$(cat /sys/class/gpio/gpio21/value)
  96.  
  97.     # echo current temperature to logfile if logturn met
  98.     i=$(($i+1))
  99.     if [ $i -eq $LOGTURN ]; then
  100.         echo $(date '+%F %T %Z') "##     0:CONTROL:       Current temperature: $cpuTemp C" >> $LOGFILE
  101.         i=0;
  102.     fi
  103.  
  104.     # do some info stuff
  105.     # [ $VERBOSE -eq 1 ] && echo -e "CPU: \033[36m$cpuTemp\033[0mC  |  FAN: $fanState  |  THRESHOLD: $MIN_THRESH-$MAX_THRESH"
  106.  
  107.     # too hot and fan not running? start!
  108.     if [ $cpuTemp -gt $MAX_THRESH ] && [ $fanState -eq 0 ]; then
  109.         echo "1" > /sys/class/gpio/gpio21/value
  110.         [ $VERBOSE -eq 1 ] && echo -e "====> [ \033[32mON\033[0m ]"
  111.         echo $(date '+%F %T %Z') "##     0:CONTROL:       Starting fan ($cpuTemp C)" >> $LOGFILE
  112.     fi
  113.  
  114.     # everything cool and fan running? stop!
  115.     if [ $cpuTemp -lt $MIN_THRESH ] && [ $fanState -eq 1 ]; then
  116.         echo "0" > /sys/class/gpio/gpio21/value
  117.         [ $VERBOSE -eq 1 ] && echo -e "====> [ \033[31mOFF\033[0m ]"
  118.         echo $(date '+%F %T %Z') "##     0:CONTROL:       Stopping fan ($cpuTemp C)" >> $LOGFILE
  119.     fi
  120.  
  121.     sleep $SLEEPER
  122.  
  123. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement