Fan Control Script for OpenWRT

The LinkSys WRT1900AC router comes with a built in fan. I wanted to find a way to be able to control the fan so that it would run based when certain temperature thresholds were reached.

The Script

#!/bin/sh

## Description: control the fan on a Linksys WRT1900AC router that has OpenWRT installed

# https://forum.openwrt.org/t/automatic-fan-control-linksys-wrt1900ac/223

CPU_TEMP=$(cut -c1-2 /sys/class/hwmon/hwmon2/temp1_input)
DDR_TEMP=$(cut -c1-2 /sys/class/hwmon/hwmon1/temp1_input)
WIFI_TEMP=$(cut -c1-2 /sys/class/hwmon/hwmon1/temp2_input)

CPU_LOW=85
CPU_HIGH=95
DDR_LOW=65
DDR_HIGH=75
WIFI_LOW=100
WIFI_HIGH=115

if [ -d /sys/devices/pwm_fan ];then
    FAN_CTRL=/sys/devices/pwm_fan/hwmon/hwmon0/pwm1
elif [ -d /sys/devices/platform/pwm_fan ];then
    FAN_CTRL=/sys/devices/platform/pwm_fan/hwmon/hwmon0/pwm1
else
    exit 0
fi

if [ "$CPU_TEMP" -ge "$CPU_HIGH" -o "$DDR_TEMP" -ge "$DDR_HIGH" -o "$WIFI_TEMP" -ge "$WIFI_HIGH" ];then
    echo "255" > $FAN_CTRL
    echo "$(date) Fan on High"
elif [ "$CPU_TEMP" -ge "$CPU_LOW" -o "$DDR_TEMP" -ge "$DDR_LOW" -o "$WIFI_TEMP" -ge "$WIFI_LOW" ];then
    echo "100" > $FAN_CTRL
    echo "$(date) Fan on Low"   
else
    echo "0" > $FAN_CTRL
    echo "$(date) Fan off"
fi
iamadmin@media:~/ubuntu-automation/openwrt$ ls -a
.  ..  fan_ctrl.sh
iamadmin@media:~/ubuntu-automation/openwrt$ ls -l 
total 4
-rw-r--r-- 1 iamadmin iamadmin 998 Jan 23  2022 fan_ctrl.sh
Updated: 2023-02-14 | Posted: 2022-01-21