Saturday, February 20, 2016

Keeping Your Raspberry Pi Connected Via WiFi

Is Your Raspberry Pi Loosing Wifi Connection? 

When my local network's access point or router restarts my Raspberry Pi does not always reconnect to the network.   This can also happen if the Pi's wifi is flaky due to distance or other issues.

To fix this I created a shell script that runs at start up based on this post.

in /home/pi add file:

#!/bin/sh -e
while true ; do
   if ifconfig wlan0 | grep -q "inet addr:" ; then
      sleep 60
      echo "Network connection down! Attempting reconnection."
      ifup --force wlan0
      sleep 10
exit 0

I run this script in /etc/rc.local
#!/bin/sh -e
/home/pi/ &
exit 0

sudo nano /etc/rc.local

Then made sure rc.local was executable:
sudo chown root /etc/rc.local
sudo chmod 755 /etc/rc.local

Make sure rc.local is executable:
sudo chmod +x /home/pi/
Check that setting change was made (look for x in permissions of file)
 ls -al /home/pi

Test if it will work in startup:
sudo /etc/init.d/rc.local start

It it works, restart your Pi:
CTRL-C to stop script if needed
sudo reboot

You can further test by restarting your access point or router.

No comments:

Post a Comment