[Solved] Raspberry Pi Wifi Disconnect Problems

Standard

Here Sometimes due to signal strength and some other issues the raspberry pi WiFi disconnects and will not reconnect automatically. Here is a script to check status of the WiFi and keep it connected.

Tip :- You can disconnect and connect wifi using   `sudo ifdown wlan0` and 'sudo ifup wlan0` respectively 

Change directory to /usr/local/bin  and create file

cd /usr/local/bin
sudo nano WiFi_Check

Paste this to file

#!/bin/bash

##################################################################
# Settings
# Where and what you want to call the Lockfile
lockfile='/var/run/WiFi_Check.pid'
# Which Interface do you want to check/fix
wlan='wlan0'
##################################################################
echo
echo "Starting WiFi check for $wlan"
date
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 "Process still running, Lockfile valid"
        exit 1
    else
        # Old Lockfile, Remove it
        #echo "Old lockfile, Removing Lockfile"
        sudo rm $lockfile
    fi
fi
# If we get here, set a lock file using our current PID#
#echo "Setting Lockfile"
sudo sh -c "echo $$ > $lockfile"

# We can perform check
echo "Performing Network check for $wlan"
if ifconfig $wlan | grep -q "inet addr:" ; then
    echo "Network is Okay"
else
    echo "Network connection down! Attempting reconnection."
    ifdown $wlan
    sleep 5
    sudo ifup --force $wlan
    ifconfig $wlan | grep "inet addr"
fi

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

# Check is complete, Remove Lock file and exit
#echo "process is complete, removing lockfile"
sudo rm $lockfile
exit 0
# END

Now set permissions

sudo chmod 0755 /usr/local/bin/WiFi_Check

Add crontab to execute it every 2 mins

sudo crontab -e

Paste content below to bottom

*/2 * * * * /usr/local/bin/WiFi_Check

Check whether its running by turning WiFi off and wait of 2 mins , it will be turned back on

pi@server7 ~ $ sudo ifconfig wlan0 down 
pi@server7 ~ $ tail -f /var/log/syslog | grep CRON
Jul  9 06:14:01 server7 /USR/SBIN/CRON[3025]: (root) CMD (/usr/local/bin/WiFi_Check )
Jul  9 06:16:01 server7 /USR/SBIN/CRON[3123]: (root) CMD (/usr/local/bin/WiFi_Check )

Here is another code if you need similar using rc.local

Errors fixed by : Siddhu Arevindh

Author  : Kevin Reed

 

Advertisements

4 thoughts on “[Solved] Raspberry Pi Wifi Disconnect Problems

  1. Nick Wethington

    I had to make some alterations to this to get it to work. Putting sudo in front of each instance of ifconfig, ifup and ifdown seemed to solve it. Otherwise I would get an error that each of these commands was “not found” in the output log (I directed crontab to write them to a log file instead of set up email notifications). This was even after changing permissions. After making these changes, things worked great!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s