Take the 2-minute tour ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

I'm using my pi to monitor my power meters. Data is transferred to PC by WiFi connection using Edimax EW-7811UN USB adapter. When the Wifi connection drops (switched off over night, or shaky connection), the USB adapter remains disabled.

Is there a way to restart the WiFi connection automatically without re-plugging the WiFi adapter?

share|improve this question

4 Answers 4

up vote 12 down vote accepted

I prefer to disable most of the network auto configuration and connection management daemon stuff and deal with it myself. Here's a (bash) script that will keep the connection up as long as the network is there and you do not have a glitchy wifi driver or power issues; the idea is to ping the router every N seconds, and if that fails, re-connect:

#!/bin/bash    

# make sure we aren't running already
what=`basename $0`
for p in `ps h -o pid -C $what`; do
        if [ $p != $$ ]; then
                exit 0
        fi
done

# source configuration
. /etc/wifi.conf

exec 1> /dev/null
exec 2>> $log
echo $(date) > $log
# without check_interval set, we risk a 0 sleep = busy loop
if [ ! "$check_interval" ]; then
        echo "No check interval set!" >> $log
        exit 1
fi

startWifi () {
        dhclient -v -r
    # make really sure
        killall dhclient
        iwconfig $wlan essid $essid
        dhclient -v $wlan
}

ifconfig $eth down
ifconfig $wlan up
startWifi

while [ 1 ]; do
        ping -c 1 $router_ip & wait $!
        if [ $? != 0 ]; then
                echo $(date)" attempting restart..." >> $log
                startWifi
                sleep 1
        else sleep $check_interval
        fi
done

So, /etc/wifi.conf in this case might contain:

router_ip=192.168.0.1
log=/var/log/wifi.log
wlan=wlan0
eth=eth0
essid=someNetwork

This all presumes an open unencrypted network (if otherwise, you will have to add the appropriate commands). I've used this approach on various linux machines, including the pi, and it works flawlessly; it will keep a system online indefinitely, even if it periodically goes to sleep (which the pi cannot anyway).

A decent check interval is 3-5 seconds; this activity will not significantly impact system resources at all.

You absolutely do need to disable the network auto-configuration first, or this will interfere with your efforts. The easiest way to do this is to comment out everything in /etc/network/interfaces except:

auto lo
iface lo inet loopback

To start networking at boot (since I use the pi headless), I have this set to run on raspbian from /etc/rc.local thusly:

wifi_mod=`lsmod | grep 8192cu`
if [ "$wifi_mod" ]; then
        echo "Starting wifi..."
        /usr/bin/nice -n -10 /usr/local/bin/wifi &
else
        echo "Starting ethernet..."
        /sbin/ifconfig eth0 up
        /sbin/dhclient eth0
fi

/usr/local/bin/wifi is the script. If you don't know what nice is for, read man nice.

The point of the if is that if my wifi dongle is plugged into the pi, the 8192cu module will be loaded by the kernel at this point -- so wifi should start. If not, then it's assumed that the ethernet is plugged in and should be used (if it is isn't, dhclient will just crap out and there is no network access). Note that I removed ifplugd from the system, which if you do leave an ethernet cable plugged in for some reason, you will have to do the same thing (as in apt-get remove and not just disable the ifplugd service, since something else seems to start it anyway -- or else you could read up on ifplugd configuration, I did not bother).

So, this gets a headless pi onto the network at boot and keeps it there. If you wanted a way to switch to eth while running without logging in, you could do something with udev rules for pulling the wifi dongle out.

share|improve this answer
    
Sounds worth testing. Not being a linux pro- how would I set the script to run on startup- put into cron? Would it be possible/required to add log rotation as well? –  andig Feb 26 '13 at 16:09
    
@andig : I added a few paragraphs about start-up via rc.local. WRT log rotation, that is a separate question that relates to log files in general (but see man logrotate). Right now that script will clobber (overwrite) any previous log if it exists with the same path, not append. For debugging I've sometimes used wifi.$$.log in the wifi.conf file, so that the last log is (unlikely) to get clobbered and I can compare one session to another after I've tweaked something. $$ is the pid of the "wifi" process. –  goldilocks Feb 26 '13 at 18:10
    
Works like a charm- thanks! –  andig Mar 1 '13 at 11:04
    
I worry that sometimes shell scripts can be fragile and break in hard to see ways when there are updates or if other networking software is installed. Just a note of caution on this approach. –  jeremiah Aug 29 '13 at 8:56
1  
@jeremiah : That's a good fear. I wrote the script, tweaked it over years, and feel I have some understanding of general context in which it operates and therefore what I need to watch out for. However, it is probably harder to pass on understanding than code snippets. To me it is just an alternative approach to trying to work with NetworkManager, by reducing the situation to some very basic elements. This is something to offer people with a similar perspective as "this is what's worked for me" across the linux distro spectrum in the circumstance of a simple stand-alone system. –  goldilocks Aug 29 '13 at 9:28

Well, there is a very simple solution:

  1. Go to /etc/ifplugd/action.d/ and rename the ifupdown file to ifupdown.original
  2. Then do: cp /etc/wpa_supplicant/ifupdown.sh ./ifupdown
  3. Finally: sudo reboot

That's all. Test this by turning off/on your AP; you should see that your Raspberry Pi properly reconnects.

share|improve this answer
2  
This is the simplest and most effective answer. Tested on 2 Rpi's and it works. Found this link from: raspberrypi.org/phpBB3/… –  Mauvis Ledford Jan 9 at 4:42
1  
Sorry for a simplistic question - why does this work? –  Jeff Meatball Yang Feb 18 at 1:24
    
@JeffMeatballYang, start a new thread for your question. –  AndaluZ Feb 18 at 8:38

I recently stumbled across a console application that sorts all the wireless configuration hell out. You can also use this tool to configure the LAN interface.

sudo apt-get install wicd-curses

It will install quite a few other packages and run its own daemon in the background but it sure makes configuring everything a whole lot easier. Run it with

sudo wicd-cruses

enter image description here

If you get a message saying no networks detected press P (must be capital so use [SHIFT]p) and type in wlan0 in the wireless interface field and press F10 to save.

  1. R to refresh the list.
  2. Use the cursors on the keybaord to navigate up and down the list
  3. Press right to configure the wireless connection
  4. Press down a few times and check "Automatically connect to this network"
  5. Pres downa few times again and ype in your password in the key field
  6. Press F10 to save

enter image description here

You might have to press C to connect to the access point. If you were wired that will most likely kill the LAN interface and bring up wireless.

It is also manages the connection so it will reconnect to any configured wireless access points if it drops out for whatever reason.

I tested plugging out the WiFi adapter and plugging it back in. It takes about 60~90 seconds but it will connect back to WiFi (I think the LAN must be unplugged though)

Hope it helps!

share|improve this answer
    
Not sure how that takes care of the reconnection- initial connect wasn't the question here! –  andig Sep 25 '13 at 14:44
1  
doh: too hurried- thanks! –  andig Sep 25 '13 at 17:14
    
I updated the answer because there is a step where you have to check connect automatically. Also in preferences you should scroll all the way down. The last option is off screen. It also says Automatically try to connect to networks for Wifi(it is ON by default). I can even take out the WiFi adapter plug in back in and it will reconnect within 2 minutes.(I did this without LAN plugged in) This is really the best way I have found to configure and manage wireless. You also get the GUI version just called wicd –  ppumkin Sep 25 '13 at 18:54

netcfg

Try netcfg. Without further details about which distribution you are using, I can't provide many more details, but it should do what you want.

share|improve this answer
    
I'm using the current raspbian distro: pi@raspberrypi ~ $ man netcfg No manual entry for netcfg pi@raspberrypi ~ $ netcfg -bash: netcfg: command not found pi@raspberrypi ~ $ apt-cache search netcfg pi@raspberrypi ~ $ –  andig Dec 28 '12 at 8:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.