Script for a coloring / cooling system temperature-dependent for Raspberry PI
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 | |
# @Author: Horloge-Skynet | |
# @Created: 2014-01-26 | |
# @Created: 2020-04-14 | |
# @Brief: Script for a coloring / cooling system temperature-dependent for Raspberry PI | |
## GPIO path ## | |
GPIO_PATH=/usr/local/bin/gpio | |
## Log file path ## | |
LOG_FILE_PATH=/root/temperatures.log | |
## Pin status ## | |
ON=1 | |
OFF=0 | |
## Pin outputs ## | |
RED_PIN=4 | |
GREEN_PIN=5 | |
FAN_PIN=6 | |
## What time slot do you want ? (Here: from 6:30 AM to 10:30 PM) ## | |
TIME_START='0630' | |
TIME_STOP='2230' | |
## A temperature threshold for LED coloring, and cooling system enabling ## | |
THRESHOLD='38' | |
## A temperature limit for cooling system enabling (even if this is the night and you're asleep !) ## | |
LIMIT='50' | |
## If the system has just started ('uptime' < 2 * 60 seconds), let's define ours GPIO pin modes ## | |
if [[ $(cut -d . -f 1 < /proc/uptime) -lt 120 ]]; then | |
"$GPIO_PATH" mode ${RED_PIN} out # Red LED | |
"$GPIO_PATH" mode ${GREEN_PIN} out # Green LED | |
"$GPIO_PATH" mode ${FAN_PIN} out # Fan | |
fi | |
## Let's get some temperatures values from the system ## | |
cpu_temp="$(cat /sys/class/thermal/thermal_zone0/temp)"; cpu_temp=$((cpu_temp / 1000)) | |
gpu_temp=$(/opt/vc/bin/vcgencmd measure_temp); gpu_temp=${gpu_temp:5:2} | |
if [[ ${cpu_temp} -gt ${THRESHOLD} || ${gpu_temp} -gt ${THRESHOLD} ]]; then | |
"$GPIO_PATH" write ${GREEN_PIN} ${OFF} # Shutdown the green LED | |
"$GPIO_PATH" write ${RED_PIN} ${ON} # Light up the red LED | |
## Get the current time ## | |
currentTime=$(date +%H%M) | |
## If we're in the time slot, or if the temperature has reached the maximum limit, let's enable the cooling system ## | |
if [[ ($((10#${currentTime})) -gt $((10#${TIME_START})) && $((10#${currentTime})) -lt $((10#${TIME_STOP}))) || (${cpu_temp} -gt ${LIMIT} || ${gpu_temp} -gt ${LIMIT}) ]]; then | |
"$GPIO_PATH" write ${FAN_PIN} ${ON} # Switch on the fan | |
fi | |
# Save the data into a log file | |
echo -e "${currentTime} --> CPU: ${cpu_temp} \\xc2\\xb0C - ${gpu_temp} \\xc2\\xb0C" >> "$LOG_FILE_PATH" | |
else | |
"$GPIO_PATH" write ${FAN_PIN} ${OFF} # Switch off the fan | |
"$GPIO_PATH" write ${RED_PIN} ${OFF} # Shutdown the red LED | |
"$GPIO_PATH" write ${GREEN_PIN} ${ON} # Light up the green LED | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment