, , , ,

Ross has joined the ranks of Pi owners, to provide computing for a (personal) cat-friendly art project destined for his living room. Additional details will leak out from time to time; this is the first installment of what will likely be several posts as the project makes progress.

Configuring a Headless Raspberry Pi for WiFi

The Raspberry Pi is a very inexpensive compact board hosting a Broadcom SOC containing enough resources to boot and run Linux from an SD card. There are currently three models: the A+, B+, and Compute Module.

Console Cable

A serial console cable allows us to speak to the Pi without concern for its current network configuration (or more likely after experimentation, misconfiguration). When shopping for a cable, look for one that runs at 3.3V logic levels. Unless you add cables and a connector, the Pi’s console port is located on three of the pins of the large GPIO header, so a serial adapter that terminates in individual wires with square-pin compatible sockets is a good idea.

Caution: do not use an RS-232 interface, it will put substantially higher voltages on the Pi’s GPIO pins and could damage your Pi. Fortunately, most RS-232 USB interfaces are terminated in a 9-pin D shell imitating the standard connector that used to be found on the backs of most PCs.

I have this cable from Adafruit based on the Prolific PL2303 chip that is terminated in individual square pin sockets which is easy to use for this purpose. Connect it as follows:

Cable signal Color GPIO Description
USB VBus 5V RED (2, 4) Pi 5V, but don’t connect this pin
GND BLK 6 Pi GND ref
Rx (in) WHT 8 Pi UART0_TxD
Tx (out) GRN 10 Pi UART0_RxD

Configure the PC COM port to 115200 baud, 8 bit, no parity, 1 stop bit, and no flow control. Use your favorite terminal emulator to open the port and interact with the console. (I use Tera Term, and PuTTY is also popular.)

WiFi Hardware

The Pi does not provide a WiFi radio out of the box. However, the model B+ does provide 4 USB host ports, so as long as a sufficiently powerful 5V supply is used a suitable radio can be plugged in there.

The Raspbian distribution includes a kernel with compiled-in support for RealTek chipsets, so a module based on their chip would be easy. Fortunately, these chips are quite common in typical cheap USB WiFi warts. Adafruit sells two models, one small unit that is barely larger than the USB A socket, and a larger one that includes an antenna. I bought the tiny one.

Installation is trivial, just plug it in to an available USB port and ignore it. The B+ even supports hot plugging, so you should be able to start using it without a reboot. If in doubt, reboot anyway, the Pi doesn’t take long to boot.

Default Network Configuration

The key to the network configuration in any Debian-derived Linux distribution (e.g. both Raspbian and Ubuntu) is the file /etc/network/interfaces. Its default content in Raspbian is:

auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
iface wlan0 inet manual
  wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

The file /etc/wpa_supplicant/wpa_supplicant.conf contains parameters for wpa_supplicant which is the daemon that interacts with a WiFi access point, exchanges credentials, and creates a network interface. Note that as most commonly used, this file will contain your WiFi pass phrase as clear text. For that reason, be careful when sharing it to avoid accidentally revealing keys. The system will require that it be owned by root, and readable only by root for your safety. Its default content is:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

Out of the box, with an ethernet cable connected and a DHCP server on the local network, the Pi will go get itself an address.

Configure WiFi

I want the WiFi to connect and use DHCP when available, and have known networks at home and the office. Just one WiFi network would generally be sufficient, but I’ve included the office network to make it easy to drag the Pi in for testing the hardware I plan to add.

It turns out that the easy way to get that effect is to have wpa_supplicant name the network it finds, and to specify address details for each in interfaces using those nicknames.

To do this, the file /etc/wpa_supplicant/wpa_supplicant.conf is edited to look like this:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

        psk="xxxx xxxx xxxx xxxx"

        psk="xxxx xxxx xxxx xxxx"

The first two lines are untouched from the default file. I’ve added a network={...} stanza for each WiFi network I plan to use. If you are configuring a Pi to use additional networks, then list them in additional stanzas. If you are using WEP, (not recommended other than for guest networks that need “screen door” level security) change the key_mgmt to be none.

Then to /etc/network/interfaces, add stanzas to describe each of home and work. I’ve simply said:

iface home inet dhcp
iface work inet dhcp

If you need a static IP address assigned in either location (which I likely will want at home) making it read something like this (with the obvious changes to suit your actual network) will do the trick :

iface home inet static
iface work inet dhcp

Finding the Pi

If you’ve let the Pi use DHCP, then you won’t always know what address it was assigned, and that makes it hard to locate it from other machines on the local network.

The solution to this problem is to let the local network run a self-configuring and self-healing DNS where names can be resolved by just asking on the local net if anyone knows the answer. Apple originated a protocol for this, and got it carried through the process as RFC6762 also known as zeroconf or bonjour. It is implemented out of the box by Macs, and is provided by the Avahi project in Linux. On many Linux distributions, Avahi is configured by default. In Raspbian it appears to not be, but is easily added. Unfortunately, Microsoft has been slow to officially support the protocol (probably at least partly due to business relations with Apple), but Apple does supply Bonjour for Windows.

Installation of the Bonjour Print Services for Windows is a simple way to get the needed system components without collecting too much other stuff. If you already have a recent enough QuickTime installed, you might already have Bonjour as it is part of QuickTime. Some other software also bundles Bonjour, and if you are already an Apple developer there is an SDK available to allow you to include Bonjour in your software product as well.

On the Pi, installation is easy, with the possible pitfall that the package databases seem to get out of date swiftly. So the commands:

pi@raspberrypi:~$ sudo apt-get update
pi@raspberrypi:~$ sudo apt-get install avahi-daemon

will, with a little patience, get the avahi daemon installed and running. At that point, the Pi will be discoverable by all machines on the local network as hostname.local, or raspberrypi.local if you haven’t changed the default host name.

If you do this and have multiple Pis, be sure to give each one a unique host name both for your own sanity, and so that you never end up with two on the same net both claiming to be the one true raspberrypi.local.

(Written with StackEdit.)