Skip to content

Instantly share code, notes, and snippets.

@adriantunez
Last active April 21, 2017 22:51
Show Gist options
  • Save adriantunez/45006b1f76be0e3ca60a to your computer and use it in GitHub Desktop.
Save adriantunez/45006b1f76be0e3ca60a to your computer and use it in GitHub Desktop.
Temperature script controller for raspberry pi fan
#! /bin/bash
VERBOSE=1 #Log changes in a log file
LOGFILE=/var/log/temperature-controller.log
TMPTHRES=55 #Temperature threshold in degrees
SLPON=300 #Sleep time
MINH=07 #Threshold hours of work
oldstat=off
curstat=off
while true; do
hour=`date +'%H'`
if [ `echo "$hour < $MINH" | bc` -eq 1 ]; then #Range of silence: [00h - 07h] AM
if [ $curstat == "on" ]; then #If fan is on and we are in range of silence turn it off
gpio mode 0 in
fi
sleep `echo "(60 - \`date +\"%M\"\`)*60" | bc` #Sleep until the next hour
continue
fi
tmp=`/opt/vc/bin/vcgencmd measure_temp | cut -d "=" -f 2 | cut -d "'" -f 1`
if [ `echo "$tmp > $TMPTHRES" | bc` -eq 1 ]; then
gpio mode 0 out #Turn on fan
curstat=on;
else
gpio mode 0 in #Turn off fan
curstat=off
fi
if [[ $VERBOSE -eq 1 && $curstat != $oldstat ]]; then
echo "[`date +'%m/%d/%Y %H:%M'`] fan: $curstat with temperature: $tmp C" >> $LOGFILE
oldstat=$curstat
fi
sleep $SLPON
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment