Typically, Raspberry Pi is connected to the local network via Ethernet. To get away from the wired network, Raspberry Pi should be connected to the existing wireless network. To do this, a WiFi adapter must be plugged into a USB port and then configured.

The following instructions refer to the setup of a WiFi adapter under the distribution “Raspbian”. Other distributions may require customization.
For example, installing a driver for the wireless adapter. One should not assume that each distribution always brings the necessary drivers.
Overview
Basically, there are several ways to set up a WiFi adapter for Raspberry Pi. Which one one uses depends on the requirements and personal preference.
- WiFi configuration in the file “/etc/network/interfaces”
- WiFi configuration with “wpa_supplicant”
- WiFi configuration with “systemd”
Many tutorials configure the WiFi via “wpa_supplicant”. This can be done if you want to run Raspberry Pi mobile and in different networks and rely on the fact that the roaming works (variant 2). If Raspberry Pi only has to work in a single WiFi, which is the rule, then a simple solution is enough (variant 1).
Making the WiFi configuration with systemd can make sense in the future (variant 3). Assuming that you do the complete network configuration via systemd.
Note: Name of the network interfaces
Since Raspbian Stretch, the Ethernet and WiFi network interfaces have different names. So no longer “eth0” and “wlan0”, but “enx …” and “wlx …”. This concerns USB-connected network adapters whose names differ from the designations mentioned here. This means that one must first determine the individual name or change the naming to the old method.
Solution (variant 1): WiFi configuration in the file “/etc/network/interfaces”
To set up a connection to a WiFi you have to edit the network configuration. To do this, open the following file:
sudo nano /etc/network/interfaces
Here you enter the following lines:
WLAN Wi
-fi allow-hotplug wlan0
iface wlan0 inet manual
wpa -sid "WLAN-NAME"
wpa-psk "WLAN-PASSWORD"
You have to enter the WiFi name (SSID) and the WiFi password (Pre-Shared-Key, PSK). Both values should be in quotation marks (“”). It is not compulsory, but recommended if the name and password contain characters that are different in this file. For example, a space.
Then save and close: Ctrl + O, Return, Ctrl + X.
Then we restart the interface. Only then will the network settings be adopted and the connection to the WiFi established.
sudo ifdown wlan0
sudo ifup wlan0
If the WiFi configuration in the network settings is correct, then Raspberry Pi has received an IP address from the DHCP server. You can see that on the “DHCPACK”. The next line also displays the IPv4 address. If not, then the Wi-Fi name, Wi-Fi password, or both is wrong.
With “ifconfig” one can again output the network interfaces. If the interface “wlan0” has been assigned an IP address, then Raspberry Pi is connected to the WiFi.
Solution (variant 2): WiFi configuration with “wpa_supplicant”
This solution may be preferable. Because the software package “wpa_supplicant” detects radio networks and takes over the automatic connection setup.
There is a file for the configuration of the wireless networks, which is also used by the network managers of the desktop interfaces.
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
The following lines must be found or added here.
ctrl_interface = DIR =/var/run/wpa_supplicant GROUP = netdev
update_config = 1
country = EN
Followed by the individual WiFi networks:
network ={
ssid ="Wireless SSID"
psk ="wireless PASSWORD"
}
Then save and close: Ctrl + O, Return, Ctrl + X.
But it is not necessary to open and edit the file. You can also create the entries automatically.
sudo wpa_passphrase "WLAN-NAME" "WLAN-PASSWORD" >> /etc/wpa_supplicant/wpa_supplicant.conf
The following error message appears: “-bash: /etc/wpa_supplicant/wpa_supplicant.conf: No permission”.
What is the problem? With “sudo” the command is executed with root rights, but the configuration file can not be described in this way. You really have to be “root” for it to work.
sudo -i
wpa_passphrase "WLAN-NAME" "WLAN-PASSWORD" >> /etc/wpa_supplicant/wpa_supplicant.conf
exit
Let’s see what we have done:
sudo cat /etc/wpa_supplicant/wpa_supplicant.conf
This command has added a new wireless network with the correct syntax at the end of the configuration file. At the same time, it has converted the Wi-Fi password into a hash value, so that the Wi-Fi connection is faster. If you want, you can now delete the plaintext password from the file (optional).
Now you can still connect to the test WiFi.
sudo wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
Alternative:
sudo wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf &
The final “&” sends the process into the background.
If the file “/etc/network/interfaces” corresponds to the original, the connection to the WiFi should be set up automatically after a reboot.
Solution (variant 3): WiFi configuration with systemd
Note: Before choosing this solution for the WiFi configuration, the network configuration must be switched to systemd, without any network manager. Otherwise, this solution makes no sense.
First we create a WiFi configuration for “wpa_supplicant”. This variant requires the use of “root”, which we have to change first.
sudo -i
Then we enter the WiFi configuration into an existing configuration file with a command.
wpa_passphrase "WLAN-NAME" "WLAN-PASSWORD" >> /etc/wpa_supplicant/wpa_supplicant.conf
The placeholders for WiFi name and WiFi password must be replaced by their own values.
At this point it is not wrong to take a look into this file and check if the entry has been created.
cat /etc/wpa_supplicant/wpa_supplicant.conf
The file should look like this, for example:
ctrl_interface = DIR = /var/run/wpa_supplicant GROUP = netdev
update_config = 1
country = EN
network = {
ssid = "Wireless NAME"
# Psk = "wireless PASSWORD"
psk = wireless PASSWORT_CODIERT
}
It should be noted that the WiFi password has been written to the file in plain text. For security reasons, you should delete the line with the clear text password (optional).
Then we return to the regular user:
exit
Then we create a systemd unit. It should be noted that this solution only makes sense if you do without any network manager.
sudo nano /lib/systemd/system/[email protected]
Description = WPA supplicant daemon (wlan0)
Requires = sys-subsystem-net-devices-wlan0.device
BindsTo = sys-subsystem-net-devices-wlan0.device
After = sys-subsystem-net-devices-wlan0.device
Before = network.target
Wants = network.target
[Service]
Type = simple
RemainAfterExit = yes
ExecStart =/sbin/wpa_supplicant -qq -c /etc/wpa_supplicant/wpa_supplicant.conf -Dnl80211 -iwlan0
Restart =on-failure
[Install]
Alias=multi-user.target.wants/[email protected]
Save and close with Ctrl + O, Return, Ctrl + X.
Then we activate and start the new systemd unit.
sudo systemctl enable [email protected]
sudo systemctl start [email protected]
What happens right now depends on several factors. Assuming that the configuration is done correctly, then the connection to the WiFi should now be established. Depending on the wireless adapter we have a different reaction here. For example, a status LED on the WiFi adapter starts to flash and, if the connection is successfully established, changes to a steady light.
Regardless, you should check the wireless connection:
sudo systemctl status [email protected]
In the line “Loaded:” the back should be “enabled”. A log entry below should have “Started WPA supplicant daemon (wlan0).” correspond. Then the connection to the WiFi was successful.
Solution: Check WiFi connection
Check if the WiFi interface is present:
ip l
Check if there is a connection to a WiFi:
wpa_cli status
If a known access point is within range, information about the connection should be displayed here.
Solution: Check network configuration
Whether the network configuration has been carried out is to be checked as follows.
ip a
Here you can see if the interface “wlan0” got an IP address.
Troubleshooting
The first thing to do is to update the system and then install a tool that makes it easier to configure WiFi.
sudo apt-get update
sudo apt-get install iw
Then you insert the WiFi adapter to a free USB port. Then it’s about finding out if the Wi-Fi adapter was even recognized. You can view all USB devices.
lsusb
If this is something of “wireless” or “802.11”, then you have almost won. Now there should also be a network interface called “wlan0” or “wlan1”. You can view all network interfaces.
ip l
If there is a network interface called “wlan0” or “wlan1” in the list, then hardware detection and driver installation went very well. Now you can do the configuration of the WiFi client and the IP settings.
Afterwards we check, whether the WiFi, in which we want to register Raspberry Pi, is also in range.
iwlist wlan0 scan | grep -i ssid
The output shows all Wi-Fi networks that are in the environment. The one in which Raspberry Pi is to be checked in should also be in the list. Prerequisite is that Raspberry Pi is within its reach.
Alternative: set up WiFi with “wicd-curses”
Setting up a WiFi for Raspberry Pi is not always easy. Especially because you have to edit files. This is very error-prone and only succeeds if you keep a certain order pretty much exactly. Easier is wicd-curses, a graphical network manager that presents all the important information in a user interface that has to be operated with the keyboard.
Problem: Connection aborts in SSH connection via WiFi adapter
Normally, SSH connections are extremely stable. If no timeouts are set on the client or server side, the connection will theoretically remain infinitely.
But it can still lead to disconnections. For example, for connections via WiFi. Typically, two problems are responsible here. Either the power saving function of the WiFi adapter or it is sloppily developed or exhibits cheap components. Particularly cheap WiFi adapters, so-called Chinese shred, can cause problems here. Remedy only the exchange for a better wireless adapter.
Problem: No auto-reconnect
If you have set up the WiFi manually and the access point is turned off once, then breaks the wireless connection. Logical. However, when the access point goes back in, Raspberry Pi does not automatically connect to the WiFi.
Raspberry Pi: Set up automatic wireless reconnect
If you have set up a WiFi connection on Raspberry Pi manually and this connection is lost for some reason, then this connection is no longer automatically restored. This can happen if the WiFi access point is switched off once, or if Raspberry Pi or the access point is out of range of the radio signals.
When the access point goes back into service or Raspberry Pi is within range of the radio signals again, then Raspberry Pi does not automatically connect to the WiFi. If you want that, then you have to configure it. It is enough to copy a few files.
First we rename the file “ifupdown”:
sudo mv /etc/ifplugd/action.d/ifupdown/etc/ifplugd/action.d/ifupdown.old
Then we copy the file in which the command for the Wi-Fi Reconnect is in place of the file “ifupdown”.
sudo cp /etc/wpa_supplicant/ifupdown.sh/etc/ifplugd/action.d/ifupdown
After a restart, the wireless reconnect should be done automatically.
sudo reboot
Note
If the reconnect has been activated in this way, then the Ethernet port is no longer automatically operated if you remove the WiFi adapter and reconnect the LAN cable.
If you want to connect Raspberry Pi again via Ethernet to the network, then you have to undo the reconnect.
To do this, change to the following directory, delete a file and copy the original back:
cd /etc/ifplugd/action.d/
sudo rm ifupdown
sudo cp ifupdown.old ifupdown
The change will only become active after a restart.
Extension: Switch off energy-saving mode of the WiFi adapter
By default, some wireless adapters enable “Power Saving”. This is a “sleep mode”, or rather an energy-saving feature. This means that the adapter will turn off the network connection when inactive. This is not a problem when using Raspberry Pi as a client. Then the WLAN connection is simply reactivated. But if you want to remotely access Raspberry Pi via SSH for example, it may not be accessible.
- Switch off the WiFi adapter power saving mode
Leave a Reply