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.
https://docs.gl-inet.com/en/3/app/ssh/

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?

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.