Troubleshoot your Wi-Fi with a Raspberry Pi
Hold on to your hats, as we join Sean Conway riding the RF waves to gain the knowledge needed to understand the inner workings of wireless.
All you wanted to do was stream that video, copy that file or try to view a few photos, but the Wi-Fi chooses that exact moment to play up. It’s a problem we’ve all encountered. How do you troubleshoot a wireless connection? With some basic radio frequency (RF) transmission theory knowledge, an understanding of the local environmental conditions and a few simple Raspberry Pi Linux applications, you’ll be better prepared to tackle Wi-Fi connection issues in the future and bedazzle fellow pub goers with your IEEE 802.11 knowledge, too!
This tutorial make the assumption that you have some basic experience with a Raspberry Pi. What does that mean? It’s always a struggle to determine what to put into a tutorial verses what to leave out. Here, the assumption permits the author to minimise details on some common Pi tasks, in favour of greater details on tasks the tutorial is designed to cover.
An example would be using a compatible Pi Wi-Fi attachment. If you’re not confident in your own Pi Wi-Fi attachment, we’ve assumed you’ve already looked into the matter, such as search around on Google search.
In this scenario, the Raspberry Pi will be the host trying to connect to a vendor’s modem (the access point, or AP) that supports wireless. Here’s a bill of materials for the setup used in this tutorial: OS: 2017-01-11-raspbian-jessie.img Hardware: Raspberry Pi Model B (Rev 2.0, 512MB): revision 000E Wi-Fi: Ralink Technology, Corp. RT5370 Wireless Adapter To install the raspberry Pi OS consult the www.raspberrypi.org site. The URL is the first choice when looking for all things Pi. The link reflects instructions that are relevant to the current OS (also see www.raspberrypi. org/documentation/installation/ installing-images).
With the OS installed there are a few raspi-config options that are employed to tailor the OS. You can expand the file system to take advantage of the larger memory cards being used, and personalise the device by giving it a hostname. Internationalisation options are tweaked to accommodate what side of the Atlantic the Pi resides. Turning on SSH and turning off a GUI console imposes the command line interface (CLI). A final touch is a password change. To keep the bad guys out, never leave password defaults unattended. The reference link www.raspberrypi.org/documentation/ configuration/raspi-config.md can provide the specifics we’ve just summarised here.
A quick reboot of the device to confirm all the options are in order. Then perform a command line update and upgrade as the final step before we establish the Pi setup for staging this WiFi tutorial.
To start the exercise, the Pi is connected to a wired network. This enables an SSH connection to the terminal command line interface. Install the Wi-Fi dongle. USB attachments can be installed or removed with the Pi power on. Examine the dmesg logs to gather information on the dongle. If you’re using a wireless attachment that’s not compatible with the Pi, it may not be detected. dmesg| grep -i usb [ 2.317400] usb 1-1.2: Product: 802.11 n WLAN
[ 2.317409] usb 1-1.2: Manufacturer: Ralink
[ 2.317418] usb 1-1.2: SerialNumber: 1.0
[ 10.538936] usb 1-1.2: reset high-speed USB device number 4 using dwc_ otg
[ 10.890759] usbcore: registered new interface driver rt2800usb
[ 21.067053] rt2800usb 1-1.2:1.0 wlan0: disabling HT as WMM/QoS is not supported by the AP
[ 21.067074] rt2800usb 1-1.2:1.0 wlan0: disabling VHT as WMM/QoS is not supported by the AP
The operating system appears to have found the Wi-Fi dongle. Another quick command line listing of USB devices obtains the details of the dongle: $ lsusb Bus 001 Device 004: ID 148f:5370 Ralink Technology, Corp. RT5370 Wireless Adapter ….... It stands to reason that, if the Pi has detected the Wi-Fi dongle, it should load the driver modules so the OS can communicate with the device. An rt2800usb was detected by examining the output of the dmesg command. The parsed output of the list modules command displays the rt2800, drivers and software libraries. $ lsmod Module Size Used by …... rt2800usb 18973 0
rt2800lib 82219 1 rt2800usb
rt2x00usb 12964 1 rt2800usb
rt2x00lib 49062 3 rt2800lib,rt2800usb,rt2x00usb ….... Using the instructions provided at www.raspberrypi.org/documentation/ configuration/wireless/wireless-cli.md to configure the Pi for wireless. It’s recommend that administrators employee the wpa_ passphrase command to build the configuration. Using this option prevents passwords from being transmitted in plain text.
Let’s leave the Pi world for a minute, to examine a free app, called Wifi Analyzer for Android that displays wireless networks. The app presents parabolic waveforms in different colours. Each colour represents the signal strength of the different network AP that’s been detected.
After that short Android interlude, let’s get back to the Pi. If you didn’t do a reboot after configuring the Wi-Fi, do it now. A dmesg check will confirm if the device is configured correctly with a wlan0 interface, and is ready for use: $ dmesg [ 21.227676] wlan0: authenticate with 00:15:05:f8:f7:c8 ……. [ 21.345086] wlan0: associated
‘wpa_cli’ is a command line front-end program for interacting with ‘wpa_ supplicant’. The command enables the user to check the current status, change the configuration, trigger events and request interactive user input. The output of the wpa_cli command provides numerous details related to a wireless device and the signal. Before employing the commands, it would be handy if you had some additional knowledge of radio frequency (RF). This is a good point to take a detour from all stuff Pi-related and explore some RF theory.
REAL LIFE RADIO FREQUENCY
A recent project required a wireless connection from a Raspberry Pi to a home router AP. The network could use one of two different residential gateways (RG) as APs. RGs are sometimes referred to as routers or modem. To be clear, it’s the device that’s responsible for connecting to the Internet Service Provider (ISP), which is providing access to the internet.
One RG was built with an external movable rubber duck antenna and the second RG had the antenna built into the device. Built-in antennae may be wrapped around the inside of the device, such as early generation Apple iPhones, or the antenna can built into the motherboard, just like current version of the Raspberry Pi-Zero.
RF signals are transmitted and received with an antenna. In the diagram (right), there’s a rendering of a vertical dipole antenna, with a signal source attached, radiating an RF signal energy out from the antenna in a torus pattern. Radiation pattern A in the drawing shows the torus or doughnut radiation pattern. The signal energy is equal in all directions. Devices that transmit and receive radio waves require antennas with a RF signal modulated (in other words, containing) with data.
Antennae are designed to radiate RF energy. How the antenna is physically constructed will affect the radiation pattern. Some antennae are engineered to transmit a very directional signal pattern. The radiation pattern identified with the letter B in the diagram focuses the RF energy out in one direction with very little radiation from the back. This enables greater range in a specific direction, although there’s a loss of signal in the opposite direction.
When using a hand-held transmitter like a walkie-talkie, best practice is to hold the device with the antenna vertical. In Hollywood action films, the protagonist holds the walkie-talkie radio transmitter horizontal for a better profile shot. With a horizontal antenna orientation, the signal will be at its weakest when the tip of the antenna is pointing in the direction the rescue aircraft is coming from. If the character is surrounded by tall, metal buildings, the signal strength radiated is reduced to the point he may not be able to transmit a strong enough signal for his rescue ride to receive.
Now that we have an understanding of antenna design, their resonant frequency, antenna orientation and
an awareness that obstructions can block signals and reduce signal strength, how is it applied?
There are two aspects that we need to understand using a wireless network: the ability to connect to the AP, and the data throughput delivered. The stronger the RF signal, the easier it is for the remote device and the AP to make a connection. More signal also means improved data throughput.
But wait — we also need to talk about noise. If you’ve ever listened to AM radio ( How old are we?! — Ed) during a thunder storm, you may have heard crackles and pops during the broadcast. That’s noise generated by the storm. Noise are errant signals generated by another sources that fall into the frequency range being used by a device. It affects the device’s ability to receive or read a signal.
What’s most important is the ratio of signal strength to noise strength. The noise strength needs to be lower than the signal strength for optimal reception. If the signal-to-noise ratio is low, then it becomes difficult for the receiver to pull the signal out of the noise.
RF signal strength is calculated using a formula that provides a value in dB (decibels). 0db is the middle of the scale. The more positive the number, the greater the signal strength. The more negative the number, the weaker the signal strength. The maths required to develop an understanding of the concept depends on slide rules or logarithmic tables. Additional theory on top of the content already covered would be a tad excessive for a Raspberry Pi tutorial. Much like cursive writing exercises at school, the benefit of gaining the knowledge versus just using a tool to arrive at the solution is always cause for debate.
On many wireless devices, a basic signal strength meter is provided using signal strength bars. The more bars, the greater the signal. The bar equivalent signal strength isn’t normally provided. It’s assumed that the greater number of bars on show, the better the reception. The diagram (below) provides a translation between bars of signal strength and dB.
-30dB is the maximum signal strength that a device can obtain. Close proximity to the access point with no obstruction will achieve this. Between -67dB and -70dB is needed for reliable packet delivery. -80dB is the minimum to make a connection. -90dB is so far down in the mud that it becomes difficult for a device to separate signal from noise.
The author’s spouse had some concerns when she came home from a weekend away and discovered the backyard Wi-Fi signal lab encroaching on the senior residence at the rear of our property. The red cones (see the photo far right) were markers in increments of 3 metres to measure distance.
An extension cord was needed to feed power to the Wi-Fi supported Raspberry Pi that was sealed in a weatherproof box. Two modems (RGs) with different antenna types were used for evaluation. It rained part way through the testing so the sealed black box that housed the Pi came in particularly handy.
BACK TO THE PI
This detour was taken to gain some RF knowledge before using the command line tool wpa_cli. Armed with this knowledge, let’s now examine some criteria that can be considered when looking at improving the response of the Wi-Fi network.
What wireless interfaces are available? $ wpa_ cli interface_list Selected interface ‘wlan0’ What’s the status of a given wireless interface? Results of all interfaces will be displayed. The Pi for the tutorial only had one interface (wlan0): $ wpa_ cli status Selected interface ‘wlan0’ bssid= 00:15:05:f8:f7:c8 freq=2432 ssid=network32k id= 0 mode=station pairwise_ cipher=CCMP group_ cipher=TKIP key_ mgmt= WPA2-PSK wpa_ state=COMPLETED ip_ address=192.168.2.203 address= 00:c1:41:39:00:83 uuid=944aa377-ebda-5ab2a510-68c81a5c4f16
How about supporting a ping command? $ wpa_ cli ping Selected interface ‘wlan0’ PONG What networks can the device connect with? $ wpa_ cli list_ networks Selected interface ‘wlan0’ network id / ssid / bssid / flags
0 network32k any [CURRENT]
[DISABLED] 1 any
and What available? other networks One command are in initiates range the displays scan while the results. the second command wpa_ Selected cli scan interface ‘wlan0’ OK And the second command... $ wpa_ cli scan_ results Selected interface ‘wlan0’ bssid / frequency / signal level / flags / ssid
00:15:05:f8:f7:c8 2432 -67 [WPA-PSK-TKIP][WPA2-PSK-CCMP] [ESS]network32k
10:9f:a9:62:a2:d92462 -59 [WPA-PSK-CCMP+TKIP][WPA2-PSKCCMP+TKIP][ESS] snigglefritz
00:1c:b3:af:cc:9a 2437 -63 [WPA-PSK-TKIP][WPA2PSK-CCMP+TKIP][ESS]network31k
10:9f:a9:61:77:f9 2452 -77 [WPA-PSK-CCMP+TKIP][WPA2-PSKCCMP+TKIP][ESS] MTSGT962
It looks like four networks have been discovered. The two scans prefixed with network are the APs used in this tutorial. If you’re familiar with ipconfig for wired networks, then iwconfig provides similar results for wireless networks. Like ifconfig, in the wireless environment, iwconfig can be also be used to manipulate basic wireless parameters: $ iwconfig wlan0 IEEE 802.11 ESSID:”network32k”
Mode:Managed Frequency:2.432 GHz Access Point: 00:15:05:F8:F7:C8
Bit Rate=24 Mb/s Tx-Power=20 dBm Retry short limit:7
RTS thr:off Fragment thr:off Power Management:off
Link Quality=39/70 Signal level=-71 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:6213 Invalid misc:38 Missed beacon:0
The command output shows what network the Pi is connected to and the signal strength. The next two commands provide similar details already displayed using the other commands. ‘iwgetid’ provides the network connection, with ‘iwlist’ provides scanning, list frequencies, bit-rates, encryption keys and so on. Wi-Fi quality and power readings are also available, by examining the contents of the wireless file in the / proc directory. $ cat /proc/net/wireless Inter-| sta-| Quality
| Discarded packets | Missed | WE
face | tus | link level noise | nwid crypt frag retry misc | beacon | 22
wlan0: 0000 43. -67. -256 0 0 0 6213 38 0
Considering we just installed an OS, did a few tweaks, added new Wi-Fi hardware and a configuration change, then detoured to take in some RF theory, you may be feeling a bit overwhelmed. All the commands provided are supported by man pages. Prefix the command in question with man separated by a space and you’ll soon gain some further insight into the command operation.
How about looking at one more command that consolidates the output from a number of commands which have been used so far. $ sudo wavemon
The output from the command is an active screen in real time displaying a number of the Pi’s Wi-Fi parameters. Remember that noise can have an effect on receiving a signal. The ‘wavemon’ output presents the link quality and signal level to demonstrate the concept. TAKE IT OUTSIDE Now that we have an understanding of some wireless commands, how about a practical application? Use the commands to answer this question: what differences does it make using an AP with an external antenna vs an AP with an internal antenna? To answer, connect the Pi to the wireless network that has an external antenna AP. Record signal levels and the greatest distance a connection can be maintained. Now complete the same exercise using an AP with an external antenna. In this scenario, the wavemon and /proc directory were used to show the readings as the Pi was moved. Revisit the diagram showing signal strength (far left). The data for the two APs with different antennae is included. The connection range was greater for the device with the external antenna.
The collection of photos (above) shows the professional engineering lab ( Ahem... — Ed) used to conduct the testing scenarios. The two different modems (the APs) were inside the author’s home above the electrical panel. On the outside, they’re located just below the attached gazebo. The red cones shown in one of the pictures were distance markers in increments of 3 metres from the AP. An few extension cords were needed to feed AC power to a Wi-Fi supported, Raspberry Pi sealed in a weather-proof box. If you look closely at one picture you should notice that it’s raining. ( All in a day’s work to bring you your issue of APC each month! — Ed)
There you have it: the command line workings of Wi-Fi on a Raspberry Pi. We hope the detour into RF theory didn’t overwhelm you. To fully appreciate the Wi-Fi commands, it’s important to have some RF background before exploring their use. The Wi-Fi commands are an excellent box of tools to open up in helping to try to resolve problems.
This Android wireless network analyser app identifies a range of networks.
Here’s a doughnutshaped signal pattern, without sprinkles or chocolate.
Low funds meant the author had to make the best of the available resources.