wifiMgr - Tool to automaticaly select an AP(STAtion) from a list of saved APs

The ability to connect to a known set of APs automatically and to make sure the device is available has been a problem under OpenWrt which pains those with travel routers. This post discusses mods to a tool set written by dabyd64 and posted here. Howto Automated Wifi network change if the current fails

The tool set was written to allow a fixed device to cycle through a set of known APs and select one when the signal from the current disappears. Similar but different requirement for a Travel Router. The basic function works well to store and automatically connect to one of the predefined AP(STAtions) in the “config” file. The tool set as written does not address the issue of accessing the device when no AP(STAtion) is available. I have attempted to make some changes to the function, and for the most part these work. There are some issues noted below, and hoping that someone smarter than I may be able to help.

The original tool would ping Google and if a response was received sleep for a period defined in the config file (default 60 sec). When no ping is received it begins to test the AP(STAtions) in the config, in order, until one is found which works and then updates the config to the iface, restarts the wireless and again confirms the operation.

This version of the tool will set the AP(STAtion) to disabled when no ping is received. It also will address an issue which is caused by manually adding an unlisted AP(STAtion), which is to remove the BSSID entry, as the original code as written does not consider this.

The original function added a file to init.d to start the function, however I find that the function starts before the wireless is fully up and will set the AP(STAtion) to disabled on boot. I have moved this to a local startup entry and added a 40 second sleep time (May vary be device what you want here) before starting the wifiMgr. Not really sure this is better than the init.d method.

I have changed the reference to the specific iface, moving it from the (default) first to the second, as when one removes a AP(STAtion) manually, the LAN/WLAN is moved up. I am assuming the LAN/WLAN will always be the same. This is needed to support using a non-listed AP (one off) connection.

I have made some minor changes to the messages inside the wifiMgr.sh file, mostly converting them to English.

Please see the readme.txt for installation instructions.

Other notes:
I have tested this on both AR9331 (AR-150) and a Mediatek MT7620N (similar to the MT300N). Both seem to work the same.

The check runs on start and then every “ConnCheckTimer” period. While I had hoped to set this for a long period, not really expecting to need this to run more than on boot, if the wifi is not up before the check runs, you will have to wait for the defined period until it finds an AP(STAtion).

On occasion there will be a UCI: Not found message in the log. I believe this relates to the tool trying to delete a non-exsistant BSSID.

There is a shell command option to run the wifiMgr to scan all or for a specific AP(STAtion). With the package luci-app-commands you can configure buttons to run the manual force (wifiMgr.sh --force X, where X is a number for a config) to select a specific entry or run through the list. Note that it always reports an error on execution, but does seem to work. It is easier and faster to use preconfigured items from here than using the Netowrk=>Wifi=>Scan to change stations. There is NO data entry.

There is no way, other than manual editing, to move a AP(STAtion) to the config file.

Untested: SSIDs with other than A-Z, 0-9.

The known issues are as follows.
1 - Currently the wifi is bounced at the “ConnCheckTimer” period for about 5-6 seconds when there is no available AP. There is no check to address the situation where no AP(STAtion) is available and the current STA iface is already disabled. The code disables the iface every time. Only an issue if one has no connection and say streaming media (like in a car).

2 - The tool is some how impacting reboot from LuCi. I have to reboot -f from a console. It looks like it reboots, but you will notice that it does not take one back to the login, and if one checks the logs they go back before the reboot.

3 - Somehow starting from rc.local has corrupted the LEDs. I believe it has to do with the parameters related to the rc.local string, as the LEDs operate fine when the wifiMgr init.d script is used. Functionality seems not to be impacted.

4 - If using the init.d wifiMgr script, I am of the opinion that Start and Restart are not working correctly.

Nice to have mods

1 - Add a check for the condition when the existing AP is set to disabled and the no AP found scenario sets the value again and cycles the wifi.

2 - I would like to be able to write the logger messages out to a log file. I can then use this screen mod to put them in a new log tab. OpenWrt Forum Archive

I think it would be better to do a

logread | grep "WifiMgr"

to fetch the logmessages instead of writing it to a new file.

 

Then you have to edit this line from the topic you mentioned:

local openvpnlog = luci.sys.exec("cat /tmp/openvpn.log")

to:

local openvpnlog = luci.sys.exec("logread | grep "WifiMgr"")

Thanks, I don’t really need the file.

I try that and let you know.

Jeroen, thanks to you I have this working.

Is it possible to somehow now remove the wifiMgr messages from the systemlog? It gets a bit busy with the wifiMgr content.

function action_syslog()
local syslog = luci.sys.syslog()
luci.template.render(“admin_status/syslog”, {syslog=syslog})

The logs are generated with the lines with

logger

at the beginning at wifiMgr.sh. So just remove or comment them.

I think already written logs can’t be removed easily.

But isn’t better to leave it intact to be able to analyse any issues easily? As said above you can filter them with “grep” for example.

The wifiMgr results are on the system log and the new AP Connections Log. I would rather them only on the one page. the point was to separate them, but that’s not what I asked for.

If the logs can not be (easily) filtered dynamically, then is there an alternative method to write them to a file? If so, will it be able to display the date and time? Downside?

I have made a couple of changes to the wifiMgr.sh file.

I have added a variable called “PingLocation” to allow the user to easily choose a more geographically desirable location. The default is the Level3 DNS server.

I also added a sleep value just before the first ping test called “PingSleep”. I wanted to eliminate the problem on boot where the tool tries to ping before the network is up. I found that it was testing about 3 seconds early so I am using a value of 5. This gets an AP now on the first try in most cases and saves a minute on boot. I believe the only downside is that in the case of a lost WAN it will take 5 seconds longer to recover.

I still do not understand why the reboot does not work from LuCi. Looking for help on this one.

The second file contains the two files that will add the AP Connections Log tot he Luci Menus.

The wifiMgr scripts use ping and logger to find out the current sta status and try different ssids.

I think there is a better way, to get output from wp_suppliant, which is the auth daemon for sta. But need to ready a lot of docs. This should be the method used in Android phones.

Thanks for your review of this function. I expect that there is a better way to perform these tasks, but I am not a programmer. The code was written by another individual and I have added some features using common sense, copy and past, search and replace.

I have considered “AP Attachment” a glaring gap in OpenWrt’s design, with no apparent resolve in site. If does not consider devices in motion or changing their AP. This is the ONLY solution I have found that actually works, and seems to work well with the exception of the reboot issue. I solved this by adding a “reboot -f” button via luci-app-command. I found this noted in another post, but not sure of it’s implication as I can not find a list of defined reboot options. I would really like to solve this.

The GLI GUI’s tools for AP connection have been a real big improvement in making a travel router usable; however it still fails when there is an ‘old’ AP configured in status 0. While I solved this with a different function that is attached to the reset button and removes the STA, it still requires a web browser to connect. The above is automatic in most cases. The missing piece is adding the current AP to the existing list of APs.

This option is a real must for travel routers. and it should be a a package in OpenWRT

On a travel router install WifiMgr IF you need it. (or at least have the option to un install it if you dont need it)

I have this running on a AR150 that i take with me. Though on another AR150 it does not make sense to have this. (static installation)

If the method that Alfie mentioned is better then that could be awesome.

Ultimately a full integration in the WRT GUI would be even better.

 

I am posting an updated version that corrects the issue with the reboot. Thanks to hostle19 for the fix.

If you are currently using the product please please note that the config file is now in the etc/config/ folder as wifiMgrcfg. Move your config and rename it. The content of this file has NOT changed. Replace the wifiMgr and wifiMgr.sh files with those in this zip. The screen mod files are the same, and do not need to be changed.

TIP: Setting the PingSleep value is important. Test with a known good AP. It needs to be long enough to let the network functionality get up. 5 is probably too short. 10 may be better. Look at your logs and make sure that the connection test finds the internet consistently on boot. If it disables the AP on boot, then PingSleep is not long enough. If the check during boot fails, then you will need to wait an additional minute (ConnCheckTimer value) for you WWAN to come up.

And the luci-app-commands is a great tool to use to execute the 'force" command from inside of LuCi. See the readme.txt file.

 

NOTE: The default OpenWrt wireless has the STA (client) listed first and the Master second. The order needs to be reversed in the wireless file. It relies on the STA interface being the wifi-iface[1]. This is not in the readme.txt file.

 

NOTE: The tool supports selection of a STAtion that is not in the config file. The GLI GUI works with out issue(on AR-150). To do this in LuCi one must:

1 - Delete the existing STAtion (client)

2 - Scan and select a new STAtion.

3 - UNCHECK THE BOX “Replace wireless configuration”.

4 - Enter the wireless key.

5 - Save and apply through the balance of the screens.

Not unchecking the box will replace all the existing wireless config with the new STAtion and will require a LAN cable on the Ethernet to connect on the LAN side.

How could I have missed this post! This is (if it works on a MiFi) exactly what I need :slight_smile:

Will report back later.

An option to use the 3/4G modem when NO Wi-Fi AP is connection would be even better :slight_smile:

Good idea!

Unless the modem is configured as a wireless STAtion, then I think not. There is no logic to do anything or test for anything other than the list of wireless APs, and overwrite the existing config with another.

Line 80 is where the existing wireless STA is set to disabled. It’s the point in the logic that it has been determined that there is no WWAN. You probably would want to connect to the 3g/4g modem here. Line 96 is where a found STAtion is reconfigured. You would probably want to turn off the modem there. The general logic tests for a functioning internet via ping. When a working internet is found APs are not tested, and there is no logic that would say there is a working STAtion that is now in range.

Fixing this is not in my skill set

How one would go from [WWAN <=> WLAN] to [3G\4G <=> WLAN] or visa versa?

Something like this?

ifdown $WAN2 # Shutdown WAN2 ifup $WAN1 # Enable WAN1
Or am I thinking to simple?

If there is a Wifi connection, no 3G/4G is needed (ifdown 3/4G)… As soon as Wifi connection is down, connect 3/4G (ifup 3/4G)… If wifi is back again, ifdown 3/4G… ??

Well, that’s about the kind of answer I was hoping for. Have you tested these strings?

I would expect that the 3g/4g is all configured and that all one needs to do is somehow disable the wifi and enable the 4g. How long does it take the 4g to connect? Is there any user interaction required once it’s configured.

Where is the config for the 3g\4g maintained? can you please post this?

Sorry… Can’t help at the moment. I’m about to go for a 2 weeks holiday :frowning:

The script I used came from the OpenWRT forum, and used it with one of my 150 routers together with a 3G USB stick.

script and info can be found here: OpenWrt Forum Archive

I needed to modify it a little bit to make it work perfect.
My modified code: #!/bin/sh INTERVAL=5PACKETS=1HOST="8.8.4.4"WAN1=wanWAN2=3GUSINGWAN - Pastebin.com
(I used Pastebin as this forum is messing things up with code)

You will still need to set all the things/right as described in the 1st post (see script and info link above)!

So the first question I see from reading through code and readme file is, what happens if your ap doesn’t have a key? Most of the public ones I connect to do not. Do you leave the config out?

Net_key1=“”

Also, will ping work before signing into a captive portal?

 

I love that you don’t need BSSID - that’s what kills the set up for me.

I do not know the answer to either question. Not tested this scenario.

I expect you are right about the key value. I changed the security on my AR150 from the default to none. It changed the encryption to “none” but did not update the “option key” value. The password was still as it was for the encryption, and probably needs to be null or the line deleted. One you can test, the other requires code mods.

The value of the encryption probably needs to be set to

net8_encrypt=“none”

I’m going to test a bit more.

Good thing is i have 1 weaker spare (the HooToo), and a better TPLink that’s my main one (seems faster).

My goal is to avoid having to whip out the laptop for config. Yours seems to cover most (traveling and staying with family, and my cable co hotspots). that covers about 75% of my current need.

Thanks for posting this.