Every once in awhile my router will go down (sometimes from firmware updates), and during that time sometimes my Raspberry Pi will also reboot and I'll get something like this in the log:
Jul 25 10:00:49 raspberrypi wpa_supplicant[536]: wlan0: Trying to associate with 80:3f:5d:99:b7:63 (SSID='X' freq=2462 MHz)
Jul 25 10:00:49 raspberrypi wpa_supplicant[536]: wlan0: CTRL-EVENT-ASSOC-REJECT status_code=16
Jul 25 10:00:49 raspberrypi wpa_supplicant[536]: wlan0: CTRL-EVENT-SSID-TEMP-DISABLED id=0 ssid="X" auth_failures=1 duration=10 reason=CONN_FAILEDThis is fine since the WiFi at that moment was not available, however the WiFi comes back within a few minutes. At this time, the only 'fix' I can find is to reboot the Pi.
However, if I am not home, I'd like it to try again. I left it for 3 hours and it never re-attempted, so I'm wondering will it ever try again? Is there any way I can make it handle this case?
11 Answer
Automatically Reconnecting WiFi on a RaspberryPi
In this post, I’m going to cover writing a short script that automatically reconnects a RaspberryPi to a WiFi network. The script will check to see if the Pi has network connectivity and, if it’s offline, will restart the wireless interface to bring it back online. We’ll use
cronto schedule the execution of this script at a regular interval.There are a few ways to determine if the RaspberryPi has network connectivity. For this script, we’ll be using
ping.Writing the script
To get started, we’ll need to determine if the RaspberryPi is connected to the network. To do this, we’ll attempt to ping a server and see if we get a response. If the command succeeds (RaspberryPi receives a response from the server), we have network connectivity. If the command fails, we’ll turn
wlan0off and back on.#!/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 # If the return code from ping ($?) is not 0 (meaning there was an error) if [ $? != 0 ] then # Restart the wireless interface ifdown --force wlan0 ifup wlan0 fiName the script something memorable (
wifi_rebooter.sh), and place this script in/usr/local/bin. Make sure it’s executable by running:chmod +x /usr/local/bin/wifi_rebooter.shScheduling regular execution
To ensure the script runs automatically, we’ll use
cron. The frequency that you run this script is a matter of personal preference - I chose to run the script every five minutes.To schedule the script, open
/etc/crontabfor editing and add this line to the bottom:*/5 * * * * root /usr/local/bin/wifi_rebooter.shThis will ensure that the script is run, as root, every 5 minutes. If you’re unfamiliar with cron syntax, take a look at the cron format.
Testing
To test that the script works as expected, we are going to take down the
wlan0interface and wait for the script to bring it back up. Before taking downwlan0, you may want to adjust the interval in/etc/crontabto1minute. Also, note that this will immediately disconnect you from your shell session.To take down
wlan0to confirm the script works, run:ifdown --force wlan0After waiting patiently for ~1 minute, try SSHing back into your RaspberryPi. Assuming everything worked, your RaspberryPi should have automatically reconnected to WiFi. Don’t forget to adjust the interval in /
etc/crontabback to a more appropriate value, if you set it to one minute for testing.