Monday, 23 April 2018

Reconnecting the Raspberry Pi to a wireless network automatically

This post is part of the book Raspberry Pi Computing: Analog Measurement which can be downloaded from Leanpub for free (or donate if you wish).


Reconnecting to the wireless network automatically

I have found with experience that in spite of my best intentions, sometimes when setting up a Raspberry Pi to maintain a WiFi connection, if it disconnects for whatever reason it may not reconnect automatically.
To solve this problem we’re going to write a short script that automatically reconnects our Pi to a WiFi network. The script will check to see if the Pi is connected to our local network and, if it’s off-line, will restart the wireless network interface. We’ll use a cron job to schedule the execution of this script at a regular interval.

Let’s write a script

First, we’ll need to check if the Pi is connected to the network. This is where we’ll try to ping an IP address on our local network (perhaps our gateway address?). If the ping command succeeds in getting a response from the IP address, we have network connectivity. If the command fails, we’ll turn off our wireless interface (wlan1) and then turn it back on (yes, the timeless solution of turning it off and on).
The script looks a little like this;
#!/bin/bash

# The IP address of our gateway on our local router
GATEWAY=10.1.1.1

# Send two pings, with the output going to /dev/null
ping -c2 ${GATEWAY} > /dev/null

# Check to see if the returned value from ping ($?)
# is not 0 and then act to restart wlan1 if necessary
if [ $? == 0 ]
then
    # Restart wlan1 (the wireless interface)
    ifconfig wlan1 down
    ifconfig wlan1 up
fi
Use nano to create the script, name it something like wifistart.sh, and save it in /usr/local/bin.
We also need to make sure it’s executable by running chmod (using sudo) as follows;

Lets run our script on a regular schedule

To make our WiFi checking script run automatically, we’ll schedule a cron job using crontab;
… and add this line to the bottom:
*/5 * * * * /usr/bin/sudo -H /usr/local/bin/wifistart.sh >> /dev/null 2>&1
This runs the script every 5 minutes with sudo permissions, writing its output to /dev/null so it doesn’t spam syslog.

Let’s test it

To test that the script works as expected, we will want to take down the wlan1 interface and wait for the script to bring it back up. Before taking down wlan1, we might want to adjust the interval in crontab to 1 minute. And fair warning, when we disconnect wlan1, we will lose that network interface, so we will need to either have a local keyboard / monitor connected, have another network interface set up or be really confident that we’ve got everything set up right first time.
To take down wlan1 to confirm the script works, run:

After waiting for 5 (or 1) minutes, we could try ssh-ing back into the Raspberry Pi or if we’re keen we could have a ping command running on another server checking the interface to show when it stops and when it (hopefully) starts again. Assuming everything works, our Pi should reconnect seamlessly.
For more info on using the Raspberry Pi or for a range of other free to download books (there are no catches) check out the list here.

No comments:

Post a Comment