User avatar
micksulley
Posts: 166
Joined: Sat Mar 03, 2012 11:48 am
Location: Melton Mowbray, England

WiFi drops out and will not reconnect

Sun May 05, 2019 9:36 am

I have a Pi 2 with WiFi dongle which runs headless and records from a camera, the files are then uploaded to my server.

It will run fine for hours, days sometimes weeks, but then it appears to stop. Investigation shows me that it is still running, following a power cycle I can see videos it recorded after it appeared to stop, so it was still running, just unable to communicate.

My understanding is that if the WiFi drops out the Pi should reconnect when it is back again. Is that true? Is there something that I need to do to encourage it to reconnect?

My wpa_supplicant.conf is

Code: Select all

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=GB

network={
	ssid="WiFiName"
	psk="PSKPassword"
}
Other info
Linux version 4.14.79-v7+ ([email protected]) (gcc version 4.9.3 (crosstool-NG crosstool-ng-1.22.0-88-g8460611)) #1159 SMP Sun Nov 4 17:50:20 GMT 2018

Thanks
Mick

User avatar
micksulley
Posts: 166
Joined: Sat Mar 03, 2012 11:48 am
Location: Melton Mowbray, England

Re: WiFi drops out and will not reconnect

Fri May 10, 2019 8:53 am

Can anyone help me with this please? It has just happened again.

User avatar
DougieLawson
Posts: 37497
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: WiFi drops out and will not reconnect

Fri May 10, 2019 10:08 am

I run this (from an @reboot line in root's crontab)

Code: Select all

#!/bin/bash
# Unified 17/04/2019 15:55

thishost=$(hostname | tr '[:upper:]' '[:lower:]')

case "$thishost" in
        raspberry)
                ip="192.168.3.1/24"
                wlx="wlan0"
        ;;
        pi)
                ip="192.168.3.2/24"
                wlx="wlx000f1340184f"
        ;;
        raspberrypi)
                ip="192.168.3.14/24"
                wlx="wlan0"
        ;;
        *)
                ip="NOT SET" # unknown
                wlx="wlx00000000000"
        ;;
esac

if [ "$ip" == "NOT SET" ]
then
        exit 20
fi

router="192.168.3.254"
google="8.8.8.8"
resolver="192.168.3.14"

logger "Wifi check: $router"

while :
do

  /bin/ping -q -c1 $google > /dev/null

  if [ $? -eq  0 ]
  then
    : #  echo "Network active"
    logger "WiFi check: network active"
  else
    DA=$(date)
    echo "Network down at $DA, fixing..."
    logger "WiFi check: Network down, fixing ..."
    logger "WiFi check: killall wpa_supplicant"
    /usr/bin/killall -v wpa_supplicant
    rc=$?
    sleep 1
    /usr/bin/killall -v wpa_supplicant
    rc=$?
    logger "WiFi check: $rc -> next: ifconfig $wlx $ip up"
    /sbin/ifconfig $wlx $ip
    /sbin/ifconfig $wlx up
    sleep 2
    logger "WiFi check: restart supplicant"
    /sbin/wpa_supplicant -B -c/etc/wpa_supplicant/wpa_supplicant.conf -i$wlx -Dnl80211,wext
    rc=$?
    sleep 1
    logger "Wifi check: $rc -> next: ip link set $wlx up"
    /sbin/ip link set $wlx up
    rc=$?
    sleep 1
    ROUTE=$(ip route)
    logger "WiFi check:  -> $ROUTE"
    logger "WiFi check: $rc -> next:  ip route add default via $router dev $wlx"
    /sbin/ip route add default via $router dev $wlx
    rc=$?
  fi

  sleep 360

done
That wakes up every ten minutes and restarts the network if Google in unreachable.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
micksulley
Posts: 166
Joined: Sat Mar 03, 2012 11:48 am
Location: Melton Mowbray, England

Re: WiFi drops out and will not reconnect

Fri May 10, 2019 10:43 am

Thanks Dougie,
A few questions -
All my Pi's are renamed pi-cabin, pi-porch, etc, does that mean I need to change the case statement?
I guess that I need to change the IP address in the case to match mine
I guess I need to change the router= line to match mine
What is the resolver? I don't know the term and can't see anything like that on my router like that.
Where will the logger output go?
Regards
Mick

User avatar
DougieLawson
Posts: 37497
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: WiFi drops out and will not reconnect

Fri May 10, 2019 3:38 pm

Change the case for each host name and each fixed IP (if set). All eighteeen of my Raspberries have fixed IP addresses. That program is running on 14 out of 18.

If you don't have fixed IP addresses then delete

Code: Select all

    logger "WiFi check: $rc -> next: ifconfig $wlx $ip up"
    /sbin/ifconfig $wlx $ip
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Silverlock
Posts: 87
Joined: Wed Apr 10, 2013 3:51 am

Re: WiFi drops out and will not reconnect

Fri May 10, 2019 6:42 pm

My 2 cents: here's a script that I use (from a @reboot line in the pi user's crontab) to keep the WiFi working.

Code: Select all

#!/bin/bash
sleeptime=120
externalpingip=192.168.0.1
while [ 1 ]
do
    /bin/sleep $sleeptime
    /bin/ping -c 2 $externalpingip >/dev/null 2>&1
    if [ $? != 0 ]
    then
        /usr/bin/logger "wlan0 reassociate"
        /sbin/wpa_cli -i wlan0 reassociate
    fi
done
I was having a fair bit of trouble with my Pi's Wifi "dropping out/going catatonic/being a cat and ignoring me". Everything looked good in anything I could think to log; everything said the WiFi was up and running but it wasn't. It wasn't until I dropped the sleeptime down to 20 seconds that I noticed something; the times between reassociations were multiples of 10 minutes. And I remembered that the key renewal time on my AP was 600 seconds... 10 minutes. I changed that to 720 seconds, and then the reassociation intervals were multiples of 12 minutes. Coincidence? Possibly. But for some reason it made me look at the channels that the other WiFi networks the Pi was picking up were broadcasting on. It turned out that two of them were using the same channel as mine, and had fairly decent signal strengths. So I overrode the auto channel setting in my AP and set it to a channel that wasn't being used by anything else nearby. Since then, I've only seen the script log a reassociation once - quite a change from the many times a day I had been seeing.

What relationship is there between key renewal and multiple APs using the same channel in the area? I'm still scratching my head on that one - and I hate leaving a problem half solved. But my Pi's WiFi is working much smoother now, and that's not a bad thing.
RPi3+external HDD/Raspbian Stretch

User avatar
micksulley
Posts: 166
Joined: Sat Mar 03, 2012 11:48 am
Location: Melton Mowbray, England

Re: WiFi drops out and will not reconnect

Fri May 10, 2019 10:16 pm

Thank you both for your help, I now have a script that I think works, but cannot figure out the @reboot bit.
My script has a line at the begining
/usr/bin/logger "##### start of WiFiWake.sh ######"
If I run the script directly I see that message in /var/log/syslog
I have tried adding the line
@reboot /home/pi/WiFIWake.sh
to both user crontab and to sudo crontab but neither produces the message, so I assume that it has not worked. How do I do it?
Mick

Andyroo

Re: WiFi drops out and will not reconnect

Fri May 10, 2019 10:30 pm

There is a great web page on debugging Cron at https://garyhall.org.uk/troubleshooting ... ry-pi.html

Section 6 should help :lol:

No it’s not my page.

PhatFil
Posts: 1616
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: WiFi drops out and will not reconnect

Sat May 11, 2019 12:06 am

fwiw i had strange issues with my pi es occasionally loosing wifi and after a lot of log watching and setting up wifi watchdogs and other checks i discovered that a router reboot was in fact the required fix and on its recovery the 'missing' pi es were all present and ready.. now if i loose a wifi connection to a pi unexpectedly the router gets a reboot command and things return to normal.

User avatar
micksulley
Posts: 166
Joined: Sat Mar 03, 2012 11:48 am
Location: Melton Mowbray, England

Re: WiFi drops out and will not reconnect

Mon May 13, 2019 10:13 pm

It has just happened again, but this time it is slightly different. What I see this time is

Code: Select all

[email protected] ~ $ ssh [email protected]
ssh_exchange_identification: read: Connection reset by peer
If I use verbose mode

Code: Select all

[email protected] ~ $ ssh -v [email protected]
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to pi-porch [192.168.1.74] port 22.
debug1: Connection established.
debug1: identity file /home/mick/.ssh/id_rsa type 0
debug1: key_load_public: No such file or directory
debug1: identity file /home/mick/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/mick/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/mick/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/mick/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/mick/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/mick/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/mick/.ssh/id_ed25519-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
ssh_exchange_identification: read: Connection reset by peer
I have searched but all the hits are resolving initial setup, what I have here is something which works, but then fails, but a power cycle gets it going again with no changes
Any ideas?
Mick

Andyroo

Re: WiFi drops out and will not reconnect

Mon May 13, 2019 10:47 pm

Are you running fail2ban or denyhost on the Pi? It’s like you have been locked out.

You may get a clue from

Code: Select all

tail -f /var/log/auth.log
Have you set any security on SSH on the Pi that may not be matched from your local machine?
Does the Pi have a static address? With the link going up and down I wonder if you have a duplicate IP address on the network? It’s possible your router has used this for one device and the Pi is jumping over it...

User avatar
micksulley
Posts: 166
Joined: Sat Mar 03, 2012 11:48 am
Location: Melton Mowbray, England

Re: WiFi drops out and will not reconnect

Mon May 13, 2019 10:57 pm

It doesn't look like it

Code: Select all

[email protected]:~ $ tail -f /var/log/auth.log
May 13 23:40:01 pi-porch CRON[1703]: pam_unix(cron:session): session opened for user pi by (uid=0)
May 13 23:40:01 pi-porch CRON[1703]: pam_unix(cron:session): session closed for user pi
May 13 23:42:01 pi-porch CRON[1715]: pam_unix(cron:session): session opened for user pi by (uid=0)
May 13 23:42:08 pi-porch CRON[1715]: pam_unix(cron:session): session closed for user pi
May 13 23:48:02 pi-porch CRON[1836]: pam_unix(cron:session): session opened for user pi by (uid=0)
May 13 23:48:09 pi-porch CRON[1836]: pam_unix(cron:session): session closed for user pi
May 13 23:50:01 pi-porch CRON[1848]: pam_unix(cron:session): session opened for user pi by (uid=0)
May 13 23:50:01 pi-porch CRON[1848]: pam_unix(cron:session): session closed for user pi
May 13 23:54:01 pi-porch CRON[1968]: pam_unix(cron:session): session opened for user pi by (uid=0)
May 13 23:54:08 pi-porch CRON[1968]: pam_unix(cron:session): session closed for user pi

Andyroo

Re: WiFi drops out and will not reconnect

Mon May 13, 2019 11:11 pm

You may have to go back time the time of the last fail :cry:

The only active lines I have in my SSH config files are:

Code: Select all

SendEnv LANG LC_*
    HashKnownHosts yes
    GSSAPIAuthentication yes
Are you confident about duplicate IP / MAC addresses - a router reboot (and any WiFi extenders) may not hurt (hate the turn off / on fixes).

Ernst
Posts: 1283
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: WiFi drops out and will not reconnect

Tue May 14, 2019 5:27 am

Andyroo wrote:
Mon May 13, 2019 11:11 pm
Are you confident about duplicate IP / MAC addresses - a router reboot (and any WiFi extenders) may not hurt (hate the turn off / on fixes).
It may not hurt to see the output of the following command when entered on the failing Pi:
ip a
Looking through the thread your suspicion with duplicate MAC address could be a possibility if a particular dongle is being used.
The road to insanity is paved with static ip addresses

User avatar
micksulley
Posts: 166
Joined: Sat Mar 03, 2012 11:48 am
Location: Melton Mowbray, England

Re: WiFi drops out and will not reconnect

Tue May 14, 2019 8:31 am

Andyroo, this is my /etc/init/ssh.config, I don't believe that I have ever changed it. Is that the file you meant?

Code: Select all

# ssh - OpenBSD Secure Shell server
#
# The OpenSSH server provides secure shell access to the system.

description	"OpenSSH server"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
respawn limit 10 5
umask 022

env SSH_SIGSTOP=1
expect stop

# 'sshd -D' leaks stderr and confuses things in conjunction with 'console log'
console none

pre-start script
    test -x /usr/sbin/sshd || { stop; exit 0; }
    test -e /etc/ssh/sshd_not_to_be_run && { stop; exit 0; }

    mkdir -p -m0755 /run/sshd
end script

# if you used to set SSHD_OPTS in /etc/default/ssh, you can change the
# 'exec' line here instead
exec /usr/sbin/sshd -D
Ernst, this is the output

Code: Select all

[email protected]:~/.ssh $ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
    link/ether b8:27:eb:d8:42:02 brd ff:ff:ff:ff:ff:ff
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 80:1f:02:af:58:23 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.111/24 brd 192.168.1.255 scope global wlan0
       valid_lft forever preferred_lft forever
All of my Pi's are set to dynamic IP, but the router binds IP to mac address, so I don't believe that I have a duplicate IP problem, however to be certain I have now set it to a new IP well out of the range of everything else. See how it goes now.

Thanks
Mick

Return to “Troubleshooting”