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!
Wifi Reconnect on drop
31 posts
Page 1 of 2 1, 2
- Posts: 1
- Joined: Thu Aug 30, 2012 7:07 pm
I think the easiest way is installing a network manager like wicd
- Posts: 189
- Joined: Sat Nov 26, 2011 12:39 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
- Code: Select all
sudo chmod +x ./network-monitor.sh
- Code: Select all
sudo ./network-monitor.sh &
- Code: Select all
fg
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
- Posts: 657
- Joined: Fri Feb 03, 2012 2:17 pm
Or instead of using 'sleep' and running in the background, you could set up a cron
- Code: Select all
sudo crontab -e
- Posts: 18
- Joined: Tue Aug 14, 2012 6:51 pm
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.
- Posts: 657
- Joined: Fri Feb 03, 2012 2:17 pm
@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!
Anyway, thanks for the script, I'm going to try it out!
- Posts: 2
- Joined: Thu Sep 13, 2012 6:07 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.
- Posts: 657
- Joined: Fri Feb 03, 2012 2:17 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: 18
- Joined: Tue Aug 14, 2012 6:51 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?
- Posts: 657
- Joined: Fri Feb 03, 2012 2:17 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
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
My RPI Info Pages including Current Setup - http://rpi.tnet.com
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.Copy the code to a file network-monitor.sh in your home directory. Then run the command
- 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
doneto set it as executable. Run it in the background using the command
- Code: Select all
sudo chmod +x ./network-monitor.shIt 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
sudo ./network-monitor.sh &This will force it to the foreground and you can then stop it using cntl-c.
- Code: Select all
fg
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: 97
- Joined: Sat Jul 07, 2012 3:53 pm
Easiest would be to place it in /etc/rc.local
Before the exit 0 at the end of that file add:
Assuming it is located in /home... If not change the path to where it is located.
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
My RPI Info Pages including Current Setup - http://rpi.tnet.com
This code works for me; thanks!
Does anyone understand why the wifi connection drops and whether anyone is working on a fix?
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
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
My RPI Info Pages including Current Setup - http://rpi.tnet.com
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:
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
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
does it really matter where the script is placed?
thanks Ergman for this script
- Posts: 32
- Joined: Tue Nov 20, 2012 7:01 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???
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
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.
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
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.......
- 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
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 had to prefix the ifconfig, ifdown etc commands with /sbin/ e.g. /sbin/ifconfig
- Posts: 9
- Joined: Tue Oct 23, 2012 4:01 pm
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: 755
- Joined: Thu Mar 29, 2012 3:37 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
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.
So, hard wired is the only real choice for a Pi in my opinion.
- Posts: 9
- Joined: Tue Oct 23, 2012 4:01 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: 755
- Joined: Thu Mar 29, 2012 3:37 pm