ElEscalador
Posts: 676
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Re: Wifi Reconnect on drop

Wed Dec 07, 2016 1:16 pm

tweak42 wrote:
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?
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.

The connection behavior might have changed on yours because of a kernel/driver update, though those usually should make it better not worse.
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.
My Autonomous Robot Project and a few of my other projects below.

https://lloydbrombach.wordpress.com/

kaebmoo
Posts: 1
Joined: Wed Jun 21, 2017 5:31 am

Re: Wifi Reconnect on drop

Wed Jun 21, 2017 5:38 am

try this

In /etc/network/interfaces
allow-hotplug wlan0

iface wlan0 inet manual

wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

iface default inet dhcp
from http://tech.scargill.net/pi-zero-wi-fi- ... reconnect/

yiannivenizelos
Posts: 1
Joined: Tue Oct 24, 2017 9:12 pm

Re: Wifi Reconnect on drop

Tue Oct 24, 2017 9:18 pm

MrEngman wrote:
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

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?

Schorschi
Posts: 220
Joined: Thu Nov 22, 2012 9:38 pm

Re: Wifi Reconnect on drop

Wed Feb 28, 2018 11:44 pm

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

Schorschi
Posts: 220
Joined: Thu Nov 22, 2012 9:38 pm

Re: Wifi Reconnect on drop

Fri Mar 02, 2018 3:47 pm

Found even better method to drop the interface, using:
# ip link set wlan0 down
# ip link set wlan0 up

codeandcam
Posts: 2
Joined: Wed Mar 07, 2018 7:26 am

Re: Wifi Reconnect on drop

Wed Mar 07, 2018 7:42 am

From what I've gathered from this thread and others running on raspbian stretch lite, hope this helps 💯

create a shellscript

Code: Select all

sudo vi /usr/local/bin/network-monitor.sh
enter insert mode by pressing

Code: Select all

i
and paste in this snippet

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
press

Code: Select all

esc
to exit insert mode and

Code: Select all

ZZ
to save and quit

make the file executable

Code: Select all

sudo chmod +x /usr/local/bin/network-monitor.sh
open the crontab

Code: Select all

sudo crontab -e
and add

Code: Select all

*/5 * * * * /usr/local/bin/network-monitor.sh

shinji2009
Posts: 26
Joined: Thu Jan 16, 2014 12:21 pm

Re: Wifi Reconnect on drop

Sat Mar 24, 2018 6:07 am

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
sudo nano /usr/local/bin/network-monitor.sh
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.

and i use

Code: Select all

*/30 * * * * /usr/local/bin/network-monitor.sh
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
Posts: 2
Joined: Wed Mar 07, 2018 7:26 am

Re: Wifi Reconnect on drop

Sun Mar 25, 2018 4:05 am

shinji2009 personally I myself am a vim user, and i like to encourage other people to try it :shock:

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.

fleshTH
Posts: 1
Joined: Fri Jun 01, 2018 3:45 pm

Re: Wifi Reconnect on drop

Fri Jun 01, 2018 3:52 pm

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.

Abdul Wajid
Posts: 41
Joined: Tue Dec 18, 2018 7:20 am

Re: Wifi Reconnect on drop

Mon Jan 14, 2019 11:15 am

I Follow your commands but even the connection is up the output is

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.
and connection goes down when i run the program

Abdul Wajid,
MrEngman wrote:
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

raspp
Posts: 1
Joined: Sun Mar 17, 2019 5:24 pm

Re: Wifi Reconnect on drop

Sun Mar 17, 2019 6:16 pm

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:

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 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:

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
Testing on RPi Zero W, Raspbian Jessie, kernel 4.14.98, wpa_supplicant 2.3. How do I figure out what's going wrong?

Elena
Posts: 1
Joined: Mon Apr 15, 2019 8:14 pm

Re: Wifi Reconnect on drop

Mon Apr 15, 2019 8:34 pm

My solution is very straight forward, but it works for me!
First, creating the script called checkwifi.sh at /usr/local/bin/

Code: Select all

sudo nano /usr/local/bin/checkwifi.sh
Adding following lines to the script:

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	
Then, making the script executable:

Code: Select all

sudo chmod +x /usr/local/bin/checkwifi.sh
Finally, making the script run every minute of every day, each day of the week, of every month using crontab.
Open crontab to edit:

Code: Select all

sudo crontab -e
Add the following line at the end of the file

Code: Select all

* * * * * sudo /usr/local/bin/checkwifi.sh
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:

Code: Select all

sudo ifconfig wlan0 down
You Raspberry Pi should successfully reconnect to the network in a minute or so.

Return to “Beginners”