lynsayshepherd.com
June 27th 2014

Keeping the Raspberry Pi Connected via Wi-fi

A few months ago, I set-up my Raspberry Pi with a wi-fi adapter. More recently, I’ve noticed that the wi-fi connection occasionally drops out, and the adapter never attempts to reconnect to the network. This is a problem if I’m trying to access the Pi via SSH.

At this stage, I’m still unsure as to why the wi-fi drops out. Initially, I thought there was an issue with my interfaces file however, adding “auto wlan0” to it in an attempt to force a reconnect failed. Trawling through the syslog files revealed that when the wi-fi dropped, a “link beat lost” message was recorded. I was fairly certain it wasn’t a power supply issue messing with the USB ports so my next idea was to create a cron job to check the status of the wi-fi adapter. This solution worked and I have outlined the steps I used below.

First of all, you need to create a new bash script in the following location (note, I’m using nano as my text editor):

cd ~
sudo nano ../../usr/local/bin/wifiscript.sh

The file has the following permission (the user can execute the file)

sudo chmod 0100 ../../usr/local/bin/wifiscript.sh

Next up, here’s the script I used to check the status of the wi-fi connection. What does it do? The Pi attempts to ping Google. If the Pi cannot successfully ping Google, then the script ensures the wi-fi interface is down before bringing it back up. Else, the wi-fi is connected and no action needs to be taken (yay!).

#!/bin/bash

TESTIP=8.8.8.8

ping -c4 ${TESTIP} > /dev/null

if [ $? != 0 ]
then
    logger -t $0 "WiFi has gone down- run ifup"
    ifdown --force wlan0
    ifup wlan0
else
        logger -t $0 "WiFi is currently ok"
fi

Now we need to create a new cron job to schedule when the wifi script runs. The new cron job goes here:

cd ~
nano ../../etc/crontab

How to add the new cron job to crontab:

*/5 * * * * root    /usr/local/bin/wifiscript.sh >> /var/log/syslog 2>&1

The above line writes all the messages generated by the wifiscript.sh file to the syslog file (so “WiFi has gone down- run ifup” or “WiFi is currently ok”). The cron job is also scheduled to run every 5 minutes.

…and that should be it! Well, not quite. It’s probably a good idea to test if the script actually works.

Take the wi-fi interface down:

ifdown wlan0

Of course, this will break your SSH connection. Wait 5 minutes and the wi-fi interface should come back up, allowing you to SSH back into the Pi.

One last check. Have a look at the syslog file. You should see where the wi-fi disconnected and where it reconnected.

nano ../../var/log/syslog

Now you’re done!

Note: this is my first attempt at writing a bash script and a cron job so comments and criticism is welcome!  In the process of writing this blog post, I found Arne’s Blog to be particularly helpful.

9 Comments

  1. This is great! Thanks!

    Comment by L — November 23, 2014 @ 1:13 am

  2. All-in-one tutorial to staying connected to WiFi. +1

    Comment by Garrett Sangalli — December 24, 2014 @ 2:30 am

  3. I get the message
    -bash: /var/log/syslog : Permission denied.
    Any idea how to solve this ?

    Comment by Piet Jongen — April 9, 2015 @ 7:25 pm

  4. You might need to run the command as root.

    Comment by Lynsay — April 28, 2015 @ 9:04 am

  5. Hi Lynsay

    I found your post while searching for answers with the same problem with the same wifi adapter.

    I like your approach however I also found another post that identifies the actual issue, it’s with the Edimax EW-7811Un adapter’s power management. If you’re using the pi directly this won’t be an issue, it’s only when remotely connecting or if the pi is acting as a headless server of some kind.

    The power management of the Edimax will put the adapter in sleep mode and won’t respond to external connections. However you can override all the various PM options with a simple script. This may be more consistent a solution as the pi often reports wlan0 as up even thought the adapter is in sleep mode.

    All details on the Rpi support page here. https://www.raspberrypi.org/forums/viewtopic.php?t=61665

    Hope it helps.
    Jools

    Comment by Jools — May 31, 2015 @ 1:43 pm

  6. Here is my issue. Sometimes I loose power. When the power comes back up the Pi is quicker than the router in startup. I’ve tried using a similar script from Sam Hobbs site, but the program it seems to get stuck even after I added a 3min sleep period to try to wait for the router.
    I stink at code. That said, all I want is a simple program that checks eth0…if it’s up great. if not it’ll try to bring it up. if that fails than it’ll wait for a period of time and try again.
    Will your script do that?

    Comment by Kevin — August 4, 2015 @ 11:23 am

  7. I have a similar issue with my script. Basically, I start the Pi, I wait for 10 mins, then start running the script to ensure everything else on the system (network interfaces etc are up and running). I realise this isn’t the most efficient way to do it and I’m looking to find a way to trigger the script when everything has loaded.

    If your Pi is faster in booting than your router and you have the script running, it should be ok. The script will keep checking every 5 mins for a connection so when the router is ready, the Pi should connect (in theory!)

    Comment by Lynsay — August 27, 2015 @ 3:20 pm

  8. Thank you so much for your simple and easy to follow documentation, I am new to linux and your instructions were great and when tested everything seems to work.

    I tried absolutely everything else I found on the net, I tried turning the power management off, I tried putting the power on the USB up, you name it I tried it and nothing ever worked for me.

    Your script works flawlessly, I don’t understand why this one works when nothing else did but thank you so much, you have saved me further frustration :).

    Cheers

    Dan Donoghue

    Comment by Dan Donoghue — September 7, 2015 @ 11:45 pm

  9. Hi, great article but what if we change interface file from wi-fi connection for a cable connection, and just plug off wi-fi dongle?

    /boot/interface file we set as below:

    iface eth0 inet static
    address 192.168.0.14
    netmask 255.255.255.0
    network 192.168.0.0
    broadcast 192.168.0.255
    gateway 192.168.0.1

    allow-hotplug wlan0
    iface wlan0 inet manual

    iface wlan0 inet static
    address 192.168.1.15
    netmask 255.255.255.0
    network 192.168.1.0
    broadcast 192.168.1.255
    gateway 192.168.1.1
    wpa-ssid “SSID here”
    wpa-psk “pass_here”

    it looks like we have both wlan0 and eth0 worked, but no, we will set router to ip from eth0 – 192.168.0.1, than only that configuration is connected. Ifconfig command show no wlan0 now (wifi usb dongle unplugged ofc) only eth0 and lo listed.

    And if the internet on eth0 is working it shouldn’t do anytning becouse ping is positive, yes? But if we unplug cable – no ping, this script will try restart wlan0 even if we haven’t listet wlan0 after “ifconfig” command? It will try script every 5 minutes or just without wlan0 it wouldnt even try to reconnect?

    Comment by Adrian — March 21, 2016 @ 4:56 pm

RSS feed for comments on this post.

Leave a comment

Note: Comments will not appear on the website until they have been moderated.