OpenWrt Forum Archive

Topic: Automatically turn off wireless when not in use

The content of this topic has been archived on 31 Mar 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi

Just thought i would share this.  I like to turn off wireless networking when its not being used to save power and unnecessary radiation so i used this wifi toggle script so i could use the SES button on my Lynksys WRT54G to turn it on and off easily.  It still got left on most of the time though so i made a script to turn it off automatically when its not being used.  It needs to be run on cron every 5 minutes and it will turn the wireless off if no-one has been connected for between 5 and 10 minutes.

#!/bin/sh
if [ $(cat /proc/diag/led/ses_white | tr -d '\n') -eq 1 ] # check if wifi is on
then
  echo "Wifi is on"
  if [ -z "$(wlc assoclist | tr -d '\n')" ] # Check if anyone is connected
  then
  echo "No-one is connected"
    case "$(cat /var/autowifi)" in # check if anyone was connected 5 mins ago
      1) 
        echo "No-one was connected 5 minutes ago so turning off wireless"
        /sbin/woggle
        cat </dev/null > /var/autowifi
      ;;
      *)
        echo "Someone was connected 5 minutes ago so leaving wireless on for now"
        echo 1 > /var/autowifi
      ;;
    esac
  else
    echo "Someone is connected"
  fi
else
  echo "Wifi is already off"
fi

I saved the script at /sbin/autowifi and added the following in crontab:

*/5 * * * * /sbin/autowifi

And you might want to 'touch /var/autowifi' so you don't get errors when it is checked for the first time.  I hope this helps someone.

Thankyou, merlininthewoods. A very useful script. I've adapted it to work in my TP-Link WDR3600, which is dual band. The result follows:

#!/bin/sh

LOG=/tmp/autowifi.log
SW=$(iwconfig wlan0 2>/dev/null | grep IEEE)

echo $(date) >> ${LOG}

if [ -z "$SW" ] # check if wifi is on
then
  echo "Wifi is already OFF" >> ${LOG}
else
  echo "Wifi is ON" >> ${LOG}
  if [ -z "$(iw dev wlan0 station dump | tr -d '\n')" ] 
  then 
  if [ -z "$(iw dev wlan0-1 station dump | tr -d '\n')" ] # Check if anyone is connected
  then
    echo "No-one is connected" >> ${LOG}
    case "$(cat /var/autowifi)" in # check if anyone was connected xx mins ago (defined in cron)
      1) 
        echo "No-one is connected since some time, so turning wireless OFF" >> ${LOG}
        /sbin/wifi down 2>/dev/null
        cat </dev/null > /var/autowifi
      ;;
      *)
        echo "Someone was connected some time ago, so leaving wireless ON for now" >> ${LOG}
        echo 1 > /var/autowifi
      ;;
    esac
  else
    echo "Someone is connected to wlan1 (5 GHz)" >> ${LOG}
    cat </dev/null > /var/autowifi
  fi
  else
    echo "Someone is connected wlan0 (2.4 GHz)" >> ${LOG}
    cat </dev/null > /var/autowifi
  fi
fi

The script should be saved as /sbin/autowifi and made executable with chmod +x /sbin/autowifi. First time a touch /var/autowifi is recommended to made it available. Also, do not forget to edit /etc/crontabs/root and add a line like:

*/15 * * * * /sbin/autowifi

Of course, you may adjust the interval in the crontab as desired.

Thanks for sharing, here is how I did it (inspired by your posts)

I'm using an old "Linksys WRT54G/GS/GL" with
OpenWrt Backfire 10.03.1 (Linux OpenWrt 2.4.37.9), see
http://wiki.openwrt.org/toh/linksys/wrt54g for more info

root@OpenWrt:~# cat /etc/hotplug.d/button/01-radio-toggle

#!/bin/sh

if [ "$BUTTON" = "ses" ] && [ "$ACTION" = "pressed" ] ; then
        ( sleep 1; /sbin/woggle ) &
fi

root@OpenWrt:~# cat /sbin/woggle

#!/bin/sh

LOCKDIR="/var/woggle.lock"

if ! mkdir ${LOCKDIR}; then exit; fi

case "$(uci get wireless.@wifi-iface[0].disabled)" in
        1)
                uci set wireless.@wifi-device[0].disabled=0
                uci set wireless.@wifi-iface[0].disabled=0
                wifi
                echo 1 > /proc/diag/led/ses_white
                echo 1 > /proc/diag/led/ses_orange
        ;;
        *)
                uci set wireless.@wifi-device[0].disabled=1
                uci set wireless.@wifi-iface[0].disabled=1
                wifi
                echo 0 > /proc/diag/led/ses_white
                echo 0 > /proc/diag/led/ses_orange
                echo 2 > /proc/diag/led/wlan
        ;;
esac

sleep 1
rmdir ${LOCKDIR}

root@OpenWrt:~# cat /sbin/autowifi

#!/bin/sh
# Filename: "/sbin/autowifi"
# set cronjob to every 15 minutes:
# */15 * * * * /sbin/autowifi

#LOG=/tmp/autowifi.log
LOG=/dev/null
F=/var/autowifi
LOCKDIR="/var/autowifi.lock"

if ! mkdir ${LOCKDIR}; then
        echo -n $(date) >> ${LOG}; echo "locked" >> ${LOG}
        exit
fi

echo -n $(date) >> ${LOG}; echo " /sbin/autowifi START" >> ${LOG}

if [ ! -e /var/autowifi ]; then
        echo -n "0" > ${F}
fi

if [ $(uci get wireless.@wifi-iface[0].disabled) -eq 0 ]; then # enabled?
        echo -n $(date) >> ${LOG}; echo "  Wifi is ON" >> ${LOG}

        if [ -n "$(wlc assoclist | tr -d '\n')" ]; then # is anyone connected?
                echo -n $(date) >> ${LOG}; echo "  someone is connected" >> ${LOG}
                echo -n "0" > ${F}
        else
                echo -n $(date) >> ${LOG}; echo "  nobody is connected" >> ${LOG}
                x=$(cat ${F})
                if [ $x -ge 4 ]; then  # more than an hour +15 ago?
                        echo -n $(date) >> ${LOG}; echo "    disable" >> ${LOG}
                        /sbin/woggle  # disable wlan
                        echo -n "0" > ${F}
                else
                        echo -n $(date) >> ${LOG}; echo "    wait" >> ${LOG}
                        let x++
                        echo -n "$x" > ${F}
                fi
        fi
fi
echo -n $(date) >> ${LOG}; echo " /sbin/autowifi END" >> ${LOG}

rmdir ${LOCKDIR}

Can any one confirm if this is functional on 14.07?  Thanks.

It's working well on 14.07 with TP-Link TL-WR841N.
I made some modification of scripts above.

#!/bin/sh

LOG=/tmp/autowifi.log
SW=$(iwinfo 2>/dev/null | grep wlan0-1)

echo $(date) >> ${LOG}

if [ -z "$SW" ] # check if wifi is on
then
  echo "Wifi is already OFF" >> ${LOG}
else
  echo "Wifi is ON" >> ${LOG}
  if [ -z "$(iw dev wlan0-1 station dump | tr -d '\n')" ] # Check if anyone is connected
  then
    echo "No-one is connected" >> ${LOG}
    case "$(cat /var/autowifi)" in # check if anyone was connected xx mins ago (defined in cron)
      1) 
        echo "No-one is connected since some time, so turning wireless OFF" >> ${LOG}
    uci set wireless.@wifi-iface[1].disabled=1
    uci commit wireless
        /sbin/wifi 2>/dev/null
        cat </dev/null > /var/autowifi
      ;;
      *)
        echo "Someone was connected some time ago, so leaving wireless ON for now" >> ${LOG}
        echo 1 > /var/autowifi
      ;;
    esac
  else
    echo "Someone is connected to wlan1" >> ${LOG}
    cat </dev/null > /var/autowifi
  fi
fi

Greetings,
Kauczu

Hi guys, i need this script for TP-LINK TD8961nd and for windows. it is possible to do that? sorry for me english wink

In idle mode only beacon frames are transmitted. Default - 10 times per second.
Its very low power

This seems to be a very nice function. Do you have to walk up to the router and press the button to turn wifi back on every time the script turns it off? That would not seem practical to me in a multi person household.
Could someone please elaborate?

The discussion might have continued from here.