Sunday, February 9, 2014

Setting up automatic Wireless Connection for Raspberry Pi

Well, like many others I also bought Raspberry pi (I think a year ago) but didn't do much with it. I decided to do a Garage door opener project and bought Relay & USB Wireless adapter (http://www.amazon.com/gp/product/B003MTTJOY/ $9.99). I started fresh with Rpi new install from http://www.raspberrypi.org/downloads and used "NOOBS (offline and network install)" to install Raspbian OS. Then enabled remote SSH so that I can connect to it from my Mac and I can use the USP ports for attaching Wireless receiver. I have used tightvncserver & tightvnc viewer java client to RDP into Rpi.

From there launch "WiFi Config", and select "scan".



 It will list all available networks and from that select your network and "Double Click" on it. It will bring another window with settings preselected, you just need to enter the password and hit "add"




This will automatically save your wireless network profile to "/etc/wpa_supplicant/wpa_supplicant.conf" file.
You can as well list the contents using 
$sudo cat /etc/wpa_supplicant/wpa_supplicant.conf

--------------------------------------------------
pi@raspberrypi ~ $ sudo cat /etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="Your SSID Name"
psk="Your SSID Password"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP
auth_alg=OPEN
}
-------------------------------------------------------------------

At this time, you can unplug your ethernet cable and still you will be able to access internet through wifi adapter. However if you restart your Rpi, your Wifi connection will not start automatically.
Follow the steps below to enable the wireless connection to startup automatically.


Note: You can't edit interfaces file directly since it is owned by root. You have to use sudo for the same

Either use nano or vi editor to match the content similar to mine

pi@raspberrypi ~ $sudo nano /etc/network/interfaces
or
pi@raspberrypi ~ $sudo vi /etc/network/interfaces
-------------------------------------------------------------------------
auto wlan0

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
-------------------------------------------------------------------------

Next edit wpa_supplicant.conf in the sameway

pi@raspberrypi ~ $sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
or
pi@raspberrypi ~ $sudo vi /etc/wpa_supplicant/wpa_supplicant.conf

-------------------------------------------------------------------------
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="Your Network SSID"
psk="Your Network Password"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP
auth_alg=OPEN
}

-------------------------------------------------------------------------
Note: proto, keymgmt can be different based upon your network security.
You can use the following blog for that reference, it's kind of the same blog, but didn't work for me.

Now you can restart your pi and enjoy it.

PS: I have used the following blog to setup my wifi, but neither did work.
Hence wrote this blog to show what worked for me.