Enabling Wifi on your Raspberry Pi

After having two Raspberry Pis after a year or two now, it gets annoying to try connecting both of them to ethernet ports on my router. You're also then limited to only doing cool things within range of your ethernet cable from the router then. So, I bought got two USB WiFi dongles from Adafruit at $11.95 each.

Now, I have never attempted to connect to a WiFi network via the command line and I found that it definitely is not the best experience, especially when trying to setup WPA or WPA2 networks. Why isn't there an easy command line tool for listing available networks, inputting a password if they're protected, and then automatically writing the necessary config files? It's overall quite silly.

I'm going to show you what I did to get it working as well as the other helpful links I found but thought could better.

First things first

After SSHing into your Rbpi make sure that you can see the wlan0 section is in the output of ifconfig

ifconfig

Mine looks like this:

If you see wlan0 there you should be set, otherwise you probably need to install the driver for the WiFi dongle.

What worked for me

What I finally came up with was using the Python library wifi to scan and create my config file. This is definitely the most painless way to setup your WiFi and it will automatically add your configuration to /etc/network/interfaces unlike the manual edit methods below.

First you need to install wifi - https://pypi.python.org/pypi/wifi/0.3.4

This can be installed by using the ever awesome pip Python package manager.

sudo pip install wifi

Upon a successful install you can now type the following. (If you have multiple WiFi dongles added for whatever reason you will need to specify which interface you would like to setup, check wifi --help)

sudo wifi 

You will now see a list of WiFi networks that are viewable by the little guy. If you don't see your WiFi network check the positioning of your Pi and make sure that it's within a reasonable range. The reception on a dongle is no where near as a good as your phone or laptop.

In order to add your network:

sudo wifi add *network-name*

This will automatically add the proper configuration to /etc/network/interfaces. But we've only just added the configuration to the file, we have to tell the Pi to actually connect.

Just type:

sudo wifi autoconnect

It will tell you which network it is connecting to (the one you probably just saved).

You can now check ifconfig and see if the wlan0 now should have an IP address! Congratulations, you are now ethernet free!

The only downside is that if for whatever reason your Raspberry Pi loses connect because it restarts or the router restarts, it will not automatically reconnect. Right now the only way to do this is to manually edit /etc/network/interfaces which is what I was trying to avoid in this tutorial. However, I've created an issue on wifi's Github repo to try and improve the interface and @rockymeza has already responded so we'll see where it goes.

sudo nano /etc/network/interfaces

And remove the -ssid-name from the line that might look like this.

iface wlan0-'ssid-name' inet dhcp

So it would now look like this:

iface wlan0 inet dhcp

After a reboot, your Pi should connect automatically.

Other methods

The ultra manual method (adafruit.com)

This method involves going and editing /etc/network/interfaces in order to get it to work.

/etc/network/interfaces

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
auto wlan0


iface wlan0 inet dhcp
        wpa-ssid "ssid"
        wpa-psk "password"

However, getting the wifi to work with more a complicated security setup requires more configuration.

Wi-fi on Raspberry Pi, a simple guide (wicd-curses)

Upon first glance this was exactly the thing I was looking for but after successfully getting wicd-curses installed it would load fine but would immediately freeze when trying to perform any action.

I read another tutorial which mentioned that this may be because wicd restarts the networking service and if you are SSH'd into the machine this causes your session to drop. Having direct access to your Raspberry Pi would avoid this issue.

The tl;dr version of getting it going is:

sudo apt-get update
sudo apt-get install wicd-curses
...
sudo wicd-curses

If you get this working let me know! @dalanmiller