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?
8 Answers 8
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
check_interval=5
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, . including ifplugd
and other networking daemons, or this will interfere with your efforts:
How can I disable autoconfigured networking on Raspbian?
I in fact used apt-get remove ifplugd
.
To start networking at boot (since I use the pi headless), I have this set to run on raspbian from /etc/rc.local
:
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).
For this to work you will probably have to
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.
-
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– andig2013年02月26日 16:09:02 +00:00Commented Feb 26, 2013 at 16:09
-
1@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 seeman 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 usedwifi.$$.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– goldilocks2013年02月26日 18:10:13 +00:00Commented Feb 26, 2013 at 18:10 -
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– jeremiah2013年08月29日 08:56:16 +00:00Commented Aug 29, 2013 at 8:56
-
3@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– goldilocks2013年08月29日 09:28:30 +00:00Commented Aug 29, 2013 at 9:28
-
So: I am aware of this post and intend to keep it updated if one of the major distro streams (debia and fedora and derived) introduces something that messes with it. I will also add some emphasis to the points about what needs to be disabled and how.goldilocks– goldilocks2013年08月29日 09:31:29 +00:00Commented Aug 29, 2013 at 9:31
(Only for RPi 1 and RPi 2, not RPi 3+. "does not work on dhcpcd based distros")
Well, there is a very simple solution:
- Go to
/etc/ifplugd/action.d/
and rename theifupdown
file toifupdown.original
- Then do:
cp /etc/wpa_supplicant/ifupdown.sh ./ifupdown
- Finally:
sudo reboot
That's all. Test this by turning off/on your AP; you should see that your Raspberry Pi properly reconnects.
-
2This is the simplest and most effective answer. Tested on 2 Rpi's and it works. Found this link from: raspberrypi.org/phpBB3/…Mauvis Ledford– Mauvis Ledford2014年01月09日 04:42:17 +00:00Commented Jan 9, 2014 at 4:42
-
19Sorry for a simplistic question - why does this work?Jeff Meatball Yang– Jeff Meatball Yang2014年02月18日 01:24:40 +00:00Commented Feb 18, 2014 at 1:24
-
@JeffMeatballYang, start a new thread for your question.AndaluZ– AndaluZ2014年02月18日 08:38:34 +00:00Commented Feb 18, 2014 at 8:38
-
9For me there were no ifupdown file there, just a link
action_wpa -> ../../wpa_supplicant/action_wpa.sh
, so maybe this is the default now ?Zitrax– Zitrax2016年03月27日 14:42:31 +00:00Commented Mar 27, 2016 at 14:42 -
2Yep, like others say: this does not work on
dhcpcd
based distros, in order to get this running on RPI3 you need toapt-get install rcconf
and switch fromdhcpcd
tonetworking
test30– test302018年10月30日 21:16:33 +00:00Commented Oct 30, 2018 at 21:16
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-curses
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.
R
to refresh the list.- Use the cursors on the keyboard to navigate up and down the list
- Press right to configure the wireless connection
- Press down a few times and check "Automatically connect to this network"
- Press down a few times again and type in your password in the key field
- 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!
-
2Not sure how that takes care of the reconnection- initial connect wasn't the question here!andig– andig2013年09月25日 14:44:29 +00:00Commented Sep 25, 2013 at 14:44
-
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
Piotr Kula– Piotr Kula2013年09月25日 18:54:47 +00:00Commented Sep 25, 2013 at 18:54 -
there's a typo in the command "sudo wicd-cruses" 'u' and 'r' are exchangedGawcio– Gawcio2014年11月14日 23:38:17 +00:00Commented Nov 14, 2014 at 23:38
-
1After installing
wicd-curses
, my RPi 3 started freezing randomly. Was never freezing before and freezing disappeared immediately once I uninstalled it. Beware!akhmed– akhmed2016年06月14日 05:22:16 +00:00Commented Jun 14, 2016 at 5:22 -
1After installing wicd-curses, my RPi 3s running Jessie Lite have not frozen and do stay connected. But today I tried connecting to one by the ethernet port, and found the interface would be lost a few seconds after boot. I was able to connect fine before configuring wicd-curses to connect to wifi.Chris Jenks– Chris Jenks2016年06月17日 22:44:43 +00:00Commented Jun 17, 2016 at 22:44
This can be solved with no extra scripts.
In /etc/network/interfaces put:
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
The roaming allows the interface to self-heal.
-
The last line seems to add a second IP address to the wlan0 interface; is that intentional?fche– fche2016年05月18日 22:07:04 +00:00Commented May 18, 2016 at 22:07
-
I only end up with one DHCP-assigned address. It wouldn't surprise me entirely if the boot scripts have changed behaviour though - this config file is unnecessarily arcane...David Sainty– David Sainty2016年05月23日 12:49:52 +00:00Commented May 23, 2016 at 12:49
-
4On my raspbian pi2 box, if the wlan goes down long enough for three cycles of authentication attempts to fail, even with "wpa-roam" it doesn't try again.fche– fche2016年05月23日 18:39:46 +00:00Commented May 23, 2016 at 18:39
-
This is the only answer that worked for me on RPi 3. Thanks!akhmed– akhmed2016年06月14日 16:48:58 +00:00Commented Jun 14, 2016 at 16:48
-
I know this is old, but can I add a second (or third...) wpa-roam line for different APs/networks? And what happens if 2 of them are in range at the same time? First come first served?Dave– Dave2021年11月10日 13:36:48 +00:00Commented Nov 10, 2021 at 13:36
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.
-
5I'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– andig2012年12月28日 08:20:19 +00:00Commented Dec 28, 2012 at 8:20
This worked for me, using Raspian Jessie on 30.March.2017:
http://alexba.in/blog/2015/01/14/automatically-reconnecting-wifi-on-a-raspberrypi/
-
1Please summarize the solution in your post, in case the link goes dead.tlhIngan– tlhIngan2017年03月31日 03:05:42 +00:00Commented Mar 31, 2017 at 3:05
-
1CMB, welcome to the RPi flavored corner of StackExchange. Site policy discourages link only answers as they're subject to link rot. You don't need to duplicate the contents of the link, but a good answer should at least expand on the highlights contained in the link.Jacobm001– Jacobm0012017年03月31日 03:31:06 +00:00Commented Mar 31, 2017 at 3:31
-
Further to Jacobm001's comment above, in keeping with our policy regarding informationless link-only answers, if this post is not edited to contain information that can stand as an answer, however minimal, in 48 hours it will be converted to Community Wiki to simplify having it corrected by the community.goobering– goobering2017年03月31日 11:44:52 +00:00Commented Mar 31, 2017 at 11:44
Another solution, as extracted from this one.
First configure your wifi settings: sudo vi /etc/wpa_supplicant/wpa_supplicant.conf
adding a section like this:
network={
ssid="MyNetworkName"
psk="MyPaz0rdz"
key_mgmt=WPA-PSK
}
Then your network settings with sudo vi /etc/dhcpcd.conf
:
interface wlan0
inform 192.168.1.200 # the static ip for the wifi card
static routers=192.168.1.254 # your router's ip
static domain_name_servers=192.168.1.254 # your dns, usually=your router
Then create this script file somewhere, for example in /home/pi/reconnect.sh
and give it +x
permission to be executable.
#!/bin/bash
router=`ip route | awk '/default/ {print 3ドル}'`
/bin/ping -q -c1 $router > /dev/null
if [ $? -eq 0 ]
then
true
# echo "Network OK"
else
echo "Network down, fixing..."
# ifdown --force wlan0
# sleep 5
/bin/kill -9 `pidof wpa_supplicant`
/sbin/ifup --force wlan0
/sbin/ip route add default via $router dev wlan0
/bin/mount -a
echo "wlan0 reconnected at `date`"
fi
Then login as root with sudo su
and edit root's crontab with crontab -e
.
At the end of this file add this line:
*/10 * * * * /home/pi/reconnect.sh
That means: "execute that script every 10 minutes (as root)" - note without the */ at the start it would be "execute at 10 minutes past the hour, every hour".
Now, if you have a connection then the script will exit and nothing will happen. If you can't ping your router then the script will try to reconnect to it using wpa_supplicant.
Notice that everything that is echoed from a cron script (in this case only when disconnected) will be emailed to root. So if you have configured your mail aliases and postscript then you will receive an email whenever your pi's wifi connection went down and then succefully back again thanks to the script.
Somehow the rest of the answers were still not satisfactory. There still doesn't seem to be a standard functionality (for such a basic function) which just works, or works with few extra settings, which also works in RPi 3 and up.
Then there are some scripts which either make things too complicated or need hardcoding addresses, or rely on custom assumptions. For example, what does it help to try pinging default route or 8.8.8.8, if the Wifi maybe is local-only? What if it doesn't set a default route at all? What if the default route is set on eth0 and is always available and wifi is used for other purposes? The question is "how to reconnect WiFi".
This is all you need:
#!/bin/bash
SSID=$(/sbin/iwgetid --raw)
if [ -z "$SSID" ]
then
ifconfig wlan0 down
sleep 30
ifconfig wlan0 up
fi
Just put that on auto-repeat/cron as root and you're set. This solution is from https://gist.github.com/carry0987/372b9fefdd8041d0374f4e08fbf052b1 which has more details as well.
PS in case there is an obscure OS or RPi version, try using "ip link set wlan0 down
/ ip link set wlan0 up
" instead of ifconfig ...
.
-
If you are still running an obsolete 2013 OS that may work, but even then is a BAD idea.Milliways– Milliways2021年02月27日 23:09:44 +00:00Commented Feb 27, 2021 at 23:09
-
1Can you explain? iwgetid seems to work on the latest raspbian.Cray– Cray2021年02月28日 07:51:14 +00:00Commented Feb 28, 2021 at 7:51