I'll have to check when I get home. I was using a tplink mini-adapter, but purchased one at Microcenter that was in the Rpi area with a proper rubber-ducky antenna hoping that would increase the reliability of my connection. Didn't seem to make a difference...but the cron/script-job seemed to. For a while... Perhaps it is a wonky driver issue. Worth looking into.tweak42 wrote:What chip is your wifi adapter using? I had a realtek model that used flaky drivers that would drop and require a power cycle to get going again. It was kinda random on uptime, so it could go hours to days sometimes.ElEscalador wrote:Is there a thing where the pi (zero, in this case) starts disconnecting if you aren't using the wifi? Although I'm running one of these scripts (two scripts up, I think) with cron. 5 minutes sometimes left me waiting for the reconnect before I could open/close my garage door. Changed it to one minute and had almost perfect luck for about a month. Then it seems my connection is pretty much down 100% of the time I first attempt to log in (I have an app that ssh handshakes and sends the command to run my c program), and I have to wait about the full minute. The connection ALWAYS does come back after a minute so I believe cron and the script must be working, but I don't understand what happened that I went from dropping connection every few minutes to pretty much within a few seconds of the reconnect script. I have made no changes since it was first working almost perfectly. Should I just set cron to run it every 15 seconds so I don't have to wait as long? Seems ridiculous but if I have to I have to...any negatives to doing so?
And for us simple folk - what is the difference between the last couple of scripts? What makes is one preferable to the other?
The connection behavior might have changed on yours because of a kernel/driver update, though those usually should make it better not worse.
-
- Posts: 913
- Joined: Tue Dec 15, 2015 4:55 pm
- Location: Detroit, MI USA
- Contact: Website
Re: Wifi Reconnect on drop
Robotics tips, hacks, book extras https://youtube.com/practicalrobotics
Re: Wifi Reconnect on drop
try this
In /etc/network/interfaces
In /etc/network/interfaces
from http://tech.scargill.net/pi-zero-wi-fi- ... reconnect/allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
-
- Posts: 1
- Joined: Tue Oct 24, 2017 9:12 pm
Re: Wifi Reconnect on drop
MrEngman wrote: ↑Mon Sep 03, 2012 6:02 pmYou could try this script.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!Copy the code to a file network-monitor.sh in your home directory. Then run the commandCode: 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
to set it as executable. Run it in the background using the commandCode: Select all
sudo chmod +x ./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 commandCode: 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 bring up an old thread but I'm a beginner and trying to figure this out so my pi automatically reconnects to the internet when it drops/resets. Where do I add/edit these files and type these commands? On my SD card is literally just the screenly OS (I don't have raspbian installed), along with a file called network.ini which has my wireless network details in to connect to my wifi (from this website - https://wizard.screenly.io). Is there a more recent and easier way for the raspberry pi 3?
Re: Wifi Reconnect on drop
It would be better to use 'dhcpcd' if you are using Raspbian based on 9.0 or later, since dhcpcd is a bit smarter than ifconfig. I would also try a connection test before using dhcpcd so you only try to reconnect if needed.
For example:
1) Ping your router, if found, exit
2) If router not found, run 'dhcpcd -k' which will kill the network interface, note this will kill all interfaces if you have more than one.
3) run 'dhcpcd' which will attempt to wake up all interfaces
4) schedule you script to run via cron as often as you need it, I would think once every 5 minutes is reasonable
For example:
1) Ping your router, if found, exit
2) If router not found, run 'dhcpcd -k' which will kill the network interface, note this will kill all interfaces if you have more than one.
3) run 'dhcpcd' which will attempt to wake up all interfaces
4) schedule you script to run via cron as often as you need it, I would think once every 5 minutes is reasonable
Re: Wifi Reconnect on drop
Found even better method to drop the interface, using:
# ip link set wlan0 down
# ip link set wlan0 up
# ip link set wlan0 down
# ip link set wlan0 up
-
- Posts: 2
- Joined: Wed Mar 07, 2018 7:26 am
Re: Wifi Reconnect on drop
From what I've gathered from this thread and others running on raspbian stretch lite, hope this helps 
create a shellscript
enter insert mode by pressing and paste in this snippet
press to exit insert mode and to save and quit
make the file executable
open the crontab and add
create a shellscript
Code: Select all
sudo vi /usr/local/bin/network-monitor.sh
Code: Select all
i
Code: Select all
#!/bin/bash
##################################################################
# Settings
# Which Interface do you want to check/fix
wlan='wlan0'
# Which address do you want to ping to see if the network interface is alive?
pingip='8.8.8.8'
##################################################################
echo "Performing Network check for $wlan"
/bin/ping -c 1 -I $wlan $pingip > /dev/null 2> /dev/null
if [ $? -ge 1 ] ; then
echo "Network connection down! Attempting reconnection."
ip link set wlan0 down
sleep 5
ip link set wlan0 up
else
echo "Network is Okay"
fi
Code: Select all
esc
Code: Select all
ZZ
make the file executable
Code: Select all
sudo chmod +x /usr/local/bin/network-monitor.sh
Code: Select all
sudo crontab -e
Code: Select all
*/5 * * * * /usr/local/bin/network-monitor.sh
-
- Posts: 27
- Joined: Thu Jan 16, 2014 12:21 pm
Re: Wifi Reconnect on drop
i was ok with viewtopic.php?f=91&t=16054&start=25#p602042 but now there's nothing in /etc/network/interfaces for no reason
codeandcam thank you
I would recommend to use
and i use
because my device is not so critical to ping google every 5 minutes.
and if you use wifi router, you can ping 192.168.1.1 or 192.168.0.1 depends of your network settings. because 8.8.8.8 may give false positive when provider down, forget to pay for traffic etc. and pi will twitch wifi for no reason.
codeandcam thank you
I would recommend to use
instead of vi because nano is so much easier and user-frendly and i could not get vi work via ssh from windows. even first launch of crontab recommend to use nano.sudo nano /usr/local/bin/network-monitor.sh
and i use
Code: Select all
*/30 * * * * /usr/local/bin/network-monitor.sh
and if you use wifi router, you can ping 192.168.1.1 or 192.168.0.1 depends of your network settings. because 8.8.8.8 may give false positive when provider down, forget to pay for traffic etc. and pi will twitch wifi for no reason.
-
- Posts: 2
- Joined: Wed Mar 07, 2018 7:26 am
Re: Wifi Reconnect on drop
shinji2009 personally I myself am a vim user, and i like to encourage other people to try it
As to pinging google I am doing this because every LAN setup can be different, therefore it may fail otherwise. But I also leave room in the config to change that if you wish.

As to pinging google I am doing this because every LAN setup can be different, therefore it may fail otherwise. But I also leave room in the config to change that if you wish.
Re: Wifi Reconnect on drop
I do not think it is best to use a public IP address to determine if your wifi is down. Your internet being down is not the same as your wifi. For example, I have my Pi Zerow on my network controlling lights. I can get on my phone and turn lights on and off over my wifi network. However, when pinging public addresses to determine if i need to reconnect to wifi, if my internet is down then my pi is cycling it's connection to the access point for no reason making it unreliable on my network.
-
- Posts: 41
- Joined: Tue Dec 18, 2018 7:20 am
Re: Wifi Reconnect on drop
I Follow your commands but even the connection is up the output is
and connection goes down when i run the program
Abdul Wajid,
Code: Select all
RTNETLINK answers: File exists
Network connection down! Attempting reconnection.
RTNETLINK answers: File exists
Network connection down! Attempting reconnection.
RTNETLINK answers: File exists
Network connection down! Attempting reconnection.
RTNETLINK answers: File exists
Network connection down! Attempting reconnection.
RTNETLINK answers: File exists
Network connection down! Attempting reconnection.
RTNETLINK answers: File exists
Network connection down! Attempting reconnection.
Abdul Wajid,
MrEngman wrote: ↑Mon Sep 03, 2012 6:02 pmYou could try this script.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!Copy the code to a file network-monitor.sh in your home directory. Then run the commandCode: 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
to set it as executable. Run it in the background using the commandCode: Select all
sudo chmod +x ./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 commandCode: 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
Re: Wifi Reconnect on drop
Can't seem to get the RPi to reconnect to a wifi network. Upon booting, it successfully connects to the network I have added to /etc/wpa_supplicant/wpa_supplicant.conf automatically.
However, if I take down the wifi network for a minute and bring it back up, nothing I do seems to be able to get it to connect again (until reboot).
I've tried several commands individually and in combinations:
I tried several of the above, but every time I check with ifconfig/iwconfig, the RPi is disconnected from the wifi network.
I have tried several versions of /etc/network/interfaces:
Testing on RPi Zero W, Raspbian Jessie, kernel 4.14.98, wpa_supplicant 2.3. How do I figure out what's going wrong?
However, if I take down the wifi network for a minute and bring it back up, nothing I do seems to be able to get it to connect again (until reboot).
I've tried several commands individually and in combinations:
Code: Select all
# Attempt 1
ifup --force wlan0
# Attempt 2
wpa_cli -i wlan0 reconfigure
# Attempt 3
ifdown wlan0 && ifup wlan0
# Attempt 4
ifconfig wlan0 down && ifconfig wlan0 up
# Attempt 5
dhcpcd -k && dhcpcd
# Attempt 6
ip link set wlan0 down
ip link set wlan0 up
# Attempt 7
systemctl restart networking
I have tried several versions of /etc/network/interfaces:
Code: Select all
# Attempt 1
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
wireless-power off
# Attempt 2
auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
wireless-power off
# Attempt 3
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
wireless-power off
Re: Wifi Reconnect on drop
My solution is very straight forward, but it works for me!
First, creating the script called checkwifi.sh at /usr/local/bin/
Adding following lines to the script:
Then, making the script executable:
Finally, making the script run every minute of every day, each day of the week, of every month using crontab.
Open crontab to edit:
Add the following line at the end of the file
Works on a Raspberry Pi 3+, Raspbian Stretch.
To test the script you can manually turn off the wifi connection by running the following command at the command prompt:
You Raspberry Pi should successfully reconnect to the network in a minute or so.
First, creating the script called checkwifi.sh at /usr/local/bin/
Code: Select all
sudo nano /usr/local/bin/checkwifi.sh
Code: Select all
#!/bin/bash
#ping the router to check the wifi connection
ping -c4 192.168.108.01 > /dev/null
# if exit code ($?) of the ping command is failed (not 0) then reconnect
if [ $? != 0 ]
then
sudo ifconfig wlan0 up
fi
Code: Select all
sudo chmod +x /usr/local/bin/checkwifi.sh
Open crontab to edit:
Code: Select all
sudo crontab -e
Code: Select all
* * * * * sudo /usr/local/bin/checkwifi.sh
To test the script you can manually turn off the wifi connection by running the following command at the command prompt:
Code: Select all
sudo ifconfig wlan0 down
Re: Wifi Reconnect on drop
Checked on PRI 3B+ when testing got cross on wifi and when hovering on it it says "No APs found scanning..." so this method does not work!Elena wrote: ↑Mon Apr 15, 2019 8:34 pmMy solution is very straight forward, but it works for me!
First, creating the script called checkwifi.sh at /usr/local/bin/Adding following lines to the script:Code: Select all
sudo nano /usr/local/bin/checkwifi.sh
Then, making the script executable:Code: Select all
#!/bin/bash #ping the router to check the wifi connection ping -c4 192.168.108.01 > /dev/null # if exit code ($?) of the ping command is failed (not 0) then reconnect if [ $? != 0 ] then sudo ifconfig wlan0 up fi
Finally, making the script run every minute of every day, each day of the week, of every month using crontab.Code: Select all
sudo chmod +x /usr/local/bin/checkwifi.sh
Open crontab to edit:Add the following line at the end of the fileCode: Select all
sudo crontab -e
Works on a Raspberry Pi 3+, Raspbian Stretch.Code: Select all
* * * * * sudo /usr/local/bin/checkwifi.sh
To test the script you can manually turn off the wifi connection by running the following command at the command prompt:You Raspberry Pi should successfully reconnect to the network in a minute or so.Code: Select all
sudo ifconfig wlan0 down
-
- Posts: 27
- Joined: Thu Jan 16, 2014 12:21 pm
Re: Wifi Reconnect on drop
i've tried all of this and still - wifi on rpi 3b is unusable. because of wifi always something hanging, if not wifi, then rpi itself. i bought a switch and hook pri to ethernet - and all just start working 24/7. without dancing with tambourine.
Re: Wifi Reconnect on drop
This is what have worked:shinji2009 wrote: ↑Tue Apr 28, 2020 2:00 pmi've tried all of this and still - wifi on rpi 3b is unusable. because of wifi always something hanging, if not wifi, then rpi itself. i bought a switch and hook pri to ethernet - and all just start working 24/7. without dancing with tambourine.
All done through SSH as my server is on remote location, so I have used puTTY to do it remotely:
I have created sh script on the desktop folder iHome with the following content:
Code: Select all
#!/bin/bash
##################################################################
# Settings
# Which Interface do you want to check/fix
wlan='wlan0'
# Which address do you want to ping to see if the network interface is alive?
pingip='8.8.8.8'
##################################################################
echo "Performing Network check for $wlan"
/bin/ping -c 1 -I $wlan $pingip > /dev/null 2> /dev/null
if [ $? -ge 1 ] ; then
echo "Network connection down! Attempting reconnection."
ip link set wlan0 down
sleep 5
ip link set wlan0 up
else
echo "Network is Okay"
fi
to change owner of the group and file itself:
Code: Select all
#change owner of the file:
chown openhabian /home/openhabian/Desktop/iHome/checkwifi.sh
#change group of the file:
chgrp openhabian /home/openhabian/Desktop/iHome/checkwifi.sh
#check file details:
ls -l /home/openhabian/Desktop/iHome/checkwifi.sh
Code: Select all
chmod +x /home/openhabian/Desktop/iHome/checkwifi.sh
Code: Select all
crontab -e
# Add the following line at the end of the file:
*/1 * * * * /home/openhabian/Desktop/iHome/checkwifi.sh
then once above 2 important steps done, I have restarted RPI and then as proposed above I have tested how the script restarting my wlan0 by the following command:
Code: Select all
sudo ifconfig wlan0 down
if RPI is hanging up then install cooler and input autorestart every midnight command:
Code: Select all
#Periodic openhab reboot go to /etc/crontab, add below line before # and raspbian will reboot every midnight at 00:00:
0 0 * * * root reboot
Re: Wifi Reconnect on drop
Hi,
I've the same problem on my pi4 with fresh installed and updated raspbian.
Any solution without script? Anyone tried with other network manager like wicd?
Very annoying to see interface down or unable to reconnect if i reboot my router
I've the same problem on my pi4 with fresh installed and updated raspbian.
Any solution without script? Anyone tried with other network manager like wicd?
Very annoying to see interface down or unable to reconnect if i reboot my router