Last active
April 21, 2017 22:51
-
-
Save adriantunez/45006b1f76be0e3ca60a to your computer and use it in GitHub Desktop.
Temperature script controller for raspberry pi fan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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