Turn On-Off Wifi with Atheros and Cron (archer C5/C7)

Post new topic   Reply to topic    DD-WRT Forum Index -> Atheros WiSOC based Hardware
Goto page 1, 2, 3, 4, 5  Next
Author Message
jean019
DD-WRT User


Joined: 25 Apr 2015
Posts: 124

PostPosted: Sun Oct 04, 2015 18:24    Post subject: Turn On-Off Wifi with Atheros and Cron (archer C5/C7) Reply with quote
I just worked today on an archer C5/C7.
My aim was to turn the wifi off at night during the week end.
Turning on/off the wifi with Atheros hardware is a mess (no "wl" command, only a useless "wl_atheros" command).
"ifconfig ath0 down" alone doesn't cut wifi transmission ("TX power" still indicates a fixed "17 Dbm", not "Radio Off").
I'm not a linux specialist, I searched in the forum. This is a mix of what I found.

I only use ath0 2,4Ghz wifi interface. Process is the same with 5Ghz ath1 . The following command seems to work :

First, create a custom script (put in Admnistration \ Command \ Custom script ) :

Code:

#!/bin/sh
case "$1" in
  wifioff)
    stopservice wland
    nvram set ath0_net_mode=disabled
    ifconfig ath0 down
    killall hostapd
    ;;

  wifion)
    stopservice lan
    nvram set ath0_net_mode=ng-only
    startservice lan
    iwconfig ath0 txpower 12
    ;;

  *)
    echo "$0 {wifioff | wifion}"
esac



Not sure if the last "iwconfig ath0 txpower" is really necessary but I prefer (12dbm for me).
You can replace "ng-only" with the mode you want (choose the value in the GUI, then telnet "nvram show | grep ath0_net_mode" to check the value.

Then add a Cron job in Administration \ Management :
Code:

30 0 * * 0,6 root /tmp/custom.sh wifioff
30 9 * * 0,6 root /tmp/custom.sh wifion


to turn wifi off at 0:30 and on at 9:30 (only during the week end, 0 is sunday, 6 is saturday).
And it works for me.
I'm not sure everything is useful but it works.

Please note : to turn on wifi, you can try "stop/startservice wland", instead of lan.
On the archer C5/C7, it's very slow with wland, 1min30 to turn the wifi on, against 10sec for stop/startservice lan". I don't know why.
Smile


A comparison between some Broadcom and Atheros commands :
Code:

Broadcom         | Atheros
wl assoclist      | wl_atheros assoclist (wlanconfig ath0 list sta)
wl radio up    | ifconfig ath0 up
wl radio down   | ifconfig ath0 down
wl txpwr1 -d n   | iwconfig ath0 txpower n


I took the script from there :
http://superuser.com/questions/380468/is-there-a-persistant-storage-in-router-with-dd-wrt
Sponsor
05dyna
DD-WRT Guru


Joined: 25 Jan 2008
Posts: 968

PostPosted: Tue Dec 22, 2015 20:49    Post subject: Reply with quote
Thanks for resolving this issue. I can confirm this does correct the problem on the TP-Link TL-WR1043ND-v1
jean019
DD-WRT User


Joined: 25 Apr 2015
Posts: 124

PostPosted: Wed Dec 23, 2015 11:43    Post subject: Reply with quote
Smile Smile
ppeter
DD-WRT Novice


Joined: 13 Feb 2009
Posts: 28

PostPosted: Sat Jan 23, 2016 9:16    Post subject: Re: Turn On-Off Wifi with Atheros and Cron (archer C5/C7) Reply with quote
Hello,

Are the nvram sets deliberately not commited ?

Thanks
rololo88
DD-WRT Novice


Joined: 07 Nov 2012
Posts: 7

PostPosted: Mon Feb 22, 2016 6:25    Post subject: Re: Turn On-Off Wifi with Atheros and Cron (archer C5/C7) Reply with quote
jean019 wrote:
I just worked today on an archer C5/C7.
My aim was to turn the wifi off at night during the week end.
Turning on/off the wifi with Atheros hardware is a mess (no "wl" command, only a useless "wl_atheros" command).
"ifconfig ath0 down" alone doesn't cut wifi transmission ("TX power" still indicates a fixed "17 Dbm", not "Radio Off").
I'm not a linux specialist, I searched in the forum. This is a mix of what I found.

I only use ath0 2,4Ghz wifi interface. Process is the same with 5Ghz ath1 . The following command seems to work :

First, create a custom script (put in Admnistration \ Command \ Custom script ) :

Code:

#!/bin/sh
case "$1" in
  wifioff)
    stopservice wland
    nvram set ath0_net_mode=disabled
    ifconfig ath0 down
    killall hostapd
    ;;

  wifion)
    stopservice lan
    nvram set ath0_net_mode=ng-only
    startservice lan
    iwconfig ath0 txpower 12
    ;;

  *)
    echo "$0 {wifioff | wifion}"
esac



Not sure if the last "iwconfig ath0 txpower" is really necessary but I prefer (12dbm for me).
You can replace "ng-only" with the mode you want (choose the value in the GUI, then telnet "nvram show | grep ath0_net_mode" to check the value.

Then add a Cron job in Administration \ Management :
Code:

30 0 * * 0,6 root /tmp/custom.sh wifioff
30 9 * * 0,6 root /tmp/custom.sh wifion


to turn wifi off at 0:30 and on at 9:30 (only during the week end, 0 is sunday, 6 is saturday).
And it works for me.
I'm not sure everything is useful but it works.

Please note : to turn on wifi, you can try "stop/startservice wland", instead of lan.
On the archer C5/C7, it's very slow with wland, 1min30 to turn the wifi on, against 10sec for stop/startservice lan". I don't know why.
Smile


A comparison between some Broadcom and Atheros commands :
Code:

Broadcom         | Atheros
wl assoclist      | wl_atheros assoclist (wlanconfig ath0 list sta)
wl radio up    | ifconfig ath0 up
wl radio down   | ifconfig ath0 down
wl txpwr1 -d n   | iwconfig ath0 txpower n


I took the script from there :
http://superuser.com/questions/380468/is-there-a-persistant-storage-in-router-with-dd-wrt


Thank you very much jean019, your piece of code works perfectly on my TP-Link WDR4310 !! (chinese version of the WDR4300) I just managed differently the cron job to suit me with other schedule, thanks again Wink.
jean019
DD-WRT User


Joined: 25 Apr 2015
Posts: 124

PostPosted: Wed Feb 24, 2016 10:43    Post subject: Reply with quote
Smile
rabrol
DD-WRT Novice


Joined: 27 Jul 2011
Posts: 22

PostPosted: Tue Mar 01, 2016 20:05    Post subject: Re: Turn On-Off Wifi with Atheros and Cron (archer C5/C7) Reply with quote
jean019 wrote:
I just worked today on an archer C5/C7.

Then add a Cron job in Administration \ Management :
Code:

30 0 * * 0,6 root /tmp/custom.sh wifioff
30 9 * * 0,6 root /tmp/custom.sh wifion


to turn wifi off at 0:30 and on at 9:30 (only during the week end, 0 is sunday, 6 is saturday).
And it works for me.


Thanks for this. I've just used this on my DIR-632a to start creating some more complex radio scheduling. It is working really well.

1 question though.
I would like the router to boot with wifi off by default, unless it is power on during one of the times that is specified in the cron job. Is that possible?
Jack82
DD-WRT User


Joined: 26 Jan 2015
Posts: 229

PostPosted: Tue Mar 01, 2016 20:16    Post subject: Re: Turn On-Off Wifi with Atheros and Cron (archer C5/C7) Reply with quote
rabrol wrote:
I would like the router to boot with wifi off by default, unless it is power on during one of the times that is specified in the cron job. Is that possible?

In Services under SES / AOSS / EZ-SETUP / WPS Button:
Turning off radio - Enable
Turn radio off at boot - Enable
rabrol
DD-WRT Novice


Joined: 27 Jul 2011
Posts: 22

PostPosted: Tue Mar 01, 2016 20:48    Post subject: Re: Turn On-Off Wifi with Atheros and Cron (archer C5/C7) Reply with quote
Jack82 wrote:

In Services under SES / AOSS / EZ-SETUP / WPS Button:
Turning off radio - Enable
Turn radio off at boot - Enable


Done! Thank you Smile

I'm guessing that leaves the radio off by default. Let's say cron is set to fire up the wifi at 6pm, but the router doesn't get plugged in until 6.05pm. Will the wifi come on?

That would be my ideal. Router checks time at startup, and enables wifi if it is scheduled to be on via the cron job, but leaves wifi off otherwise.
jean019
DD-WRT User


Joined: 25 Apr 2015
Posts: 124

PostPosted: Wed Mar 02, 2016 0:11    Post subject: Reply with quote
That's one of my issue.
With Tomato, everytime you boot the rooter, the wifi turns on by default, which is good for me (I use the "keep alive" one time per week to be sure the router is ok).

With dd-wrt and my method, reboot doesn't do anything to wifi. If it's off, it stays off.
If cron job is at 6am, and you turns on you router at 6'05, it stays off. Sad

Maybe I have to force the same "wifi on" command to execute at startup...
rabrol
DD-WRT Novice


Joined: 27 Jul 2011
Posts: 22

PostPosted: Tue Mar 15, 2016 17:21    Post subject: Re: Turn On-Off Wifi with Atheros and Cron (archer C5/C7) Reply with quote
Jack82 wrote:
rabrol wrote:
I would like the router to boot with wifi off by default, unless it is power on during one of the times that is specified in the cron job. Is that possible?

In Services under SES / AOSS / EZ-SETUP / WPS Button:
Turning off radio - Enable
Turn radio off at boot - Enable


As it turns out, this didn't work as expected.

Yes, the router would boot with wifi disabled. When the script ran, the wifi light turned on, but the radio didn't. When I SSH'd in to run the script it was the same. The script would run, the radio appeared to turn on, but didn't. In the end I had to disable turning the wifi off at boot time.

Once I set it back to wifi on at boot, the wifi would turn on and off fine with the cron job.

So is there another solution? Router boots, checks the cron schedule, and then enables or disables wifi based on the time?
plazma
DD-WRT User


Joined: 22 Feb 2012
Posts: 68

PostPosted: Wed Jun 22, 2016 12:24    Post subject: Atheros Cron Wifi Schedule Script and BST Fix Reply with quote
I had been trying to schedule the wifi on an 841 v9 unit and none of information i was finding on older posts (i suspected being pre 3.x) worked. That was until I had stumbled onto jean019's solution.

So i thought i would share back my adaptation.

So hows my script different:

- Fixes Daylight saving issues
- Works no matter when the router is powered on
- Turns off wifi at the end of boot

Instead of running two cron scripts at two fixed times, i use a single cron task that runs ever minute. The script then works out if its suppose to turn the wifi on or off and then will only apply the appropriate action if required.

At the end of the script you will see the script actually downs the wifi at boot, this is instead of setting the wireless off (disabled) at boot in the gui. I did this for two reasons, firstly a user commented this didnt work when they tried and also because it makes it easier to deploy the script, ie you get the wifi working (enabled), deploy the script and it takes care of it from there. Also if you were to remove the script and reboot the router the wifi would then default to a working state again.

Note: This script has been updated to 1.4 and the new version can be found here

The startup script:
Code:

# PLAzmA WIFI Schedule Script Version 1.2

# WRITE SCRIPT OUT TO TMP
cat > /tmp/wificheduler.sh << 'EOF'
#!/bin/sh
START=1000
STOP=1800
WIFIMODE=mixed

# FIX FOR UK TIME (REMOVE IF NOT IN UK)
export TZ=GMT0BST,M3.5.0/1,M10.5.0

# IF BOOT HAS NOT COMPLETED DONT RUN
if [ ! -f /tmp/boot_complete ]; then exit 1; fi

# IF TIME IS BEFORE START OR AFTER STOP AND HOSTAPD IS RUNNING, THEN BRING DOWN WIFI
if [ $(date +%H%M) -lt $START ] || [ $(date +%H%M) -gt $STOP ]; then if [ $(ps | grep hostapd | cut -c27-33 | grep -v grep) = hostapd ]; then echo "wifi off">/tmp/wificheduler.status; stopservice wland; nvram set ath0_net_mode=disabled; killall hostapd; mv /tmp/hostap_alive /tmp/hostap_dead; fi; fi

# IF TIME IS AFTER START TIME AND BEFORE END TIME AND WIFI HAS NOT BEEN WOKEN, WAKE IT UP
if [ $(date +%H%M) -gt $START ] && [ $(date +%H%M) -lt $STOP ]; then if [ ! -f /tmp/hostap_alive ]; then echo "wifi  on">/tmp/wificheduler.status; nvram set ath0_net_mode=$WIFIMODE; sleep 10; stopservice lan; startservice lan; mv /tmp/hostap_dead /tmp/hostap_alive; fi; fi

EOF

# SET THE EXECUTE BIT
chmod +x /tmp/wificheduler.sh

# BRING DOWN WIFI ON BOOT
sleep 15
stopservice wland; nvram set ath0_net_mode=disabled; killall hostapd; touch /tmp/hostap_dead; touch /tmp/boot_complete

# DONT FORGET TO ADD THE CRONTAB JOB IN THE GUI (SANS QUOTES) "* * * * * root /bin/sh /tmp/wificheduler.sh"

# TO DEBUG TELNET IN AND RUN (SANS QUOTES) "while true; do cat /tmp/wificheduler.status; sleep 10; done"
# THEN YOU CAN USE "date YYYYMMDDHHMM" IN ANOTHER SESSION TO QUICKLY ADJUST TIME.
# NOTE: wificheduler.status IS TOGGLED AS THE COMMAND STARTS TO RUN AND
# hostapd_(status) IS TOGGLED AT THE END OF THE RUN.
# THUS IT ALLOWS YOU TO DEBUG IF SOMETHING GETS STUCK BY COMPARING THE TWO.

# CHANGE LOG:
# 1.2 ADDED DEBUG, PREVENT CRON SCHEDULE RUN UNTIL BOOT ENDS AND
# ENSURED SCRIPT WOULD STILL RUN IF /tmp BECOMES FULL
# 1.1 CHANGE TO USE SERVICE LAN FOR QUICKER RESTART

If you need daylight saving in another time zone, you can get the required code from: https://wiki.openwrt.org/doc/uci/system

So the boring bit for anyone thats intrested in reading.

Q) Isnt it a bad idea for the script to continue to run once every minute?

A) Not really, when its time to turn wifi on, the script turns the wifi one and then touches a file (a lock) this prevents the script from re-attempting once the wifi has been turned on. When its time to turn the wifi off, the script checks if hostapd is running (as when the wland service is stopped and wifi is disabled hostapd doesn't relaunch). Then once its time and its downed the wifi once, it wont reattempt as hostapd is already dead. Finally when the script turns the wifi off it then removes the lock file so it can then turn the wifi back on later on. Also the script only uses files in /tmp (ram), additionally nvram changes are never committed (permanently written to NVRAM), so it shouldn't put any wear on anything.

Q) Why doesn't your script adjust the tx power or bring down ath0

A) The simple answer, neither is actually required, hostapd knows what the power level is set in the gui, so when it relaunches it sets this accordingly. I had (prior to this version of the script) previously also used ifconfig to bring down ath0, however i found under certain conditions it either wouldn't come back up or if it did hostapd wouldn't be happy and would come up, die and rinse and repeat. Im not 100% if not bringing down the interface means the chip is still technically powered, however when hostapd dies its no longer broadcasting... which is our goal right. You could add back in iwconfig ath0 txpower off; to the turn wifi off command if you want, however i noticed on the 841 unit i was testing on this command didn't actually turn the wifi off, instead it just lowered the power level. Although when i tried this hostapd was still running so maybe it re-adjusted, or its just not possible with my radio chipset.

Q) Why don't you set the wifi to just as disabled in the gui at boot.

A) I answered this above, however when deploying the script its easier to setup the wifi working, test it and then deploy the script. Instead of setup wifi, test it, change it to disabled and then deploy the script. Arguably there is a very short time when the router boots that wifi will become active, but as the times so short its not really a concern as its not long enough to be usable for someone to beat the system and power cycle the router to get usable wifi.

Q) What if i need two or more time slots instead of one.

A) Your going to need to rewrite the script, sorry for my use case i only need one time slot. My suggesting would be to do this is 24 hour long blocks or 48 half hour blocks and then get the script to check if the time was either and active or inactive block. This is sort of similar to way i believe the gui wifi schedule works on broadcom units. If i ever need this i will post the updated script back here, or if someone does please post back and share.

Q) Why wont the script work if i want the wifi enabled during midnight and into the next day.

A) Sorry the scripts designed to work only for a wifi on block inside the same 24 hour period (so day starts with wifi off, turns on for a period and then ends the day with the wifi off again). If you need this you will need to re-write the script, this is down to the way the or and the and statement is written.

Q) Why doesn't it work for me?

A) Did you add the cronjob in the gui, if not do that, also did you enable the ntp client, if not you need to do that, the router needs to know what the time is to make the script work. On the main gui setup page enable the ntp client, set your time zone and enter your time server (for eg pool.ntp.org). If you check both of these and your still having an issue, its possible your units a little different and you will need to adapt the script to your unit. You can also check the debug info in the scripts comments, this should assist a little in tracking down your issue.

Q) Ive tried the script by using run command (instead of saving as a startup script), but it doesn't work.

A) The answer to this is simple the is a qwerk with the run command option. It appears it doesn't respect the 'EOL' command to prevent expanding the shell variables, to fix the script to work with the run command option you must also add an escape character to the shell variables so where you have say $START you must change to \$START (this must be done to all variables).

Q) Why not use startservice wland instead of starting and stopping the lan service.

A) jean019 is right it takes a while for wland to relaunch the hostapd process, where as restarting the lan service brings things back up much quicker in my tests.

I hope this helps others out, as i searched for a workable solution for a long time, at lot of the information out there appears to be pre dd-wrt 3.x and simply doesnt work with modern builds.

Again thanks to jean019 for the original working commands.

PLAzmA


Last edited by plazma on Fri Jun 16, 2017 13:25; edited 4 times in total
cardboardbox
DD-WRT Novice


Joined: 27 Feb 2010
Posts: 22

PostPosted: Sun Jun 26, 2016 22:08    Post subject: Reply with quote
Thanks for the help in this thread. This might be a dumb question but I keep thinking about it... Does turning the radio on and off daily and adjusting TX Power eventually wear out the router faster than if we never changed these settings?
cardboardbox
DD-WRT Novice


Joined: 27 Feb 2010
Posts: 22

PostPosted: Wed Jun 29, 2016 3:10    Post subject: Reply with quote
This is driving me nuts, works fine for ath0, but I cant get ath1 to work correctly...

nvram set ath1_net_mode=acn-only does change the setting because if I telnet in I can see the value of ath1_net_mode but in the dd-wrt wireless basic settings the wireless network mode of ath1 is still disabled and the radio does not seem to be turned on.

Any ideas? Its as if ath1's wireless network mode is not named ath1_net_mode.
jwoods
DD-WRT User


Joined: 13 Mar 2016
Posts: 403

PostPosted: Wed Jun 29, 2016 6:05    Post subject: Reply with quote
cardboardbox wrote:
This is driving me nuts, works fine for ath0, but I cant get ath1 to work correctly...

nvram set ath1_net_mode=acn-only does change the setting because if I telnet in I can see the value of ath1_net_mode but in the dd-wrt wireless basic settings the wireless network mode of ath1 is still disabled and the radio does not seem to be turned on.

Any ideas? Its as if ath1's wireless network mode is not named ath1_net_mode.


Put the text value of the variable in quotes...

nvram set ath1_net_mode="disabled"

nvram set ath1_net_mode="ac-only"

https://www.dd-wrt.com/wiki/index.php/Hardware#NVRAM

There is no acn-only...there is acn-mixed and ac-only.
Goto page 1, 2, 3, 4, 5  Next Display posts from previous:    Page 1 of 5
Post new topic   Reply to topic    DD-WRT Forum Index -> Atheros WiSOC based Hardware All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum