Skip to content

Instantly share code, notes, and snippets.

@enwi
Last active November 17, 2019 13:55
Show Gist options
  • Save enwi/27ec1855638dbb520ea26dc883752f2f to your computer and use it in GitHub Desktop.
Save enwi/27ec1855638dbb520ea26dc883752f2f to your computer and use it in GitHub Desktop.
Little shell script that reads the temperature of a raspberry pi cpu and a connected harddrive using smartctl and controls a GPIO pin where a fan should be connected.
#!/bin/bash
c_FAN=26 # gpio pin the fan is connected to
c_MIN_TEMPERATURE=45 # temperature in degrees c when fan should turn on
c_TEMPERATURE_OFFSET=2 # temperarute offset in degrees c when fan should turn off
c_HARDDRIVE="sda" # name of your harddrive
temperature_offset=$(( $c_MIN_TEMPERATURE - $c_TEMPERATURE_OFFSET ))
fan_state=0
echo "$c_FAN" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$c_FAN/direction
while [ 1 ]; do
cpu_temperature=$(cat /sys/class/thermal/thermal_zone0/temp)
cpu_temperature=$(( $cpu_temperature / 1000 ))
hdd_temperature=$(sudo smartctl -d sat --all /dev/$c_HARDDRIVE | grep Temperature_Celsius | awk '{print $10}')
if [ !fan_state ] && ([ $cpu_temperature -gt $c_MIN_TEMPERATURE ] || [ $hdd_temperature -gt $c_MIN_TEMPERATURE ])
then
fan_state=1
echo "1" > /sys/class/gpio/gpio$c_FAN/value
elif [ fan_state ] && [ $cpu_temperature -lt $temperature_offset ] && [ $hdd_temperature -lt $temperature_offset ]
then
fan_state=0
echo "0" > /sys/class/gpio/gpio$c_FAN/value
fi
/bin/sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment