Follow by Email

Blog Archive

Search This Blog

Raspberry Pi setup WLAN adapter

Raspberry Pi setup WLAN adapter under the distribution "Raspbian"
Share it:
Raspberry Pi setup WLAN adapter
Raspberry Pi setup WLAN adapter

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 WLAN adapter must be plugged into a USB port and then configured.
The following instructions refer to the setup of a WLAN 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.

Basically, there are several ways to set up a WLAN adapter for Raspberry Pi. Which one one uses depends on the requirements and personal preference.
  • WLAN configuration in the file "/ etc / network / interfaces"
  • WLAN configuration with "wpa_supplicant"
  • WLAN configuration with "systemd"
Many tutorials configure the WLAN 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 WLAN, which is the rule, then a simple solution is enough (variant 1).
Making the WLAN 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 WLAN 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): WLAN configuration in the file "/ etc / network / interfaces"
To set up a connection to a WLAN 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:
-fi allow-hotplug wlan0
iface wlan0 inet manual
wpa -sid "WLAN-NAME"

You have to enter the WLAN name (SSID) and the WLAN 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 WLAN established.
sudo ifdown wlan0
sudo ifup wlan0

If the WLAN 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 WLAN.

Solution (variant 2): WLAN 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 WLAN 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

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 WLAN.
sudo wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf

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 WLAN should be set up automatically after a reboot.

Solution (variant 3): WLAN configuration with systemd
Note: Before choosing this solution for the WLAN configuration, the network configuration must be switched to systemd, without any network manager. Otherwise, this solution makes no sense.
First we create a WLAN configuration for "wpa_supplicant". This variant requires the use of "root", which we have to change first.
sudo -i

Then we enter the WLAN configuration into an existing configuration file with a command.
wpa_passphrase "WLAN-NAME" "WLAN-PASSWORD" >> /etc/wpa_supplicant/wpa_supplicant.conf

The placeholders for WLAN name and WLAN 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 WLAN 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:

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/wpa_supplicant@wlan0.service
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 =
Wants =

Type = simple
RemainAfterExit = yes
ExecStart = / sbin / wpa_supplicant -qq -c / etc / wpa_supplicant / wpa_supplicant.conf -Dnl80211 -iwlan0
Restart = on-failure


Save and close with Ctrl + O, Return, Ctrl + X.

Then we activate and start the new systemd unit.
sudo systemctl enable wpa_supplicant@wlan0.service

sudo systemctl start wpa_supplicant@wlan0.service

What happens right now depends on several factors. Assuming that the configuration is done correctly, then the connection to the WLAN should now be established. Depending on the wireless adapter we have a different reaction here. For example, a status LED on the WLAN 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 wpa_supplicant@wlan0.service

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 WLAN was successful.
Solution: Check WLAN connection
Check if the WLAN interface is present:
ip l

Check if there is a connection to a WLAN:
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.
The first thing to do is to update the system and then install a tool that makes it easier to configure WLANs.
sudo apt-get update
sudo apt-get install iw

Then you insert the WLAN 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.

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 WLAN client and the IP settings.

Afterwards we check, whether the WLAN, 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 WLAN with "wicd-curses"
Setting up a WLAN 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 WLAN 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 WLAN. Typically, two problems are responsible here. Either the power saving function of the WLAN adapter or it is sloppily developed or exhibits cheap components. Particularly cheap WLAN 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 WLAN 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 WLAN.

Raspberry Pi: Set up automatic wireless reconnect
If you have set up a WLAN 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 WLAN 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 WLAN. 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/

After a restart, the wireless reconnect should be done automatically.
sudo reboot

If the reconnect has been activated in this way, then the Ethernet port is no longer automatically operated if you remove the WLAN 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 WLAN 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.
Share it:

Raspberry Pi

Post A Comment: