Connecting Raspberry Pi to WiFi

Raspberry Pi doesn’t come with WiFi adapter built-in. It only have one fast ethernet (RJ45) port to connect it to the network. In order to make your Raspberry Pi become wireless, there are many small-sized WiFi USB dongle which is work out of the box with Raspberry Pi. One of them is TP-LINK TL-WN823N WiFi USB dongle.

Plug in the dongle to the Raspberry Pi USB port, then it should be directly recognized by Raspberry Pi as wlan0 interface. If it doesn’t, try rebooting your Raspberry Pi. Check it by using ifconfig command, and you should see something like below:

wlan0     Link encap:Ethernet  HWaddr 64:70:02:08:6b:64
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:289 errors:0 dropped:333 overruns:0 frame:0
          TX packets:38 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:49192 (48.0 KiB)  TX bytes:5594 (5.4 KiB)

Notice that the wlan0 interface doesn’t have any IP address assigned to it.

Configuring WiFi IP Address

To configure the IP address of your Raspberry wlan0 interface, edit the following file using nano / gedit / vi or any other text editor you like. Assuming that your Raspberry Pi will be getting it’s IP address using DHCP, put the following line to your /etc/network/interfaces file

allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
wireless-essid Nano-Hotspot
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

In this example my Hotspot SSID is Nano-Hotspot which is protected by a WPA-PSK key. On the last line, I mentioned a wpa_supplicant.conf file which I use to store information needed to connect to the hotspot, such as the key itself.

If your hotspot configuration is open, which is not requiring to put any password or key, then the following configuration on the /etc/network/interfaces is sufficient enough.

allow-hotplug wlan0
iface wlan0 inet dhcp
wireless-essid Nano-Hostpot

In case that your hotspot requires access key, then you need to know the hotspot password to generate the PSK key in order to be able to connect to.

Generating Pre-Shared-Key Configuration

Open up Raspberry Pi terminal and execute the following example command:

pi@raspi:~# wpa_passphrase Nano-Hotspot secretpassword
network={
 ssid="Nano-Hotspot"
 #psk="secretpassword"
 psk=225df5bb1a27c09cbef5ed023079bc7e7845d64037a98cdb1d8a59f1e0b475a3
}
pi@raspi:~#

Where Nano-Hotspot is the SSID name and secretpassword is the password itself in this example. Copy the above command output to theĀ /etc/wpa_supplicant/wpa_supplicant.conf file. Save and reboot your Raspberry Pi. The next time you boot your Raspberry Pi, it should be automatically connected to the defined hotspot.

How do I know whether my Raspberry Pi is connected or not?

Simple, if your WiFi dongle internal LED is lit or blinking then it is working. Then check it’s configuration using ifconfig command. In my case it was assigned an IP address of 192.168.0.12 like the following:

wlan0     Link encap:Ethernet  HWaddr 64:70:02:08:6b:64
          inet addr:192.168.0.12  Bcast:255.255.255.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:455 errors:0 dropped:504 overruns:0 frame:0
          TX packets:48 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:76251 (74.4 KiB)  TX bytes:7794 (7.6 KiB)