FEATURE REQ: WiFi on/off with side switch?

While the alternative solution does break the WiFi, it looks like that first bit might actually be useful. I feel like that might be overkill, as there are already bash scripts in place for the BTN activation/position, but that first script might be handy, if I can make it be in WAP mode instead of Bridge mode. Thank you for that, I think this is another step forward in figuring out how to do this the best way possible.

+Peace be with you.

Could you help?..

Need a “walk-through,” for how to edit scripts (for this topic’s function), as I am (more or less) clueless & new to all of this.

History:

@Nikolas I just recently tried copying and pasting your script as a 100% replacement of the “switchaction” file on my AR150 mini-router.

(Yes, I am completely new to this).

Unfortunately, I wasn’t able to determine that this — alone — worked perfectly, for the desired outcome… — (that desired outcome being, like this thread is named, to have one of my two external buttons be changed, in order now to be able to switch the wifi on or off).

If you don’t mind… :

Could you actually walk me through — (step by step) — describing how to modify this script (or scripts), to get this to function properly?..

This is my first time ever messing around with these codes. So, I need every bit of help (& detail) you can mention.

In fact, it’s seems like a miracle that I successfully SSH’d into my router, and then looked into the files using WinSCP; (etc.)

But, beyond that, I’m pretty clueless as to what the code there (that I’m looking at) actually means; how to modify it; how to test it (and to verify that any modifications are then working); etc.

In case anyone can offer to help, here are the particulars:

(1) Router model = gl-AR150
(2) Firmware = openwrt-ar150-3.100-1218.bin
(3) I just completely re-flashed the above firmware (3.100), in order to “start clean” — (after having somehow disabled[?] my iPhone-tethering function, using WinSCP, just before that)

Would you need to know anything else?..

Any help, out there?..

Many thanks, in advance.
+May God reward you.

First you need to know how to use winscp to edit files on the router.

Then check these files.

/etc/rc.button/BTN_8
This is the file of AR150 switch button. Do not change it. Its contents should be like this:

#!/bin/sh

#This is the switch of AR150, left pressed, release right
# echo "$BUTTON ${ACTION}" > /dev/console
# /etc/init.d/initswitch restart

[ $SEEN -gt 0 ] || exit 0

echo "$BUTTON ${ACTION}" > /dev/console
/usr/bin/switchaction &

/usr/bin/switchaction
This is the script actually run when you toggle the switch. Just read and try to understand this script.

Pls modify as below.
Line 12, pls add

toggle_wifi(){
	local action=$1
	if [ "$action" = "OFF" ];then
		  uci set wireless.@wifi-iface[0].disabled=1

	else
		uci set wireless.@wifi-iface[0].disabled=0
	fi
	uci commit wireless
	wifi
}

image

Line 141 pls add

"wifi")
				toggle_wifi ON
			;;

image

Line 165 pls add

            "wifi")
				toggle_wifi OFF
			;;

image

/etc/config/glconfig
Edit this file

config service 'switch_button'
	option enable '1'
	option function 'wifi'

image

After you modify the 3 files as above, the switch button already works to turn on off wifi.
Left: wifi on
Right: wifi off

Can I get the reward from God?

4 Likes

Hi alzhao,

Thank you for the informative post of yours.
I am hesitating to edit the mentioned files because you are mentioning certain line numbers to insert the proper code but my file does not contais as many code lines.

Could you please clarify for me how can I edit in this case?

Sorry for the lame question :see_no_evil:

Can you just post your file contents?

1 Like

This is how it looks like my /usr/bin/switchaction/ :

#!/bin/sh

check_other_vpn(){
        wg_server=$(uci get wireguard_server.@servers[0].enable)
        ov_server=$(uci get vpn_service.global.enable)
        client=""
        if [ "$1" = "ovpn" ];then
                client=$(uci get wireguard.@proxy[0].enable)
        else
                client=$(uci get  glconfig.openvpn.enable)
        fi
  
        if [ "$wg_server" = "1" -o "$ov_server" = "1" -o "$client" = "1" ];then
                rm /var/lock/switch.lock
                exit 0
        fi

        if [ -f "/etc/config/shadowsocks" ]; then
        	ss_client=$(uci get shadowsocks.@transparent_proxy[0].main_server)
        	[ "$ss_client" != "nil" ] && {
                rm /var/lock/switch.lock
                exit 0
        	}
        fi

        if [ -f "/etc/config/ss-service" ]; then
        	ss_server=$(uci get ss-service.host.enable)
        	[ "$ss_server" = "1" ] && {
                rm /var/lock/switch.lock
                exit 0
        	}
        fi     
}

set_function(){

	# Using lock, avoid restart repeatedly
	LOCK=/var/lock/switch.lock
	if [ -f "$LOCK" ];then
		exit 0
	fi
	touch $LOCK

	model=$(awk -F': ' '/machine/ {print tolower($NF)}' /proc/cpuinfo| cut -d- -f2-)
	switch_left=
	switch_disabled="0"
	switch_enabled=$(uci get glconfig.switch_button.enable)
	switch_func=$(uci get glconfig.switch_button.function)

	case "$model" in
		"ar150")
			switch_left=$(grep -o "BTN_8.*hi" /sys/kernel/debug/gpio)
			;;
		"ar300m")
			switch_left=$(grep -o "left.*hi" /sys/kernel/debug/gpio)
			;;
		"mt300a")
			switch_left=$(grep -o "BTN_1.*hi" /sys/kernel/debug/gpio)
			;;
		"mt300n")
			switch_left=$(grep -o "BTN_0.*hi" /sys/kernel/debug/gpio)
			;;
		"mt300n-v2")
			switch_left=$(grep -o "BTN_0.*hi" /sys/kernel/debug/gpio)
			;;
		"mifi-v3"|\
		"ar750s")
			switch_left=$(grep -o "right.*lo" /sys/kernel/debug/gpio)
			;;
		"ar750")
			switch_left=$(grep -o "sw1.*lo" /sys/kernel/debug/gpio)
			;;
		"*")
			switch_disabled="1"
			;;
	esac

	if [ "$switch_disabled" = "1" ] || [ "$switch_enabled" != "1" ]; then
		rm $LOCK
		exit 0
	fi

	#if switch is on left
	if [ -n "$switch_left" ]; then
		case "$switch_func" in
			"wg")
				check_other_vpn wg
                wgswitch=`uci get wireguard.@proxy[0].enable`
                if [ "$wgswitch" != "1" -o "$wgswitch" != 1 ];then
                        uci set wireguard.@proxy[0].enable='1'
                        uci commit wireguard
                fi
                wgstat=`pidof "wg-crypt-wg0"`
                if [ "$wgstat" = "" -o "$wgstat" = " "  ];then
                        /etc/init.d/wireguard restart
                fi
			;;
			"vpn")
				check_other_vpn ovpn
				vpn_status=$(pidof openvpn)
				vpn_cfg=$(uci get glconfig.openvpn.ovpn)
				if [ -z $vpn_status ] && [ -n "$vpn_cfg" ]; then
					uci set glconfig.openvpn.enable='1'
					uci commit glconfig
					/etc/init.d/startvpn restart
				fi
			;;
			"*")
			;;
		esac
	else
		case "$switch_func" in
			"wg")
				uci set wireguard.@proxy[0].enable='0'
				uci commit wireguard
				/etc/init.d/wireguard stop
			;;
			"vpn")
				vpn_en=$(uci get glconfig.openvpn.enable)
				if [ "$vpn_en" = "1" ]; then
					uci set glconfig.openvpn.enable='0'
					uci commit glconfig
					/etc/init.d/startvpn stop
				fi
			;;
			"*")
			;;
		esac
	fi

	rm $LOCK
}
set_function

Can you pls check what is your firmware version? This seems old.

OpenWrt 18.06.1 r7258-5eb055306f / LuCI openwrt-18.06 branch (git-18.196.56128-9112198)

This an AR-150 mini router.

Sorry I mean on the default UI.

Pls find your model https://dl.gl-inet.com/ and download the latest beta firmware to try.

@alzhao For the AR-150 mini router the firmware 3.017 and 3.024 both show what @Bonami posted from the switchaction file. The upgrade section of the router shows 3.017 as being the stable release for the AR-150.

What is the best way to add the code to get the side switch to turn the wifi on/off?

Thanks.

You’d better change /usr/bin/switchaction/ manually and keep a copy and restore it after upgrade.

Since your example differs from the code in the 3.017 and 3.024 switchaction, I’m not sure what changes need to be made. Can you explain how to make the edits using the code @Bonami provided above?

alzhao, thanks for your code.

Tried your modifications on my ar750s 3.105 and it kind of works. When I reboot the device, the wifi is off by default regardless of the position of the switch. So if I leave it in the on position (right) and reboot, the gui will say wifi is on but it is not. To get wifi on, I have to move the switch to off (left) and then back to on (right). Now the gui is in sync with the actual state of the wifi.

Perhaps some sort of initiation code is needed.

Also, I was hoping your code would add wifi on/off as an additional option in the gui for configuring the function of the switch but it doesn’t. So does the wifi toggle replace the other switch options or works in parallel with them?

I did figure out that if I want both 2.4ghz and 5ghz wifi to be toggled, I need to make this change:

toggle_wifi(){
	local action=$1
	if [ "$action" = "OFF" ];then
		  uci set wireless.@wifi-iface[0].disabled=1
		  uci set wireless.@wifi-iface[1].disabled=1
	else
		uci set wireless.@wifi-iface[0].disabled=0
		uci set wireless.@wifi-iface[1].disabled=0
	fi
	uci commit wireless
	wifi
}

After reboot, can you move the switch on/off one more time and it works?

Also can you send me the system log? ssh to the router and use “logread” to get the log.

Both wifi channels come on if I manually toggle the switch to off and then on. Sometimes maybe one channel comes up after reboot so maybe it’s a timing issue?

Here is my boot log: 2020122_750_log - Pastebin.com

Legend, this is exactly what I was looking for.
I should add that with the very last step, there is already a config service 'switch_button' entry, so that needs to be commented out for anyone who didn’t notice it.

I would like the slide switch to turn wifi on/off on my GL-MT300N-V2-58b running 3.102

What should I have for the whole /usr/bin/switchaction ?

Anything else needed to change?

Thanks.

Hello there :slight_smile:

For those who have the issue that the switch button position is not taken into account on init / start up, here is another way to use your switch button as a wifi toggler .

  1. Connect to your router with ssh :

sh root@ip

  1. Install wifitoggle package :

opkg update && opkg install wifitoggle

(more info : [OpenWrt Wiki] Wi-Fi on/off buttons)

  1. Set these settings for the wifitoggle package :
    note: BTN_8 works for the GL-AR300M, you might need to change it

uci set wifitoggle.@wifitoggle[0].button=‘BTN_8’
uci set wifitoggle.@wifitoggle[0].persistent=‘1’
uci set wifitoggle.@wifitoggle[0].timer=‘0’
uci commit

( 4. should be optional, reboot router :

reboot

)

  1. Verify settings are applied :

uci show wifitoggle

you should see :

root@xxx:~# uci show wifitoggle

wifitoggle.@wifitoggle[0]=wifitoggle
wifitoggle.@wifitoggle[0].led_enable_trigger=‘timer’
wifitoggle.@wifitoggle[0].led_enable_delayon=‘500’
wifitoggle.@wifitoggle[0].led_enable_delayoff=‘500’
wifitoggle.@wifitoggle[0].led_disable_default=‘0’
wifitoggle.@wifitoggle[0].button=‘BTN_8’
wifitoggle.@wifitoggle[0].persistent=‘1’
wifitoggle.@wifitoggle[0].timer=‘0’

and the router button is now a wifi toggle and keeps state after rebooting the router!

1 Like

Sorry to resurrect an ancient thread, but after upgrading my AR300M16 (Shadow) to (beta) firmware 4.3.6 it looks like the way the switch is handled has changed and is now far better and more flexible. A quick forum seach didn’t find a reference to this so thought it worth posting…

Each selectable switch function is now an individual script within the /etc/gl-switch.d/ directory. Adding a new script to the directory will add it to the dropdown list of functions in the
SYSTEM → Toggle Button Settings
menu item. Look at the supplied openvpn.sh and wireguard.sh scripts for the basic calling conventions. So my very simplistic /etc/gl-switch.d/WiFi.sh script to activate / deactivate the primary WiFi AP via the switch is:

#!/bin/sh

action=$1

if [ "$action" = "on" ];then
        uci set wireless.default_radio0.disabled='0'
        uci commit wireless
        wifi reload
fi

if [ "$action" = "off" ];then
        uci set wireless.default_radio0.disabled='1'
        uci commit wireless
        wifi reload
fi

sleep 5

Easily amended to include the Guest SSID etc. List the installed WiFi interface names using
uci show wireless
The state will remain across reboots and is reflected on the Wireless UI panel, although won’t take account of changes made with the system down.
Much better and more straightforward than the old approach and easy to add further selectable switch functions.

V4.3.6 / OpenWrt 22.03.4 just squeezed onto the AR300M16 and working well so far!

1 Like

Nice. Add a logger -p notice -t wifi-toggle "radio(s) disabled" (… & enabled) after wifi reload to enable a touch of logging. I call it a toggle as switch is a lil’ too close to the term network switch.

What’s the purpose of sleep 5?