Auto-reconnecting to WiFi!


#1

The Raspberry Pi will not automatically reconnect to WiFi if its connection is dropped. This can become very problematic, especially for those who care alot about streaks, stats, and ranking.

This script will test and attempt to fix the Raspberry’s WiFi connection. The original script can be found here.



#!/bin/bash

##############################################################################################################
#
#   == WLAN TEST SRIPT ==
#   Original author: Kevin Reed (dweeber.dweebs@gmail.com)
#	Edited by cecil1829
#   Original on GitHub:https://github.com/dweeber/WiFi_Check 
#
#	Tests the wireless connection between the RasPi and connected router. Attempts to reconnect if the link
#	appears to be down.
#
#	== INSTALLATION ==
#	1. Change directories to wherever you want to install the script, preferably /usr/share.
#		~$ cd /usr/share
#	2. Create a new shell script named 'wlan_reconnect.sh'
#		~$ sudo nano wlan_reconnect.sh
#	3. Copy this script from your web browser and paste it into your terminal text editor. Save and exit.
#	4. Make the script execuatable.
#		~$ sudo chmod 0755 /usr/share/wlan_reconnect.sh
#	5. Create a new cronjob that will execute the script on a regular basis. 
#		~$ crontab -e
#		~$ */5 * * * * /path/to/script/wlan_reconnect.sh
#		
#	The script will be executed every 5 minutes. Change the '5' to change 
#	the time period.
#
#	== CONFIGURATION ==
#	Where should the script create its lockfile, and what should its name be?
	lockfile='/var/run/wlan_reconnect.pid'
#	
#	What wireless network interface should the script check and fix (typically wlan0)?	
	wlan='wlan0'
#
#	What address should the script ping to test connectivity (using your router's IP is recommended)?
	pingip='192.168.1.1'
#
##############################################################################################################

####################
#	BEGIN SCRIPT   #
####################

#Print and log some status text.
echo
echo "Starting WiFi check for $wlan"
date
logger "wlan - Starting WiFi check for $wlan"
echo

# Check to see if there is a lock file
if  -e $lockfile ]; then
    # A lockfile exists... Lets check to see if it is still valid
    pid=`cat $lockfile`
    if kill -0 &>1 > /dev/null $pid; then
        # Still Valid... lets let it be...
        echo "[info] Script still running, exiting current instance."
		logger "wlan - [info] Script still running, exiting current instance."
        exit 1
    else
        # Old Lockfile, Remove it
        echo "[info] Removing old lockfile..."
		logger "wlan - [info] Removing old lockfile..."
        rm $lockfile
    fi
fi
# If we get here, set a lock file using our current PID#
echo "[info] Setting Lockfile"
logger "wlan - [info] Setting Lockfile"
echo $$ > $lockfile

# We can perform check
echo "[info] Performing Network check for $wlan"
logger "wlan - [info] Performing Network check for $wlan"
/bin/ping -c 2 -I $wlan $pingip > /dev/null 2> /dev/null
if  $? -ge 1 ] ; then
    echo "[WARN] $wlan link down, attempting to reconnect..."
    logger "wlan - [WARN] $wlan link down, attempting to reconnect..."
    /sbin/ifdown $wlan
    /bin/sleep 5
    /sbin/ifup --force $wlan
else
    echo " OK ] $wlan link up"
    logger "wlan -  OK ] $wlan link up"
fi

echo
echo "Current IP Setting:"
/sbin/ifconfig $wlan | grep "inet addr:"
echo

# Check is complete, Remove Lock file and exit
echo " OK ] process is complete, removing lockfile"
logger "wlan -  OK ] process is complete, removing lockfile"
rm $lockfile
exit 0

###################
#	 END SCRIPT   #
###################


You can test the script by manually executing it, or by checking the system log (use Ctrl+C to exit).


tail -f /var/log/syslog

Hopefully, you will find this script useful for beefing up your PiAware installations. I’ve tested it multiple times on my installation, and so far everything is working well.


#2

My experience has been that the Pi has no problem reconnecting to Wifi after an outage. I did have one Pi that had that issue, but I recently created a new SD card with Raspbian, Piaware, dump, etc., and now it has no trouble reconnecting to Wifi. I have four Pi’s running 24/7 and only one has had that problem. The reconnection issue may have something to do with your particular network or the Pi’s Wifi adapter.