Chapter 4. Setting Up the Wireless

As you’ve seen, the Raspberry Pi is a full-fledged computer, if a bit smaller and underpowered compared to what the average user is accustomed to. Almost immediately after taking it out of the box, you can insert a formatted hard drive (the SD card), plug in an HDMI-capable monitor, add a USB keyboard and a mouse, and start computing. So even if your desk looks like Figure 4-1, the Pi is so small that you can power it up and start programming. It doesn’t even need airflow; even when it’s overclocked, the ARM processor doesn’t get hot enough to need any appreciable cooling, so you can put it in a drawer or a project box and run it that way. (Yes, you can even put it in an Altoids tin, though there are a few strategic cuts you’ll need to make to enable it to fit.)

My desk on a good day
Figure 4-1. My desk on a good day

Once it’s running, plug in an Ethernet cable and connect the Pi to your modem or router, and you can surf the Web, install updates and upgrades, change your Facebook status, and any number of other miscellaneous tasks that require Internet access, using either the built-in Midori or NetSurf browsers, or a third-party browser such as Firefox (called Iceweasel on the Pi) or even Google Chrome (called Chromium on the Pi).

But what if you don’t want your Pi to be tethered to your modem or router, or even your desktop? After all, one of the huge advantages of the Pi is its size and portability; it’d be a shame to be stuck working at your desk. It’d be especially inconvenient to have a robot’s brain (for example) tied to a modem, and the robot’s mobility limited by the length of whatever Ethernet cable you happened to have lying around. And forget moving around outside. Robots need to be mobile, not tied down.

Perhaps a future version of the Pi (model C?) will come with an onboard wireless chip, even though it would probably make the Pi a bit larger. I’m not aware of any plans for a wireless model, though, which means that for the time being we’ve got to be proactive and add a wireless USB adapter, also called a dongle, to give the Pi wireless connectivity.

If you’ve been playing with the Raspberry Pi from the beginning, you may be aware of a few problems getting it to work wirelessly. Some of those problems have stemmed from the Pi being Linux based. Other problems arise from the Pi’s use of an ARM processor. (That may seem strange, as ARM processors are at the heart of millions of smartphones worldwide, but unlike the Pi, cell phones don’t use a USB wireless adapter to connect to WiFi.)

Times have changed, of course, and (with a few unremarkable exceptions) Linux’s wireless problems have become virtually nonexistent. That is, until early adopters fired up the Pi, plugged in a USB dongle, and tried to get online in the coffee shop (Figure 4-2).

If the Pi recognized the wireless adapter at all, it seemed a miracle. Most of the time the adapter didn’t fire up, but if it did, the Pi didn’t recognize it as a network device. Linux drivers may or may not have existed for different devices, but even if they did, they didn’t work on Raspbian for whatever reason. Raspberry Pi forums filled with the lonely cries of stranded users: “How do I get wireless working on my Pi?” Luckily, the Pi community worked together and determined that all you need is the right adapter.

As it turns out, only wireless adapters with certain chipsets play nicely with the Pi, something that many of us found out the hard way. Chances are extremely good that the D-Link or Netgear adapter you picked up from the big box store will run sketchily on the Pi, if it runs at all. Sometimes you may get lucky; if the chipset is compatible and the adapter doesn’t draw too much current, you’re golden. If, however, the adapter draws too much current, you’ll most likely experience other problems, such as sporadic mouse and keyboard connectivity problems, and even intermittent shutdowns. For that reason alone, it’s always a good idea to plug your wireless adapter directly into your Pi’s USB port, and plug the mouse and keyboard—if you’re using them—into a USB hub, assuming that you’re using a nonpowered hub. If you use a powered USB hub, you can plug the adapter into it without problems. Otherwise, the Pi may not send enough current through the hub to power the adapter.

The thriving Raspberry Pi community has experimented with any number of adapters, saving us from having to go through them all one by one. As it turns out, those dongles with the Ralink RTL8188CUS chipset will work almost without exception. The Pi community has found that the following two adapters work excellently:

Both of these are available on Amazon and eBay for reasonable prices (less than $10). In fact, I suggest you order more than one; they’re so small that I’ve actually lost a few, and when (not if) you order another Pi, you can equip it as soon as it arrives. If you choose to go off on your own and explore the compatibility of a different adapter and/or chipset, and if you have some luck getting it to work, you’re encouraged to post your findings on the Raspberry Pi forums.

Making It Work: The GUI Way

When you get your adapter, plug it into the Pi. On your Pi’s desktop, you’ll see an icon called WiFi Config (Figure 4-5). Double-click it, and you’ll see the configuration screen (Figure 4-6).

The adapter, wlan0, should show up in the top drop-down list, and your wireless network should show up below that. With your network selected, click the Manage Networks tab. Select your network and click Edit, which will bring up the NetworkConfig window (Figure 4-7).

Enter your network information and click Save. Now just go back to the Current Status tab and click the Connect button. The adapter should negotiate a lease from your DHCP-enabled router and hop onto your wireless network. To make sure it worked, in your terminal window, type ifconfig. You should see three Ethernet adapters listed: eth0, lo, and wlan0 (the newest one). Assuming you’re still connected via Ethernet cable, your eth0 adapter should have an inet address listed, and now your wlan0 adapter should have one as well. Voilà! Your wireless is now working!

On the other hand, perhaps this whole process didn’t work. Or perhaps you’re feeling nostalgic for the old days, when you had to wire up your adapter (no pun intended) with bubble gum and baling wire. So let’s look at the process of setting up a wireless adapter using the command line and editing some files.

It’s pretty easy, and requires only one package and one file edit. First, in your terminal, update and upgrade your Pi:

sudo apt-get update
sudo apt-get upgrade

This is always a good idea before a computing session when you’re going to be installing new packages and software. Many Linux distros notify you of available updates; Raspbian, however, does not, so checking once a day is a good habit to get into.

Once you’re upgraded, try the following:

sudo apt-get install wpasupplicant

You’ll most likely get a message stating that wpasupplicant is already the newest version; I believe it’s included in all newer releases of Raspbian. In case you have an old version, however, you’ll need to download and install it.

Once it’s installed, use a text editor to open its configuration file:

sudo nano /etc/wpa_supplicant/
wpa_supplicant.conf

Here is where you put in your network information. (Coincidentally, it’s where the information is stored if you enter it via the NetworkConfig GUI in the previous section.) Leave the first lines of the file as they are, but add the following lines:

network={
   ssid="_your network id_"
   psk="_your network key_"
   proto=WPA
   key_mgmt=WPA-PSK
   pairwise=TKIP
   auth_alg=OPEN
}

Replace your network id and your network key with your network name and key. (Your network key is the password you’ve set to log into your network from your devices.) It’s true, you’re putting your network password into a configuration file, and it’s in plain text. For those of you with the tinfoil hats, this means that if your Pi falls into your arch-enemy’s hands, that person will have the key to your network. Unfortunately, there’s no way around this. It is a configuration file, after all, and the Pi’s operating system must be able to “read” it. You can only hope that any nemesis who gets their hands on your rover is not Linux-savvy.

You may have to fool with the settings to match your network configuration; some older versions of wpasupplicant won’t work with WEP authentication. Think of it as a kick in the pants to change your security settings if you haven’t already; WEP is not a secure standard.

Save the file and reboot your Pi:

sudo shutdown -r now

When your Pi comes back up, your wireless adapter should connect to your WiFi router.

Getting your wireless working, although impressive, is only half the battle. You’ll also need to set a static IP for your adapter. If you don’t, it’s entirely possible that every time you power on your Pi, it’ll receive a different IP address from your router. Because you need to log in remotely in order to control your robot (for those times you’re controlling it on your home network), you’re going to need to know the IP address it’s using. When you set a static IP, the router will always assign the same numeric sequence to your Pi’s wireless adapter.

This part is easy. In your terminal, open your interfaces file:

sudo nano /etc/network/interfaces

If this is the first time you’ve opened the file, it’s probably going to look something like this:

auto lo

iface lo inet loopback
iface eth0 inet dhcp

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

The first part of the file is fine, but you’re going to edit the rest of it to connect to your network without using DHCP. Just a warning: again, this requires putting your network SSID and password in plain text in your interfaces file, so try to keep your Pi out of your enemies’ hands.

Edit the file so it looks like this (I’m using my own address, netmask, and other values—substitute your own):

auto lo

iface lo inet loopback
iface eth0 inet dhcp

auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.2.60
netmask 255.255.255.0
broadcast 192.168.2.255
gateway 192.168.2.1
dns-nameservers 8.8.8.8 8.8.4.4
wpa-passphrase my-passphrase
wpa-ssid my-ssid

The address is the IP address you’d like to assign to your Pi. The netmask establishes the subnet you’re using; unless you have a huge subnet in your house, chances are yours will also be 255.255.255.0. The broadcast value will mirror the first three values of your subnet, with a .255 at the end, and the gateway is the IP address of the router on your network.

You’ll notice you don’t need the wpa_supplicant and iface default lines; you can either remove them or comment them out by placing a # at the beginning of the line. When you’ve edited the file, save it, and reboot your Pi with the following:

sudo shutdown -r now

When your Pi comes back up, you should be connected to your network with full Internet access and a static IP. To test it, open a terminal and first type ifconfig and make sure that the address listed for wlan0 is the one you chose and entered in the interfaces file. Finally, make sure you can access the network by typing ping google.com and see that you get some responses.

Now that your Pi has a static IP, and because you enabled the SSH server when you set up your Pi with the raspi-config tool (see Appendix A if you need help with this), you can now log into your Pi with this command, giving it the password raspberry when you’re prompted:

ssh -l pi 192.168.2.60

If for some reason this fails, fear not—all is not lost. You will, however, need to connect physically to your Pi with a keyboard/mouse/monitor combination to proceed. (For what it’s worth, I always physically connect to my Pi this way when I’m screwing with networking stuff. That way, I still have access to it, even if I completely kill my network adapter.)

When you have the Pi hooked up to a monitor, open a terminal and type ifconfig to see exactly what IP addresses you do have. You should have three adapters listed: eth0, lo, and wlan0. If the adapter is working and active, it should have an inet addr: listed in the second line of its description. If there is no address listed, and you’ve followed all of the preceding steps, you may have a defective wireless adapter. Try another dongle (preferably one with the chipset mentioned earlier) and see if you have better luck.

No, this doesn’t mean you’re decapitating your Pi. A headless configuration refers to the practice of doing your Pi-related work over a network connection, rather than attaching it to a monitor, keyboard, and mouse. I do all of my Pi work this way, as do many other users. With the wireless adapter working and a static IP address configured, you can store the Pi anywhere in your house or office, and simply log into it to work on it. It’s also how you’ll be working with it after it’s set up on the rover, as you’re not going to be following the rover around with a keyboard and a monitor.

The main protocol you’ll be using to log into your Pi remotely is Secure Shell, or SSH. If you’re using a Mac or a Linux machine as your desktop computer, you won’t need any special tools, as those machines have SSH clients built in. The Pi has a built-in SSH server, which can be activated (if you didn’t already) using the raspi-config tool (see Appendix A for more information). If you’re using Windows, you’ll need a client called PuTTY. No installation is necessary; just download the program and place it on your desktop or wherever you’d like.

When you’re ready to log in, just open an SSH connection to your Pi’s IP address. On a Mac or Linux machine, the syntax is as follows:

ssh -l pi 192.168.2.60

(Using whatever your Pi’s IP address is, and assuming you haven’t changed the default username from pi.) Then just enter the password when prompted, and you’ll be working on the Pi via the command line, as if you had a terminal open in a standard desktop session. On a Windows machine, just open PuTTY, leave everything as the default, put the Pi’s IP into the Host Name (or IP Address) field, and click Open.

If you would like to see a desktop interface but still want to work remotely, you can configure the Pi as a Virtual Network Computing (VNC) server. Once the server is installed, you can start it on the Pi, start up a VNC client on your desktop machine, and again work remotely, only with a full desktop. The easiest package to get running (in my opinion) is TightVNC. Open a terminal on the Pi and install it with the following:

sudo apt-get install tightvncserver

Once it’s installed, you can start the server with a command line:

vncserver :1 -geometry 1024x768 -depth 16

Now all you need to do is install a VNC client on your desktop machine (I use Chicken of the VNC on my Mac) and connect to the Pi’s VNC server.

Setting Up an Ad Hoc Network

The last thing I’d like to go over in this chapter is setting up your Pi to form an ad hoc network. Although being wirelessly connected to a network is great if you’re at home or school or the office, you won’t be able to connect to your rover if there’s no local network. And because rovers do best, well…roving, you’ll probably want to take it outside.

The solution here is to set your Pi to become a server of an ad hoc network. With its own DHCP service, it can hand out DHCP leases to your laptop or your phone, allowing you to ssh directly into it, even outdoors. (An ad hoc network is simply a small, computer-to-computer network. If you were to connect your Pi directly to your laptop with an Ethernet cable, you would have a small, wired, ad hoc network.)

You’ll need to edit your interfaces file again with this command:

sudo nano /etc/network/interfaces

You may want to save the current file as interfaces-old or something similar before you start poking around. I sometimes switch back and forth between interfaces files, depending on my plans for the Pi that day. Sometimes it’s just easier that way.

When it’s saved, edit the wireless portion of the file so it looks like the following:

auto wlan0
iface wlan0 inet static
address 192.168.1.1
netmask 255.255.255.0
wireless-channel 1
wireless-essid RPiWireless
wireless-mode ad-hoc

Save it and exit. Then restart your wireless connection with the following:

sudo ifdown wlan0
sudo ifup wlan0

Now you should be able to see a network called RPiWireless from any other computer with a wireless connection.

Don’t try to connect to it yet, however. Your Raspberry Pi has created an ad hoc network, and is broadcasting it, but that’s all your Pi is doing. Any other computers trying to connect to that network will never be assigned an IP address, and thus will never be able to connect to it and communicate with the Pi. We need to set up the Pi to assign IP addresses to connecting computers, and we’ll do that with a DHCP package called, appropriately, isc-dhcp-server. First, update your Pi and then grab the package:

sudo apt-get update
sudo apt-get install isc-dhcp-server

When it’s finished installing, you need to configure it using its configuration file. Open it with this command:

sudo nano /etc/dhcp/dhcpd.conf

There’s a lot of preconfigured information in that file. In general, you’ll want the only uncommented lines to be like this:

ddns-update-style interim;
default-lease-time 600;
max-lease-time 7200;
authoritative;
log-facility local7;
subnet 192.168.1.0 netmask 255.255.255.0 {
 range 192.168.1.5 192.168.1.100;
}

Now restart your Pi and look for the wireless network again with your other computer. Not only should the network show up, but you should be able to connect to it. Once you’re connected to the Pi’s ad hoc network, you can ssh into your Pi by typing ssh -l pi 192.168.1.1. This is the command you’ll use to connect to your rover after you take it outside, away from any WiFi networks. Moreover, with the DHCP server running, you won’t have to set up your laptop with a static IP address; the Pi will assign you one every time you connect to the network. You can assign your laptop a static IP if you wish, but it’s unnecessary, particularly because that IP address will be valid only during the times you’re logged into the Pi’s ad hoc network. Otherwise, you’ll have to reconfigure your laptop’s IP address every time you log on and off the Pi’s network.

Now that we have the wireless working, let’s look at building the rover.