Thursday, January 16, 2014

Wi-Pi : 802.11 Networking for Raspberry Pi (EW-7811Un)

One of the most conspicuously absent standard interfaces on the Raspberry Pi is built-in support for 802.11 WiFi wireless LAN networking.

A low-cost, low-power way to remedy this is for example the Edimax EW-781Un USB WiFi adapter which plugs easily into one of the USB ports on the Raspberry Pi and is supported out of the box by the current Raspbian distribution.

It seems to be very popular for use with Raspberry Pi and is available for about $10 in many places where Raspberry Pi are sold.

WiFi Client

Connecting to an existing WiFi network is trivial, once we know the SSID and access password for the network we are trying to connect to. After plugging in the adapter, it should be automatically recognized by the linux kernel - the output of lsusb should contain an entry like this:
Bus 001 Device 004: ID 7392:7811 Edimax Technology Co., Ltd EW-7811Un 802.11n Wireless Adapter [Realtek RTL8188CUS]

In order to connect to a typical home wifi network, we only need to add the following to /etc/networks/interfaces :
allow-hotplug wlan0

iface wlan0 inet dhcp
wpa-ssid <SSID>
wpa-psk <wifi-key>

substituting <SSID> with the "name" (SSID) of the wifi network and <wifi-key> wifi access code or password configured in the router. After rebooting or running sudo ifup wlan0, the interface should be connected and configured with an address from the gateway via DHCP.

WiFi Access Point

Configuring an EW-781Un adapter as a WiFi access point is not as easy, as the RTL8188CUS chipset is not supported by the standard version of hostapd. Some tutorials like here or here are explaining how to install hostapd and replace it with a custom version which supports the chipset.

For example:
sudo apt-get install hostapd

wget http://www.daveconroy.com/wp3/wp-content/uploads/2013/07/hostapd.zip
unzip hostapd.zip 
sudo mv hostapd /usr/sbin/hostapd
chomd+x /usr/sbin/hostapd

One of the simplest use-case for an access point is to connect a tablet, phone or laptop to a Raspberry Pi as a console or controller for some application which doesn't require Internet access - e.g. a robot or a monitoring device of some sorts.

To allow most standard devices to connect to the Raspberry Pi access point, we have to configure a static IP address on the interface and set up a DHCP server to push IP interface configuration to the devices which are connecting.

For a simple static IP address add the following to /etc/networks/interfaces :
allow-hotplug wlan0

iface wlan0 inet static
address 10.0.0.1
netmask 255.255.255.0

Dnsmasq is a small footprint DHCP server and DNS server/proxy for small networks connected to the Internet masquerading behind a  NAT firewall.
sudo apt-get install dnsmasq

It can also easily serve DNS names for the local network by picking up names from the static /etc/hosts file. In order to have dnsmasq server dynamic IP addresses and DNS names in a pseudo-domain .home to devices connecting to the access point on wlan0, change /etc/dnsmask.conf to the following:
local=/home/
interface=wlan0
domain=home
dhcp-range=10.0.0.2,10.0.0.255,12h

For example, to create a network called TempSensor, create the following hostapd configuration in /etc/hostapd/hostapd.conf:
interface=wlan0
driver=rtl871xdrv
bridge=br0
ssid=TempSensor
channel=1
wmm_enabled=0
wpa=2
wpa_passphrase=tempsensor
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CMMP
rsn_pairwise=CCMP
auth_algs=1
macaddr_acl=0

and set DAEMON_CONF="/etc/hostapd/hostapd.conf"
in /etc/default/hostapd and restart.