Friday 4 October 2013

Raspberrypi auto connect to wifi on boot up

Setting up a WiFi connection on the RaspberryPi

raspberrypi-wifi
I’ve recently tried to setup a WiFi connection with the RaspberryPi and encountered some issues: the connection didn’t start automatically and it wasn’t stable. I’m using an Edimax EW-7811Un USB dongle, which is supposed to be supported out of the box with Raspian Wheezy (2012-12-16).
After researching the issue, I finally have a configuration that works reliably. Here are the steps I’ve followed.

Setting up the configuration

Edit the /etc/network/interfaces file to look like this:
auto lo

iface lo inet loopback
iface eth0 inet dhcp

#auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
Then add your WiFi parameters to /etc/wpa_supplicant/wpa_supplicant.conf.
Here are my parameters to connect to my freebox (French ISP router).
network={
        ssid="YOURSSID"
        scan_ssid=1
        key_mgmt=WPA-PSK
        proto=WPA
        psk="YOURPASSWORD"
}
At this point, the you should be able to start the connection with this command:
sudo ifup wlan0
In some cases, you might need to do sudo ifup wlan0 first.

Connect automatically when booting

To make the connection work when the RaspberryPi boots, I have added a few lines to /etc/rc.local (source) :
echo "Starting WiFi..."
wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant.conf
sleep .5s
dhclient wlan0
echo "WiFi should be started"

exit 0

Keep the connection alive

To make sure that the connection stays up, I’ve done two things.
First, disable the power management of the WiFi dongle. Create a new /etc/modprobe.d/8192cu.conf file with this content (source):
options 8192cu rtw_power_mgnt=0 rtw_enusbss=0
Then, make the RaspberryPi ping the router every minute. Open your crontab:
$ crontab -e
and add this line at the end :
*/1 * * * * ping -c 1 192.168.0.254
Replace the IP address by the actual IP address of your router.
It took me some time to get it right, but now the Pi connection seems to work fine.

3 comments:

  1. I want to connect my raspberry pi3 to any open wifi network. Is there any way to do that. Thanks

    ReplyDelete
  2. You need the SSID unfortunately i am not aware of a way to do it automatically

    ReplyDelete

Amazon

Donate

Donate Towards More Raspberry PI's for Projects