Raspberry Pi - Part 07 - Activating WiFi
Raspberry Pi - Part 07 - Activating WiFi
-
Activating WiFi
Version: 2023-02-16
Starting from Raspberry Pi 3, WiFi hardware is on-board and a WiFi dongle is no longer
needed. Should you however prefer a Wi-Fi dongle, see later in this document
Important:
Your WiFi router or Access Point should be setup for WPA2 only and not WPA/WPA2. The last
setting might not garantuee a probleem-free connectivity.
This tutorial works best if your router is broadcasting the SSID. Make sure you have
"Broadcast SSID" set up on your router.
Basic setup
On the Raspberry Pi 3B+ and Raspberry Pi 4B, you will also need to set the country code, so
that the 5GHz networking can choose the correct frequency bands.
But also, with the latest Buster Raspberry Pi OS release, you must ensure that the
wpa_supplicant.conf file contains the following information at the top
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
You can edit the wpa_supplicant.conf file and add the following. (Note: you need to replace
'BE' with the 2 letter ISO code of your country. See Wikipedia for a list of 2 letter ISO 3166-1
country codes.)
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=BE
sudo reboot
Band 1:
* 2412 MHz [1]
Maximum TX power: 20.0 dBm
Channel widths: 20MHz HT40+
* 2417 MHz [2]
Maximum TX power: 20.0 dBm
Channel widths: 20MHz HT40+
...
Band 2:
* 5170 MHz [34] (disabled)
* 5180 MHz [36]
Maximum TX power: 20.0 dBm
Channel widths: 20MHz HT40+ VHT80
* 5190 MHz [38] (disabled)
...
This will list all available wireless networks, along with other useful information. Look out for:
network={
ssid="testing"
psk="testingPassword"
}
The password can be configured either as the ASCII representation, in quotes as per the
example above.
wpa_passphrase "testing"
Then you will be asked for the password of the wireless network (in this case
testingPassword). The output is as follows
network={
ssid="testing"
#psk="testingPassword"
psk=131e1e221f6e06e3911a2d11ff2fac9182665c004de85300f9cac208a6a80531
}
Note that the plain text version of the code is present, but commented out. You should delete
this line from the final wpa_supplicant file for extra security.
The wpa_passphrase tool requires a password with between 8 and 63 characters. To use a
more complex password, you can extract the content of a text file and use it as input for
wpa_passphrase. Store the password in a text file and input it to wpa_passphrase by calling
wpa_passphrase "testing" < file_where_password_is_stored. For extra security, you
should delete the file_where_password_is_stored afterwards, so there is no plain text copy
of the original password on the system.
To use the wpa_passphrase–encrypted PSK, you can either copy and paste the encrypted PSK
into the wpa_supplicant.conf file, or redirect the tool's output to the configuration file in one
of two ways:
sudo su
then call
• Or use
and enter the testing password when asked. The redirection to /dev/null prevents tee
from also outputting to the screen (standard output).
If you want to use one of these two options, make sure you use >>, or use -a with tee, either
will append text to an existing file. Using a single chevron >, or omitting -a when using tee,
will erase all contents and then append the output to the specified file.
iwconfig
and
ifconfig wlan0
If the inet addr field has an address beside it, the Raspberry Pi has connected to the
network. If not, check that your password and ESSID are correct.
On the Raspberry Pi 3B+ and Raspberry Pi 4B, you will also need to set the country code, so
that the 5GHz networking can choose the correct frequency bands.
You can edit the wpa_supplicant.conf file and add the following. (Note: you need to replace
'BE' with the 2 letter ISO code of your country. See Wikipedia for a list of 2 letter ISO 3166-1
country codes.)
country=BE
Note that with the latest Buster Raspberry Pi OS release, you must ensure that the
wpa_supplicant.conf file contains the following information at the top
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=BE
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=BE
network={
ssid="testing"
psk="testingPassword"
}
or
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=BE
network={
ssid="testing"
psk=131e1e221f6e06e3911a2d11ff2fac9182665c004de85300f9cac208a6a80531
}
network={
ssid="testing"
key_mgmt=NONE
}
Hidden networks
If you are using a hidden network, an extra option in the wpa_supplicant file, scan_ssid,
may help connection.
network={
ssid="yourHiddenSSID"
scan_ssid=1
psk="Your_wireless_network_password"
}
You can verify whether it has successfully connected using iwconfig and ifconfig wlan0. If
the inet addr field has an address beside it, the Raspberry Pi has connected to the network.
If not, check your password and ESSID are correct.
network={
ssid="SchoolNetworkSSID"
psk="passwordSchool"
id_str="school"
}
network={
ssid="HomeNetworkSSID"
psk="passwordHome"
id_str="home"
}
If you have two networks in range, you can add the priority option to choose between them.
The network in range, with the highest priority, will be the one that is connected.
network={
ssid="HomeOneSSID"
psk="passwordOne"
priority=1
id_str="homeOne"
}
network={
ssid="HomeTwoSSID"
psk="passwordTwo"
priority=2
id_str="homeTwo"
}
network={
...
bssid=AC:84:C6:27:EE:2A
...
}
If you want to prevent it connects to a specific AP, find out the MAC address of that AP and
add it to the config. Eg: AP with MAC-address 8E:FC:A6:10:02:58
network={
...
bssid_blacklist=8E:FC:A6:10:02:58
...
}
network={
...
bssid=AC:84:C6:27:EE:2A
bssid_blacklist=8E:FC:A6:10:02:58
...
}
freq: 5180
beacon interval: 100 TUs
capability: ESS Privacy SpectrumMgmt ShortSlotTime (0x0511)
signal: -59.00 dBm
last seen: 0 ms ago
SSID: Engrie
Now you can specify the wpa_supplicant entry freq_list to only go for that specific one
freq_list=5180
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=BE
freq_list=5180
network={
ssid="testing"
psk="testingPassword"
}
• you can connect to only 2.4 Ghz band by specifying (for Belgium)
freq_list=2412 2417 2422 2427 2432 2437 2442 2447 2452 2457 2462 2467 2472
freq_list=5180 5200 5220 5240 5260 5280 5300 5320 5500 5520 5540 5560 5580 5600 5620 5640 5660 5680 5700
Connecting to a channel
If you want connect to a channel, you can first list all channels seen by your Pi
Next, you can use the band/frequency method to connect to a specific channel
interface eth0
static ip_address = 192.168.1.100 / 24
static routers = 192.168.1.1
static domain_name_servers = 192.168.1.1
interface wlan0
static ip_address = 192.168.1.101 / 24
static routers = 192.168.1.1
static domain_name_servers = 192.168.1.1
Let’s take a look at all this, here interface eth0 corresponds to a connection of wired type
and interface wlan0 to a Wi-fi connection. So you have to choose the one that corresponds
to your setup.
static ip_address is used to assign the ip that your Raspberry Pi will have. Generally the ip
is of type 192.168.1.x , replace the x with the value of your choice, be careful not to conflict
with other devices .
For static router (also known as gateway) and static domain_name_servers (also known
as DNS servers), this is the ip of your router, which is 192.168.1.1 .
Save the file. The SD card is now ready to be inserted into the Raspberry Pi or reboot your Pi.
iwconfig wlan0
but once you reboot, power management will be on again. To make this off at every boot, add
the command in rc.local like this
nano /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
ifconfig wlan0 up
iwconfig wlan0 mode ad-hoc
iwconfig wlan0 essid "Pi"
ifconfig wlan0 192.168.1.1 netmask 255.255.255.0
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
ifconfig wlan0 up
iwconfig wlan0 mode ad-hoc
iwconfig wlan0 essid "Pi"
ifconfig wlan0 192.168.1.1 netmask 255.255.255.0
exit 0
When you have finished press [Ctrl] + x. This will ask if you want to save the modified files.
Press 'Y' and then hit [Return] to save the file with the same name.
Reboot your Pi
sudo reboot
This will show you how to set up a wireless ad-hoc network, but only if it not already
connected. The wireless hotspot will also Dynamically assign IP Addresses to clients that
connect.
1. Install the packages hostapd (WiFi hotspot manager) and udhcpd (dhcp server that will
hand out the IP Addresses to clients that connect)
2. Configure udhcpd
# This is the range of IPs that the hotspot will give to client devices.
start 192.168.0.2
end 192.168.0.20
# The device udhcp listens on.
interface wlan0
remaining yes
# The DNS servers client devices will use.
opt dns 8.8.8.8 4.2.2.2
opt subnet 255.255.255.0
# The Pi's IP address on wlan0 which we will set up shortly.
opt router 192.168.0.1
# 1 hour DHCP lease time in seconds
opt lease 3600
When you have finished press [Ctrl] + x. This will ask if you want to save the modified
files. Press 'Y' and then hit [Return] to save the file with the same name.
The next step is to ‘enable’ the udhcpd server. To do this, edit the /etc/default/udhcpd
file and comment out the following line by prepending a hash symbol:
DHCPD_ENABLED="no"
Like this:
#DHCPD_ENABLED="no"
When you have finished press [Ctrl] + x. This will ask if you want to save the modified
files. Press 'Y' and then hit [Return] to save the file with the same name.
This will enable the udhcpd server. Later, we will prevent it from starting automatically,
just in case we do actually manage to connect to another wireless network.
The next step is to configure hostapd. Edit the following file (creating it if it doesn’t exist)
/etc/hostapd/hostapd.conf. It should contain the following text:
interface=wlan0
driver=nl80211
ssid=Raspi_wifi
hw_mode=g
channel=11
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=somepassword
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
When you have finished press [Ctrl] + x. This will ask if you want to save the modified
files. Press 'Y' and then hit [Return] to save the file with the same name.
The above configuration will create an ad-hoc wireless g network on channel 11. You
should change the channel to one that is not in use in the area where you will be using
your Pi. It will be secured with WPA-2, and password will be “somepassword”. You should
change this to a more secure password.
4. To check the connectivity on boot and performing the appropriate action, run the following
commands:
This will prevent udhcpd and hostapd from running on boot and preventing your Pi from
connecting to other wireless networks automatically.
Next, edit the file: /etc/rc.local. It should look something like this:
exit 0
if [ "$_IP" ]; then
The sleep 10 makes sure that your Pi has time to connect to any wireless networks it can.
The code starting after the “My IP Address is…” will output a message telling the console
whether or not we are setting up an ad-hoc wireless access point.
When you have finished press [Ctrl] + x. This will ask if you want to save the modified
files. Press 'Y' and then hit [Return] to save the file with the same name.
Reboot your Pi
sudo reboot
Use
crontab -e
and add:
Pre-checks:
sudo poweroff
2. Insert your WiFi dongle and power on the Pi. Wait another 30 seconds after it completed
powered on.
iwconfig
...
wlan0 IEEE 802.11bgn ESSID:"YourSSID”
Mode:Managed Frequency:2.422 GHz Access Point: 00:18:E7:F2:E3:36
Bit Rate=65 Mb/s Tx-Power=20 dBm
...
4. To scan for WiFi networks, use the command. It might be usefull to note down some
information about the WiFi network you will use like ESSID, IE, Group, Pairwise,
Authentication, …
...
wlan0 Scan completed :
Cell 01 - Address: 00:18:E7:F2:E3:36
ESSID:"YourSSID"
Protocol:IEEE 802.11bgn
Mode:Master
Frequency:2.427 GHz (Channel 4)
Encryption key:on
Bit Rates:144 Mb/s
Extra:wpa_ie=dd1a0050f20101000050f...
IE: WPA Version 1
Group Cipher : TKIP
Pairwise Ciphers (2) : TKIP CCMP
Authentication Suites (1) : PSK
IE: IEEE 802.11i/WPA2 Version 1
Group Cipher : TKIP
Pairwise Ciphers (2) : TKIP CCMP
Authentication Suites (1) : PSK
IE: Unknown: DD0E0050F204104A0001101044000102
Quality=100/100 Signal level=75/100
...
This scan shows I have excellent signal (Quality=100/100 Signal level=75/100) for
my network with SSID “YourSSID”. My AP uses the standard 802.11b/g/n and I am at
high bitrate (144 Mb/s) to transfer data. Encryption is active (on) and 2 versions are
available WPA (Version 1) with related info and WPA2 (Version 1)with related info.
lsusb
lsmod
Even after buying a powerful and reliable power supply, USB WiFi adaptors often fail to
perform. They'll work fine on boot, but as soon as the network traffic dies down, they become
unreliable.
The problem here is that many of these adaptors use a power-saving mode designed for
smartphones and tablets where the network traffic is typically predominantly outbound. If the
Raspberry Pi doesn't send any network traffic for a while, the wireless adaptors go into low
power mode and become difficult to contact from an external source. For example, you may
not be able to ping or establish an SSH connection from your desktop PC to your Raspberry Pi,
or an SSH session may become unreliable after being left idle for a few minutes.
These power-saving modes can be turned off. The actual power saving is pretty minimal,
especially if you actually want to have the Pi serving data pretty much constantly.
For the WiFi dongles driven by the module 8192cu, add the following two lines to the
configuration file and save the file:
When you have finished press [Ctrl] + X. This will ask if you want to save the modified files.
Press 'Y' and then hit [Return] to save the file with the same name.
auto lo
iface lo inet loopback
auto eth0
allow-hotplug eth0
iface eth0 inet manual
auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
post-up iw dev wlan0 set power_save off
auto wlan1
allow-hotplug wlan1
iface wlan1 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
When you have finished press [Ctrl] + X. This will ask if you want to save the modified files.
Press 'Y' and then hit [Return] to save the file with the same name.
Reboot your Pi
sudo reboot
sudo iwconfig
...
wlan0 IEEE 802.11bgn ESSID:"YourSSID"
Mode:Managed Frequency:2.422 GHz Access Point: 00:18:E7:F2:E3:36
Bit Rate=65 Mb/s Tx-Power=20 dBm
Retry short limit:7 RTS thr:off Fragment thr:off
Power Management:off
Link Quality=70/70 Signal level=-37 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:14 Missed beacon:0
...
auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-ssid “YourSSID”
wpa-psk “YourKEY”
auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-ssid “YourSSID”
wpa-psk “YourKEY”
...
auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-scan-ssid 1
wpa-ap-scan 1
wpa-key-mgmt WPA-PSK
wpa-proto RSN WPA
wpa-pairwise CCMP TKIP
wpa-group CCMP TKIP
wpa-ssid "<Your Wifi SSID>"
wpa-psk <Your PSK Value>
...
When you have finished press [Ctrl] + x. This will ask if you want to save the modified
files. Press 'Y' and then hit [Return] to save the file with the same name.
Wait a minute or 2 after reboot completed before checking your wireless connection
sudo iwconfig
...
wlan0 IEEE 802.11bgn ESSID:"YourSSID"
Mode:Managed Frequency:2.422 GHz Access Point: 00:18:E7:F2:E3:36
Bit Rate=65 Mb/s Tx-Power=20 dBm
...