Automatic fan control - Linksys WRT1900AC

Automatic fan control

Runs as a daemon, MUCH FASTER than a cron job

Create two files "fan_control" "fan_monitor" and UL them into the proper dir(s)

/etc/init.d/fan_control
All this script file does is start/stop the daemon and auto-start up at bootup

/usr/sbin/fan_monitor
All this script file does is to monitor temperatures and turn on/off the fan

Delete the stock cron job for fan control.

start/stop from the command line

/etc/init.d/fan_control start
/etc/init.d/fan_control stop

fan_control

#!/bin/sh /etc/rc.common

# put in /etc/init.d/fan_control

START=99

PIDFILE=/var/run/fan_monitor_pid

start() {
    start-stop-daemon -b -S -x /usr/sbin/fan_monitor -m -p $PIDFILE
}

stop() {
    start-stop-daemon -K -p $PIDFILE
}

fan_monitor

#!/bin/sh

# put in /usr/sbin/fan_monitor
# fan_monitor
# Utility script to monitor temperatures and turn on/off the fan in accordance
# with the original Belkin specs.

INTERVAL=30 # Poll every 30 seconds

# The fan will turn on if any of these temperatures is exceeded
CPU_ON=58   # Belkin default is 85
DDR_ON=48   # Belkin default is 65
WIFI_ON=51  # Belkin default is 105

# The fan will turn off if all of the temperatures are below these values
CPU_OFF=80  # Belkin default is 80
DDR_OFF=60  # Belkin default is 60
WIFI_OFF=70 # Belkin default is 100

old_pwm=-1

fan_set() {
    if $1; then
        pwm=255
    else
        pwm=0
    fi
    if [ "$pwm" -ne "$old_pwm" ]; then
        echo "Setting fan PWM to: $pwm"
        echo "$pwm" > /sys/devices/platform/pwm_fan/hwmon/hwmon0/pwm1
        old_pwm=$pwm
    fi
}

# Turn fan off when exiting
trap "{ fan_set false; exit; }" SIGINT SIGTERM

# Main fan control loop
while true
do
    cpu=`cat /sys/class/hwmon/hwmon2/temp1_input`
    ddr=`cat /sys/class/hwmon/hwmon1/temp1_input`
    wifi=`cat /sys/class/hwmon/hwmon1/temp2_input`

# Divide by 1000 to get temp values in C
    cpu=`expr $cpu / 1000`
    ddr=`expr $ddr / 1000`
    wifi=`expr $wifi / 1000`

    if [ "$cpu" -ge $CPU_ON \
        -o "$ddr" -ge $DDR_ON \
        -o "$wifi" -ge $WIFI_ON ]
    then
        fan_set true
    elif [ "$cpu" -le $CPU_OFF \
        -a "$ddr" -le $DDR_OFF \
        -a "$wifi" -le $WIFI_OFF ]
    then
        fan_set false
    fi

    sleep $INTERVAL &
    wait %1
done

Tip

Just use this code, run it from the command line. it will show REAL-TIME info.

Much easer to set the thresholds.

while true; do a=`cat /sys/class/hwmon/hwmon2/temp1_input`; b=`cat /sys/class/hwmon/hwmon1/temp1_input`; c=`cat /sys/class/hwmon/hwmon1/temp2_input`; d=`date +"%H:%M:%S"`; f=`cat /sys/devices/platform/pwm_fan/hwmon/hwmon0/pwm1`; echo $d cputemp=$(($a/1000))" "ddrtemp=$(($b/1000))" "wifitemp=$(($c/1000)) fanstatus=$f;sleep 1; done
2 Likes

Thanks for this! Happy new year

EDIT:
aren't these odd, your default "ON" values?

The fan will turn on if any of these temperatures is exceeded

CPU_ON=58 # Belkin default is 85
DDR_ON=48 # Belkin default is 65
WIFI_ON=51 # Belkin default is 105

The fan will turn off if all of the temperatures are below these values

CPU_OFF=80 # Belkin default is 80
DDR_OFF=60 # Belkin default is 60
WIFI_OFF=70 # Belkin default is 100

Without knowing the device in question, nor about eventual sensor quirks, but Belkin's defaults appear to be a bit high and quite taxing on the hardware.

You may want to take a look at my modified @gufus scripts. That is how I run my mamba, inside a cabinet, and the fan pretty much stays at 50%, unless under load and then I have never seen it go above 75%. I added some granularity, and temperature variance.

Edit: Here

1 Like

I agree, but looking at the default values here, the fan would:
turn on at 58 and off when below 80
turn on at 48 and off when below 60
turn on at 51 and off when below 70

All of the on values are technically under the off value?
Am I missing something?

Would you be able to upload a copy to your usual spot? Your files.tar doesn't have anything script wise in it for the fan control, and the only other way I see would be flashing your actual image.

EDIT: Thanks, got it! Looks much better.

I am working off a base of these files, and a copy from @anomeome

So far so good, but I have one more question :slight_smile: It works great as long as I issue the "start" command.

How are you tackling auto starting this at boot (reboot)? Simply by placing the fan_control in /etc/init.d ? Don't we need to execute our actual command "start"?

Where are you guys adding "/etc/init/d/fan_control start" during boot? (in /etc/rc.common as is in the header of fan_control ?)

EDIT: N/M got it, https://wiki.openwrt.org/doc/techref/initscripts

Seems we need to make a S and a K symlink from /etc/rc.d back to ../init.d/fan_control to cleanly start at boot and stop and shutdown

EDIT2: /etc/init.d/fan_control enable :wink:

Wow... Hey guys. Can someone make a simple install program to add fan control and see it on the LEDE's "Overview" and program setups somewhere in the "System" tabs. PLEASE! Don't make it confusing for us non-Linux folk.
The simpler you make it, the more people will use it.