Mode button to toggle wifi on/off

Hello,

Like probably most of the MikroTik users willing to learn scripting, I have exercised myself with a script dedicated to toggling wifi on/off through pressing the Mode button (Router OS v7.20.6). I have tested it and it works for me. I consider that wifi is on if at least one interface is active.

Since it was made in order to learn, I am open to any constructive criticism

Activation_désactivation_wifi

:local Wifi1Inactivity [/interface get wifi1 disabled]

:local Wifi2Inactivity [/interface get wifi2 disabled]

:if ($Wifi1Inactivity = true and $Wifi2Inactivity = true) do={
    /interface wifi enable wifi1
    /interface wifi enable wifi2
    :log info "Activation du wifi"
} else={
    /interface wifi disable wifi1
    /interface wifi disable wifi2
    :log info "Désactivation du wifi"
}

I assigned it to the Mode button through the GUI.

EDIT : I have another wifi interface dedicated to iot, but it doesn’t seem to be autonomous, probably because it is a slave of wifi2.

You disable/enable interface called wifiN, so you disable/enable all SSIDs it tramsits

1 Like

Since you're merging the state of all interfaces, you might want to store it in a shared variable, like this:
:local AllWifiInactivity ([/interface get wifi2 disabled] and [/interface get wifi1 disabled]) or no variables at all
:if ([/interface get wifi1 disabled] and [/interface get wifi2 disabled]) do={

You can enable and disable it with one line
/interface wireless enable wifi1,wifi2
Virtual interfaces can be controlled by number
interface/wireless/enable numbers=3,4

My commands use wireless because I'm using that package

1 Like

Maybe useful, maybe not, you could also switch on/off the user led to have It signal whether wifi Is on or off, JFYI:

1 Like

Very interesting, I’ll give it a go. Thank you!

One addition you could do is use a [find ssid="yourSSID2Toggle"], for example, instead of referencing the interface name directly. The general reason being is if you find in scripting, you can abstract things like interface name so you’re “safe” from folks renaming the interfaces…

1 Like

So here is the resulting script

:if ([/interface get wifi1 disabled] and [/interface get wifi2 disabled]) do={
    /interface wifi enable [/interface wifi find configuration.ssid="MikroTik-xx"]
    :log info "Activation du wifi"
} else={
    /interface wifi disable [/interface wifi find configuration.ssid="MikroTik-xx"]
    :log info "Désactivation du wifi"
}

EDIT : I realise that I should have avoided hard-scripting wifi1 and wifi2 in the condition. I suppose I might have used a for loop. I’ll try and conceive it.

Yep, but the link I posted was intended to let you know about the clever trick by rextended, your script checks if something is disabled and if it is, turns it on, or, if it is enabled, and then turns it off.

The linked approach works (IMHO more directly) with the opposite of the status found:

2 Likes

Thanks for that precision. I didn’t understand initially.

I will think about it but I’m not sure it fits my case, since there are 2 wifi interfaces and I want to deactivate both even if only one is active, or reactivate them both if none is active.

That’s what I get, at this stage without taking into account the proposal of @jaclaz

:local WifiInactive true

:foreach i in [/interface wifi find configuration.ssid="MikroTik-xx"] do={ :if ([/interface get $i disabled] = false) do={ :set WifiInactive false }}

if ($WifiInactive = true) do={
    /interface wifi enable [/interface wifi find configuration.ssid="MikroTik-xx"]
    :log info "Activation du wifi"
} else={
    /interface wifi disable [/interface wifi find configuration.ssid="MikroTik-xx"]
    :log info "Désactivation du wifi"
}

EDIT : this one avoids repeating several times the same operation

:local WifiInactive true

:local WIFI [/interface wifi find configuration.ssid="MikroTik-xx"]

:foreach i in $WIFI do={ :if ([/interface get $i disabled] = false) do={ :set WifiInactive false }}

if ($WifiInactive = true) do={
    /interface wifi enable $WIFI
    :log info "Activation du wifi"
} else={
    /interface wifi disable $WIFI
    :log info "Désactivation du wifi"
}

Well, you are taking two separate items but want to manage them as if they were a single entity.

You can virtually "group" the two items under a single third item.

If the third single item (the user led, in the example) is enabled, set both wifi1 and 2 enabled, if it is disabled set both wifi1 and 2 enabled.

I has to be tested/*checked, but something loosely like:

/system leds find [:if ($leds=[:toarray "user-led"]) do={
set $".id" type="on" disabled=(!$disabled)
/interface wifi set disabled=(!$disabled) [/interface wifi find configuration.ssid="MikroTik-xx"]
:log info "Wifi disabled=(!$disabled)"
}]

might do.

Only ideas ...

This script monitors the state of button presses and is not connected to the state of WiFi. WiFi can be changed programmatically, and then the logic of button presses will be reversed.

I know.

I was just throwing ideas on the table.

That is only good if you ONLY manage the wifi through the mode button.

But you can have instead a different script running periodically that checks the status of the led and synchronizes the two wifi interfaces to that.

As said, only ideas ...