Wi-Fi is a convenient way to allow your Raspberry Pi to operate in a wireless environment within a wireless network. It is not an ideal solution for servers, though, as there is an increased latency in transmitting data, and many Wi-Fi networks suffer from noise generated by other networks on the same frequency.
There are many types of wireless adapter available, and not all drivers are included with Raspbian. You might have to install a specific driver, but this is usually a simple process. When talking about wireless configuration, you have to target the chip that is used on the adapter and not the end vendor who is selling it.
Recommended wireless adapters
One of the most stable and affordable chipsets is Realtek RT8191, which works with the 802.11n standard. It is also compatible with the older 802.11b/g specifications, just in case your router does not support the newer 802.11n specification. However, it does not support 5 GHz frequencies or the 40 MHz dual band.
Shop around to find the best price, and it's best to not go for overpriced adapters that claim to be the only and the best ones for your Raspberry Pi.
Tip
You should be aware that, by default, wireless and wired networks do not work in combined mode. After configuring Wi-Fi you will need to remove your wired connection and reboot the Raspberry Pi. This will configure the wireless interface correctly. Connecting the wired connection usually drops the wireless connection after the Raspberry Pi has been booted.
Setting up from a desktop
Using the Wi-Fi configuration tool found in the menu bar while you're on the desktop is the most convenient way to connect to your wireless within seconds. It is recommended that you start with this if you have just installed Raspbian and plan on using Wi-Fi instead of an Ethernet connection. Your SSID, which is the readable name of your wireless access point, has to be broadcasted by your router for this to work. If you have it hidden, you should use the console method instead. Follow these steps to set up a desktop:
Left-click on the network icon found at the end of the menu bar on the right-hand side.
All unhidden networks will be displayed.
Left-click on the network you would like to connect to.
Enter your password and click on OK.
After a few seconds, the icon will stop flashing.
You can right-click on the network icon to adjust the network settings.
Setting up from a console
If you plan to run your Raspberry Pi in headless mode (where you do not have a monitor or the keyboard connected), you do not need to access the desktop by default, and you also cannot use the desktop network utility.
In the latest distributions of Raspbian, the SSH service is enabled by default, and you can SSH into your Raspberry Pi. If you have just joined your Wi-Fi network, you can tweak its settings. This is intended when you use Ethernet in order to set up the Wi-Fi. You can now log in via the SSH, and enter the following command:
sudo nano /etc/network/interfaces
This will open the included file editor, called nano, and present you with your current interface configuration. We are interested in the last few lines. If you do not see some of the lines, you can type them in manually:
auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
wpa-ssid "ssid"
wpa-psk "yourpassword"
To save the file, press CTRL + X, followed by Y, and finish by pressing Enter.
The allow-hotplug command will do as its name suggests: it allows you to plug wireless adapters in and out assigned to the wlan0 interface. Auto wlan0 tells Raspbian to configure the interface automatically based on the settings you provide.
We need to replace SSID and the password with the details of your router, keeping the text enclosed within quotation marks. This is the most basic configuration that can be used.
There is one problem with this configuration, though: if the wireless disconnects from your router, the interface will not be brought back up automatically. There are many scripts that try to solve this problem. The configuration requires that you know extra details about the wireless configuration. The next section offers a fully automatic way to do this. We will use wpa-supplicant, which is now installed by default with Raspbian. In /etc/network/interfaces, we change the last few lines to the following:
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the book in the Search box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on Code Download.
You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
In nano, a simple text editor, we need to type the following:
The configuration file contains some extra details about the connection. The way the supplicant works is that it will try to connect using the defined parameters for proto, pairwise, and group. You might have to adjust key_mgnt, but WPA-PSK is the most common configuration for modern wireless routers.
This method will also allow you to connect to your router if it does not broadcast its SSID.
Tip
It is a common misconception that disabling the SSID broadcast protects your network. There are tools that can still find SSID names using normal wireless cards without any effort, even though you have turned this off. The best security is to use the latest encryption protocol and a complex password.
Using wicd-curses
Using the wicd-curses package is a simpler way of setting up wireless and even wired interfaces in the console. The package allows you to edit advanced options such as a static IP, DNS, and hostnames. A daemon also runs in the background, which automatically reconnects the interface if the wireless signal drops out and is a more reliable way of maintaining a wireless network in poor signal areas. To use this package, you should comment out any settings in the wpa_supplicant interfaces file:
Tip
Installing this package will install many other dependency packages and can take up in excess of 8 MB of extra space. Some sources also claim that running this daemon uses more overhead, but this sounds like a fair price to pay for ease of use and reliability, especially with Raspberry Pi 2.
sudo apt-get install wicd-cursessudo wicd-curses
You will get a console screen with a list of available wireless access points. Use the cursor keys to select your access point, and press the right arrow key to edit its properties.
Tip
If you see the message that no wireless networks were detected, try to open the preferences window by pressing P, and type wlan0 in the wireless interface field.
Once you are in the configuring preferences screen for wireless networks, you would only need to enter your password in the key field. Save the settings by pressing F10.
If you set up the wireless connection using a wired connection, you will need to press C, which will connect to the access point. Keep an eye on the status LED on your Wi-Fi (if it has one). You should see it start to flicker, and after a few seconds, it should become solid, which means a connection has been established. At this point, it is highly likely that the SSH session will be dropped because the wired connection gets disabled. You should unplug the wired connection and reconnect using the wireless IP or hostname.
Tip
You can find the wireless IP by logging into your router via the web interface and looking at the DHCP list.