Setting up Wifi on a Raspberry Pi

I recently got a few Rasbperry Pis and some "Ourlink" 802.11b/g/n USB Wifi dongles. They come in a low profile option but I got the larger one with hopes for improved reception. I think both use the same Realtek based chipset (RTL8188CUS).

Setting up wifi is pretty easy with the newer Rasbian releases (this guide was written for 2013-05-25-wheezy-raspbian).

Prepare The Pi

First you have to install Rasbian onto your SD card, which is beyond the scope of this guide. Try looking at the elinux.org RPi guide. This guide was written using the 2013-05-25-wheezy-raspbian release.

Once you have your SD card setup, plug in your Pi and the wifi dongle. You should update any existing software and optionally install some must haves before continuing.

# update apt-get's package index and upgrade any already existing packages
sudo apt-get update -y && sudo apt-get upgrade -y
# install some 'must haves'
sudo apt-get install -y git build-essential vim tmux curl
# reboot the pi, most likely un-necessary but the update might
# have pulled down some new firmware
sudo reboot

Setup the Wifi Interface

Some guides instruct you to either search apt for some extra packages, or install drivers from the cd that came with your dongle but I found this wasn't required.

First we'll make sure the dongle is recognized with lsusb.

pi@raspberrypi ~ $ lsusb
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. 
Bus 001 Device 004: ID 0bda:8176 Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN Adapter

As you can see, my dongle is on the last line, with the chipset listed.

Now run lsmod to see avaliable kernel modules, hopefully we'll see something that matches our chipset.

pi@raspberrypi ~ $ lsmod
Module                  Size  Used by
snd_bcm2835            16304  0 
snd_pcm                77560  1 snd_bcm2835
snd_seq                53329  0 
snd_timer              19998  2 snd_pcm,snd_seq
snd_seq_device          6438  1 snd_seq
snd                    58447  5 snd_bcm2835,snd_timer,snd_pcm,snd_seq,snd_seq_device
snd_page_alloc          5145  1 snd_pcm
8192cu                490353  0 
leds_gpio               2235  0 
led_class               3562  1 leds_gpio

The interesting module is third last (8192cu). The number doesn't exactly match our chipset but its close enough to work.

Setup the Wifi Network

We have to generate a wifi config file and add the network details. wpa_supplicant is supposed to help with this but it would not work for me.

# create the config file
pi@raspberrypi ~ $ sudo vim /etc/wpa.config
# paste the following
network={
    ssid="<your ssid>"
    proto=RSN
    key_mgmt=WPA-PSK
    pairwise=CCMP TKIP
    group=CCMP TKIP
    psk="<your wpa key>"
}

If you care about your password being saved in cleartext, you can use wpa_passphrase <ssid> <password> to encode the string and use that instead. Note, this only provides security through obscurity.

pi@raspberrypi ~ $ wpa_passphrase ssid password
network={
    ssid="ssid"
    #psk="password"
    # copy psk into /etc/wpa.config, replacing the cleartext password
    psk=44116ea881531996d8a23af58b376d70f196057429c258f529577a26e727ec1b
}

Add the wlan0 configuration to /etc/network/interfaces. I removed the existing wlan0 configuration code (but not the eth0 configuration since I still want to use ethernet sometimes). I've also included a static address configuration which might be more useful, since you might not have a screen attached to your Pi and it can be a pain if the address changes constantly.

pi@raspberrypi ~ $ sudo vim /etc/network/interfaces
# loopback
auto lo
# ethernet
iface lo inet loopback
iface eth0 inet dhcp
# wifi (dhcp config)
auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa.config
# wifi (static address config)
#auto wlan0
#iface wlan0 inet static
#    address 10.0.1.59
#    netmask 255.255.255.0
#    gateway 10.0.1.1
# wpa-conf /etc/wpa.config

Bring the wifi interface up, I've found if this doesn't work the first time, just try again.

pi@raspberrypi ~ $ sudo ifup wlan0
ioctl[SIOCSIWAP]: Operation not permitted
ioctl[SIOCSIWENCODEEXT]: Invalid argument
ioctl[SIOCSIWENCODEEXT]: Invalid argument
Internet Systems Consortium DHCP Client 4.2.2
Copyright 2004-2011 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/wlan0/00:e0:4c:10:60:fc
Sending on   LPF/wlan0/00:e0:4c:10:60:fc
Sending on   Socket/fallback
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 3
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 5
DHCPREQUEST on wlan0 to 255.255.255.255 port 67
DHCPOFFER from 10.0.1.1
DHCPACK from 10.0.1.1
bound to 10.0.1.59 -- renewal in 33153 seconds.

I found that bringing interfaces up and down can effect my ssh connection, even if it's an unrelated interface. Press ~. to kill an ssh connection if this happens to you, instead of waiting for it to time out.

Lastly check for an IP address.

pi@raspberrypi ~ $ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether b8:27:eb:79:7c:54 brd ff:ff:ff:ff:ff:ff
    inet 10.0.1.27/24 brd 10.0.1.255 scope global eth0
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:e0:4c:10:60:fc brd ff:ff:ff:ff:ff:ff
    inet 10.0.1.59/24 brd 10.0.1.255 scope global wlan0

Success, we can see our wlan0 interface has the address 10.0.1.59.

Written 25th of July, 2013