Reconnect RaspberryPi Automatically to Network

By Jimmy Bonney | June 6, 2015
Forest fruits

Raspbian is a Linux distribution based on Debian optimized for the Raspberry Pi. It offers a user friendly interface for the tiny hardware but does not come with all the advanced features that you might be used to on a complete Debian or Ubuntu system. One of the limitation that I observed was related to the wifi connection. As soon as the connection is dropped, there is no automatic reconnection. One needs to unplug the adapter or run ifconfig wlan0 up to turn up the connection again.

This limitation is particularly annoying when the Raspberry Pi is configured as a kiosk to display a dashboard (with auto-refresh) from an external website (such as Google Analytics for instance). In this article we’ll identify how we can configure the Raspberry Pi to reconnect automatically to the wireless network and run a refresh on the current opened page when getting back online.

There are a number of different solutions already available online (see sources below) to solve this problem. The one presented below is an aggregation of different methods that takes into account that:

  1. ping is blocked on the network on which the Raspberry Pi is running
  2. the browser (Chromium) needs to be refreshed when the connection is back

Without further due, the script below is self-documented (also available as a gist). All code related to ping has been commented out in favor of wget that will attempt to fetch a public page to confirm whether the connection is up. The code below can be saved to /usr/local/bin/wifi_reloader.sh.

#!/bin/bash

# The IP for the server you wish to ping (8.8.8.8 is a public Google DNS server)
# SERVER=8.8.8.8

# Only send two pings, sending output to /dev/null
# ping -c2 ${SERVER} > /dev/null
# Using alternative method to pinging to check if the page exists
wget -q --spider http://www.google.com

# If the return code from ping / wget ($?) is not 0 (meaning there was an error)
if [ $? != 0 ]
then
    # Exit full screen in case of error
    xdotool search --onlyvisible "Chromium" windowactivate --sync key F11
    # Restart the wireless interface
    sudo ifdown --force wlan0
    sudo ifup wlan0
    # Sleep for 45 seconds to give a chance to the network adapter to get an IP
    sleep 45s
    # Reload the active page (Google Analytics)
    xdotool search --onlyvisible "Chromium" windowactivate --sync key F5
    # Restore full screen once connection is established
    xdotool search --onlyvisible "Chromium" windowactivate --sync key F11
fi

Once the file is saved, make it executable (chmod +x /usr/local/bin/wifi_reloader.sh). The only remaining thing is to ensure that the script runs regularly to check whether the connection is still up or not. For this purpose, we can use cron. Let’s just add an entry (crontab -e) to check every couple of minutes if the Raspberry Pi is still connected to Internet:

*/2 * * * * DISPLAY=:0 sh /usr/local/bin/wifi_reloader.sh

Note the DISPLAY=:0 that is required to ensure that the xdotool commands in the script itself get executed on the screen on which it is run.

This work pretty well but there are a couple of issues that still need to be addressed when time will allow. First, in some occasion, it seems that a new tab gets open and the developer console is opened as well on both the new tab and the previously existing tab. This might be an issue with F5 that can be replaced by Ctrl+R but I do not see any reason why this is happening. Second, there are times when 45 seconds is not sufficient to get an IP. In such case, we might get in a loop when we will attempt to shut down the connection again even though this might not be necessary. The risk of this occurring could be reduced by running the script less often and providing more time to get an IP.


comments powered by Disqus