Wifi Reconnect on drop


34 posts   Page 1 of 2   1, 2
by colin79666 » Thu Aug 30, 2012 7:22 pm
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!
Posts: 1
Joined: Thu Aug 30, 2012 7:07 pm
by Nr90 » Thu Aug 30, 2012 7:55 pm
I think the easiest way is installing a network manager like wicd
Posts: 213
Joined: Sat Nov 26, 2011 12:39 pm
by MrEngman » Mon Sep 03, 2012 6:02 pm
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
Simplicity is a prerequisite for reliability. Edsger W. Dijkstra
Posts: 1353
Joined: Fri Feb 03, 2012 2:17 pm
Location: Southampton, UK
by mattura » Tue Sep 11, 2012 8:39 pm
Or instead of using 'sleep' and running in the background, you could set up a cron
Code: Select all
sudo crontab -e
Posts: 20
Joined: Tue Aug 14, 2012 6:51 pm
by MrEngman » Wed Sep 12, 2012 9:10 am
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.
Simplicity is a prerequisite for reliability. Edsger W. Dijkstra
Posts: 1353
Joined: Fri Feb 03, 2012 2:17 pm
Location: Southampton, UK
by loneboat » Thu Sep 13, 2012 6:10 am
@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!
Posts: 2
Joined: Thu Sep 13, 2012 6:07 am
by MrEngman » Thu Sep 13, 2012 10:01 am
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.
Simplicity is a prerequisite for reliability. Edsger W. Dijkstra
Posts: 1353
Joined: Fri Feb 03, 2012 2:17 pm
Location: Southampton, UK
by mattura » Mon Sep 17, 2012 7:52 pm
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...
Posts: 20
Joined: Tue Aug 14, 2012 6:51 pm
by MrEngman » Mon Sep 17, 2012 9:32 pm
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?
Simplicity is a prerequisite for reliability. Edsger W. Dijkstra
Posts: 1353
Joined: Fri Feb 03, 2012 2:17 pm
Location: Southampton, UK
by Dweeber » Mon Sep 17, 2012 9:57 pm
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_Check/master/WiFi_Check
Dweeber A.K.A. Kevin...
My RPI Info Pages including Current Setup - http://rpi.tnet.com
User avatar
Posts: 606
Joined: Fri Aug 17, 2012 3:35 am
Location: Mesa, AZ
by Trixster » Fri Dec 14, 2012 1:58 pm
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?
Posts: 120
Joined: Sat Jul 07, 2012 3:53 pm
by Dweeber » Sat Dec 15, 2012 4:11 pm
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.
Dweeber A.K.A. Kevin...
My RPI Info Pages including Current Setup - http://rpi.tnet.com
User avatar
Posts: 606
Joined: Fri Aug 17, 2012 3:35 am
Location: Mesa, AZ
by rmcd » Sat Dec 15, 2012 11:31 pm
This code works for me; thanks!

Does anyone understand why the wifi connection drops and whether anyone is working on a fix?
Posts: 3
Joined: Sat Dec 15, 2012 11:27 pm
by Dweeber » Sun Dec 16, 2012 1:14 am
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....
Dweeber A.K.A. Kevin...
My RPI Info Pages including Current Setup - http://rpi.tnet.com
User avatar
Posts: 606
Joined: Fri Aug 17, 2012 3:35 am
Location: Mesa, AZ
by rmcd » Sun Dec 16, 2012 4:12 pm
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.
Posts: 3
Joined: Sat Dec 15, 2012 11:27 pm
by bazpaul » Fri Dec 28, 2012 3:11 pm
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
Posts: 32
Joined: Tue Nov 20, 2012 7:01 pm
by bazpaul » Fri Dec 28, 2012 3:24 pm
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???
Posts: 32
Joined: Tue Nov 20, 2012 7:01 pm
by rmcd » Fri Dec 28, 2012 4:07 pm
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.
Posts: 3
Joined: Sat Dec 15, 2012 11:27 pm
by pluggy » Fri Dec 28, 2012 5:01 pm
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
Don't judge Linux by the Pi.......
User avatar
Posts: 2072
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
by electricalbanana » Thu Feb 14, 2013 9:54 pm
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
electrical-banana.com
Posts: 1
Joined: Thu Feb 14, 2013 9:45 pm
by elyobelyob » Fri Mar 08, 2013 11:16 am
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
Posts: 9
Joined: Tue Oct 23, 2012 4:01 pm
by pjc123 » Fri Mar 08, 2013 11:58 am
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.
Posts: 911
Joined: Thu Mar 29, 2012 3:37 pm
by elyobelyob » Fri Mar 08, 2013 12:53 pm
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?
Posts: 9
Joined: Tue Oct 23, 2012 4:01 pm
by elyobelyob » Sat Mar 09, 2013 10:55 am
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.
Posts: 9
Joined: Tue Oct 23, 2012 4:01 pm
by pjc123 » Sat Mar 09, 2013 12:30 pm
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.
Posts: 911
Joined: Thu Mar 29, 2012 3:37 pm