Since my Pi is stationary at home and uses only one WiFi network, I wrote some scripts to make it automatically reconnect upon lost WiFi connections.
#!/bin/bash
TESTIP=192.168.1.1
ping -c4 ${TESTIP} > /dev/null
if [ $? != 0 ]
then
logger -t $0 "WiFi seems down, restarting"
ifdown --force wlan0
ifup wlan0
else
logger -t $0 "WiFi seems up."
fi
You can put this script under /usr/local/bin and add the following line to the system wide /etc/crontab
:
*/5 * * * * root /usr/local/bin/testwifi.sh
This will check every five minutes if the connection is still up, and restart it, if the router cannot be pinged. If you dislike all the syslog messages, you can comment them out in the script.
My corresponding /etc/network/interfaces
looks like this (I uninstalled all the network managers):
auto lo
iface lo inet loopback
iface eth0 inet static
address 192.168.3.42
netmask 255.255.255.0
auto wlan0
iface wlan0 inet dhcp
pre-up wpa_supplicant -Dwext -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf -B
post-down killall wpa_supplicant ; rmmod 8192cu ; modprobe 8192cu
iface default inet dhcp
This is for my WiFi dongle, which uses the 8192cu driver. If yours uses a different one, you need to adjust the above script.
You need to adjust the TESTIP
to an IP of your liking (e.g. your Router), and you need to provide /etc/wpa_supplicant/wpa_supplicant.conf
. An example config looks like this:
network={
ssid="myssid"
psk=12345
}
There are many more configuration options for WiFi networks. So if it does not work, you may have to read a bit about that.