Page 1 of 3

Wifi Reconnect on drop

Posted: Thu Aug 30, 2012 7:22 pm
by colin79666
Hi,

Quite please with myself. Recently go my Pi and this evening managed to get the Edimax ew-7811un working with it (after having issues removing another adaptor I had tried).

Anyway the only slight issue I have is if the wifi drops (e.g. router reboots) then it doesn't auto reconnect. I have to unplug the adaptor and plug it back in again. As I normally connect by SSH rather than keyboard/monitor I can't just restart the networking service. Obviously I can't go unplugging/reconnecting the adaptor if I'm not at home when it drops.

Any ideas on how to force it to keep retrying the access point if it disappears for a few seconds?

Thanks!

Re: Wifi Reconnect on drop

Posted: Thu Aug 30, 2012 7:55 pm
by Nr90
I think the easiest way is installing a network manager like wicd

Re: Wifi Reconnect on drop

Posted: Mon Sep 03, 2012 6:02 pm
by MrEngman
colin79666 wrote:Hi,

Quite please with myself. Recently go my Pi and this evening managed to get the Edimax ew-7811un working with it (after having issues removing another adaptor I had tried).

Anyway the only slight issue I have is if the wifi drops (e.g. router reboots) then it doesn't auto reconnect. I have to unplug the adaptor and plug it back in again. As I normally connect by SSH rather than keyboard/monitor I can't just restart the networking service. Obviously I can't go unplugging/reconnecting the adaptor if I'm not at home when it drops.

Any ideas on how to force it to keep retrying the access point if it disappears for a few seconds?

Thanks!
You could try this script.

Code: Select all

#!/bin/bash

while true ; do
	if ifconfig wlan0 | grep -q "inet addr:" ; then
		sleep 60
	else
		echo "Network connection down! Attempting reconnection."
		ifup --force wlan0
		sleep 10
	fi
done
Copy the code to a file network-monitor.sh in your home directory. Then run the command

Code: Select all

sudo chmod +x ./network-monitor.sh
to set it as executable. Run it in the background using the command

Code: Select all

sudo ./network-monitor.sh &
It checks every 60 seconds if your wifi has a network connection. If it finds it has no network address it will attempt to force a reconnect and continue doing that until a connection is re-established. If you want to stop it as it is running in the background first use the command

Code: Select all

fg
This will force it to the foreground and you can then stop it using cntl-c.

Tested it here in a couple of ways. First powering off my wifi access point. The script detects no network connection and starts attempting to force a reconnect. Powered the access point back on and after a couple of minutes the connection is re-established. For another test I removed the MAC address from the list of allowable addresses in the access point MAC filter. The network connection went down. Re-enabled the MAC address and it came back up again after a minute or two.

MrEngman

Re: Wifi Reconnect on drop

Posted: Tue Sep 11, 2012 8:39 pm
by mattura
Or instead of using 'sleep' and running in the background, you could set up a cron

Code: Select all

sudo crontab -e

Re: Wifi Reconnect on drop

Posted: Wed Sep 12, 2012 9:10 am
by MrEngman
mattura wrote:Or instead of using 'sleep' and running in the background, you could set up a cron

Code: Select all

sudo crontab -e
Looks overly complicated. If I want to check every minute I've got to enter 60 commands in to crontab, and then if it takes longer than a minute to reconnect, which is very likely, then cron will start additional commands. So could end up with multiple copies of the command running and I'd hate to think what might happen then.

So modify the script to check if it is already running and abort if it is. Hmm. Guess the best method is whichever takes least computing time. Think I'll stick with my script.

Re: Wifi Reconnect on drop

Posted: Thu Sep 13, 2012 6:10 am
by loneboat
@MrEngman: Why would you have to enter 60 commands? Wouldn't prefixing it with "* * * * *" make it run every minute from crontab?

Anyway, thanks for the script, I'm going to try it out!

Re: Wifi Reconnect on drop

Posted: Thu Sep 13, 2012 10:01 am
by MrEngman
loneboat wrote:@MrEngman: Why would you have to enter 60 commands? Wouldn't prefixing it with "* * * * *" make it run every minute from crontab?

Anyway, thanks for the script, I'm going to try it out!
OK maybe got that wrong then. But the script will still need updating to ensure only one copy is running.

Re: Wifi Reconnect on drop

Posted: Mon Sep 17, 2012 7:52 pm
by mattura
MrEngman wrote:
loneboat wrote:@MrEngman: Why would you have to enter 60 commands? Wouldn't prefixing it with "* * * * *" make it run every minute from crontab?

Anyway, thanks for the script, I'm going to try it out!
OK maybe got that wrong then. But the script will still need updating to ensure only one copy is running.
Without any 'sleep' commands it is highly unlikely that the script will take as long as 60 seconds to finish executing! Just remove the 'sleep's and the while loop, and put it in the cron list. Then every minute the cron will cause the script to execute, the script will check the network connection and restart it if it is down, then the script will finish. A minute later, the cron will cause the script to execute again, and so on...

Re: Wifi Reconnect on drop

Posted: Mon Sep 17, 2012 9:32 pm
by MrEngman
mattura wrote:
MrEngman wrote:
loneboat wrote:@MrEngman: Why would you have to enter 60 commands? Wouldn't prefixing it with "* * * * *" make it run every minute from crontab?

Anyway, thanks for the script, I'm going to try it out!
OK maybe got that wrong then. But the script will still need updating to ensure only one copy is running.
Without any 'sleep' commands it is highly unlikely that the script will take as long as 60 seconds to finish executing! Just remove the 'sleep's and the while loop, and put it in the cron list. Then every minute the cron will cause the script to execute, the script will check the network connection and restart it if it is down, then the script will finish. A minute later, the cron will cause the script to execute again, and so on...
Even if the script is modified so it doesn't loop but just executes once without looping it can take some time if the router is down. Now if it executed every 2 minutes I would be happier with that, so how can crontab do that?

Re: Wifi Reconnect on drop

Posted: Mon Sep 17, 2012 9:57 pm
by Dweeber
Script uses/checks for a lockfile, allows you to set what interface. Run from cron, I would do it every 5 minutes, but every 2 or whatever will work (example cron entry in code)

https://raw.github.com/dweeber/WiFi_Che ... WiFi_Check

Re: Wifi Reconnect on drop

Posted: Fri Dec 14, 2012 1:58 pm
by Trixster
MrEngman wrote:
colin79666 wrote:Hi,

Quite please with myself. Recently go my Pi and this evening managed to get the Edimax ew-7811un working with it (after having issues removing another adaptor I had tried).

Anyway the only slight issue I have is if the wifi drops (e.g. router reboots) then it doesn't auto reconnect. I have to unplug the adaptor and plug it back in again. As I normally connect by SSH rather than keyboard/monitor I can't just restart the networking service. Obviously I can't go unplugging/reconnecting the adaptor if I'm not at home when it drops.

Any ideas on how to force it to keep retrying the access point if it disappears for a few seconds?

Thanks!
You could try this script.

Code: Select all

#!/bin/bash

while true ; do
	if ifconfig wlan0 | grep -q "inet addr:" ; then
		sleep 60
	else
		echo "Network connection down! Attempting reconnection."
		ifup --force wlan0
		sleep 10
	fi
done
Copy the code to a file network-monitor.sh in your home directory. Then run the command

Code: Select all

sudo chmod +x ./network-monitor.sh
to set it as executable. Run it in the background using the command

Code: Select all

sudo ./network-monitor.sh &
It checks every 60 seconds if your wifi has a network connection. If it finds it has no network address it will attempt to force a reconnect and continue doing that until a connection is re-established. If you want to stop it as it is running in the background first use the command

Code: Select all

fg
This will force it to the foreground and you can then stop it using cntl-c.

Tested it here in a couple of ways. First powering off my wifi access point. The script detects no network connection and starts attempting to force a reconnect. Powered the access point back on and after a couple of minutes the connection is re-established. For another test I removed the MAC address from the list of allowable addresses in the access point MAC filter. The network connection went down. Re-enabled the MAC address and it came back up again after a minute or two.

MrEngman
Sorry to drag up an old thread, but this is exactly the sort of script I'm looking for. I've followed MrEngman's instructions and it works beautifully, however how do I now get this script (placed in /home as instructed!) to auto-execute in case the Pi reboots itself?

Re: Wifi Reconnect on drop

Posted: Sat Dec 15, 2012 4:11 pm
by Dweeber
Easiest would be to place it in /etc/rc.local

Before the exit 0 at the end of that file add:

Code: Select all

/home/network-monitor.sh &
Assuming it is located in /home... If not change the path to where it is located.

Re: Wifi Reconnect on drop

Posted: Sat Dec 15, 2012 11:31 pm
by rmcd
This code works for me; thanks!

Does anyone understand why the wifi connection drops and whether anyone is working on a fix?

Re: Wifi Reconnect on drop

Posted: Sun Dec 16, 2012 1:14 am
by Dweeber
rmcd wrote:This code works for me; thanks!

Does anyone understand why the wifi connection drops and whether anyone is working on a fix?
Lots of reasons for the connection to drop...
o poor signal
o problem with access point
o interference from another access point
o problem with RPi
o Something in the software you are using
o Problems with power
...

Mine typically stay online for days (weeks) at a time.

Note that other computer platforms typically have processes that will reestablish a connection if it is lost or seek out another authorized connection if one is present. So you may not notice a flaky Access Point on those types of devices.

The script is essentially doing the same thing on the RPi....

Re: Wifi Reconnect on drop

Posted: Sun Dec 16, 2012 4:12 pm
by rmcd
Thanks @Dweeber. I understand there can be lots of reasons, but there are two strange aspects:

1. from googling, there are a lot of people who think that previous wheezy images did not have this problem, but that it started after updates. This is anecdotal, of course, and I can't judge

2. my ubuntu laptop runs network-manager and automatically reconnects if there is a drop (which almost never happens for the access point I'm using with the pi). I don't understand why the pi doesn't do this. Syslog shows an error message about possible multiple wpa_supplicant processes:

Code: Select all

Dec 15 10:35:33 raspberrypi wpa_supplicant[4034]: Failed to initialize control interface 'DIR=/var/run/wpa_supplicant GROUP=netdev'.#012You may have another wpa_supplicant process already running or the file was#012left by an unclean termination of wpa_supplicant in which case you will need#012to manually remove this file before starting wpa_supplicant again.

Re: Wifi Reconnect on drop

Posted: Fri Dec 28, 2012 3:11 pm
by bazpaul
I have the same problem, i logged in a root, but i don;t really know where the home folder is - i just saved the file to the top level folder and did everything else

does it really matter where the script is placed?

thanks Ergman for this script

Re: Wifi Reconnect on drop

Posted: Fri Dec 28, 2012 3:24 pm
by bazpaul
oooops, one more question will the script start up everytime my Pi starts???

Also i have the network-monitor file stored in the same folder with

.bash-history
.bash-irc
.profile

is this ok???

Re: Wifi Reconnect on drop

Posted: Fri Dec 28, 2012 4:07 pm
by rmcd
I'm not sure which script you're talking about. Are you running the shell script that loops (this is what I'm guessing), or are you using crontab?

If you're running the shell script that loops, it will not run on startup unless you explicitly make that happen. You could for example edit /etc/rc.local and put a reference to the script (with a full path) there. Anything run from rc.local will execute when the OS boots.

If you're using cron, the cron service runs automatically each time the pi starts, so there's no need for you to do anything after you've set it up.

You can find out where you've stored the script by running the command pwd (print working directory). The location is probably /home/pi .

I run the script every 5 minutes using cron, which is what I would recommend. Do the following:

1. sudo crontab -e

2. At the bottom of the file, enter a line that looks like this (this presumes your script is named "network-monitor.sh"

# m h dom mon dow command
*/5 * * * * /home/pi/network-monitor.sh

3. save the file and you're done! The script will run every 5 minutes. If you omit "/5", it will run every minute.

Re: Wifi Reconnect on drop

Posted: Fri Dec 28, 2012 5:01 pm
by pluggy
bazpaul wrote:I have the same problem, i logged in a root, but i don;t really know where the home folder is - i just saved the file to the top level folder and did everything else

does it really matter where the script is placed?

thanks Ergman for this script
Root's home folder is /root

Everybody else's is in /home/username

Re: Wifi Reconnect on drop

Posted: Thu Feb 14, 2013 9:54 pm
by electricalbanana

Code: Select all

#!/bin/bash

while true ; do
   if ifconfig wlan0 | grep -q "inet addr:" ; then
      sleep 60
   else
      echo "Network connection down! Attempting reconnection."
      ifup --force wlan0
      sleep 10
   fi
done
This is not beautiful but works great!
Thanks

Re: Wifi Reconnect on drop

Posted: Fri Mar 08, 2013 11:16 am
by elyobelyob
I did write a post, but discovered from the issues page on github what the issue was (although I thought the fix had been imported - but seems not?) .. basically, I was able to run from the command line myself, but was not running correctly when ran from cron ...

I had to prefix the ifconfig, ifdown etc commands with /sbin/ e.g. /sbin/ifconfig

Re: Wifi Reconnect on drop

Posted: Fri Mar 08, 2013 11:58 am
by pjc123
elyobelyob wrote:I did write a post, but discovered from the issues page on github what the issue was (although I thought the fix had been imported - but seems not?) .. basically, I was able to run from the command line myself, but was not running correctly when ran from cron ...

I had to prefix the ifconfig, ifdown etc commands with /sbin/ e.g. /sbin/ifconfig
I also discovered this yesterday when I installed the program. I redirected standard error from the script running in cron to a file so I could see what was going on, and saw that it could not find the commands ifconfig, ifdown and ifup. The full path fixed the problem. Another option would be to append /sbin to the PATH in the script.

Unfortunately I don't need the script anymore as I replaced the wifi dongle on my pi late yesterday with a router connected directly through the Ethernet port so I can get more range out of my pi. The added bonus is that I never have to worry about wifi disconnects without reconnects any more if I am out of range of the pi with my smartphone, because I am now hard wired at the pi and that happily keeps on running.

Re: Wifi Reconnect on drop

Posted: Fri Mar 08, 2013 12:53 pm
by elyobelyob
pjc123 wrote:... as I replaced the wifi dongle on my pi late yesterday with a router connected directly through the Ethernet port
Yes, that is what I am thinking of doing at some point. My first Pi runs perfectly with ethernet. I am trialling this with a dongle. As would like to keep current setup. But as this is supposed to control my boiler, it needs to very reliable.
Also written a shell script to pass control back away from Pi on failure.
Surely there must be a way of having the Pi constantly try and rejoin the wifi network via a config setting?

Re: Wifi Reconnect on drop

Posted: Sat Mar 09, 2013 10:55 am
by elyobelyob
Well, it seems that my IP address lease is just fine. I've written a ping test and the connection has dropped, yet this script has not kicked in. So, it seems that WiFi connectivity is a dirty, unstable area with Raspberry Pi. My system worked fine in development when in 100% wifi spot, but any chance of a low signal and it's too unreliable.

So, hard wired is the only real choice for a Pi in my opinion.

Re: Wifi Reconnect on drop

Posted: Sat Mar 09, 2013 12:30 pm
by pjc123
elyobelyob wrote:Well, it seems that my IP address lease is just fine. I've written a ping test and the connection has dropped, yet this script has not kicked in. So, it seems that WiFi connectivity is a dirty, unstable area with Raspberry Pi. My system worked fine in development when in 100% wifi spot, but any chance of a low signal and it's too unreliable.

So, hard wired is the only real choice for a Pi in my opinion.
I forgot to add that the script did not work for me either. To test it I turned off the router, redirected the stdout and stderr to files and watched the files as the script was running. The problem is that ifconfig does not recognize the dropped connection and just keeps reporting that it is OK via an assigned address. Now iwlist did properly report that it had been a while since there was activity on the connection, but that certainly would not be a solution.