Tuesday, March 19, 2024

Raspberry Pi – Connect to multiple wireless networks (WLAN) automatically

Abstract: You wish to configure your Raspberry Pi (Wheezy or Jessie) so that it automatically connects to different wireless networks.

If you wish to take your Raspberry Pi to different houses and wish that it connects automatically to the wlan there, you need to configure multiple different WLAN configurations. The Raspberry Pi can then scan the environment and use the WLAN which is fits your configuration. To implement that you need to follow the steps below:

1.) At first you need to connect a WLAN USB stick (if the Raspberry Pi you are using isn´t supporting WLAN). I would prefer to get a EDIMAX EW-7811UN. Once installed, connect a LAN wire to the device and power up your pi.

2.) Login into your raspberry (Default User: pi; Default Password: raspberry) via SSH (see here how to enable SSH) and the IP address you got (if that isn´t possible you can connect the device to a screen and use a USB keyboard)

3.) Check if the USB stick is recognized. This can be done via multiple ways (as outlined below):

3a.) Via the full DMESG output

dmesg |more

It should show something similar like below:

usb 1-1.5: new high-speed USB device number 4 using dwc_otg
usb 1-1.5: New USB device found, idVendor=7392, idProduct=7811
usb 1-1.5: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 1-1.5: Product: 802.11n WLAN Adapter
usb 1-1.5: Manufacturer: Realtek

3b.) Via the DMESG output (using only a subset)

dmesg | grep WLAN

It should show something similar like below:

[    2.974987] smsc95xx 1-1.1:1.0 eth0: register 'smsc95xx' at usb-20980000.usb-1.1, smsc95xx USB 2.0 Ethernet, b8:27:eb:3e:88:88

3c.) Via the lsusb output with

lsusb

It should show something similar like below:

Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp. LAN9500 Ethernet 10/100 Adapter / SMSC9512/9514 Hub

If the USB device is successfully detected you can go to the next step.

4.) Edit the network config via

sudo nano /etc/network/interfaces

and replace the whole content with:

auto lo
iface lo inet loopback

auto eth0
allow-hotplug eth0
iface eth0 inet manual
#iface eth0 inet dhcp

auto wlan0
allow-hotplug wlan0
#iface wlan0 inet dhcp
iface wlan0 inet manual
pre-up wpa_supplicant -Dwext -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf -B

5.)  Now edit the wpa_supplicant config via:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

and replace the content in the config with:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

# Country code
# The ISO/IEC alpha2 country code for the country in which this device is
# currently operating.
#country=US
country=DE

# IEEE 802.1X/EAPOL version # wpa_supplicant is implemented based on IEEE Std 802.1X-2004 which defines # EAPOL version 2. However, there are many APs that do not handle the new # version number correctly (they seem to drop the frames completely). In order # to make wpa_supplicant interoperate with these APs, the version number is set # to 1 by default. 0 might be used if the driver should handle this feature. # Note: When using MACsec, eapol_version shall be set to 3, which is # defined in IEEE Std 802.1X-2010. eapol_version=1


# ---------------------------------
# network block (in preference order, the first match is used)

network={
# Network 1

# The SSID from the network 1
# (either as an ASCII string with double quotation or as hex string; network name)
ssid="YourFirstWLAN"

# Enable/Disable the scan for SSIDs
# (this can be used to find APs that do not accept broadcast SSID or use multiple SSIDs;
# this will add latency to scanning, so enable this only when needed)
scan_ssid=1

# list of accepted protocols
# Protocol type can be: RSN (for WP2) and WPA (for WPA1)
# WPA = WPA/IEEE 802.11i/D3.0
# RSN = WPA2/IEEE 802.11i (also WPA2 can be used as an alias for RSN)
# Default:
# proto=WPA RSN
proto=RSN

# list of accepted authenticated key management protocols
# WPA-PSK = WPA pre-shared key (this requires 'psk' field)
# WPA-EAP = WPA using EAP authentication (this can use an external
# program, e.g., Xsupplicant, for IEEE 802.1X EAP Authentication
# IEEE8021X = IEEE 802.1X using EAP authentication and (optionally) dynamically
# generated WEP keys
# NONE = WPA is not used; plaintext or static WEP could be used
# If not set, this defaults to: WPA-PSK WPA-EAP
# Default:
# key_mgmt=WPA-PSK WPA-EAP
key_mgmt=WPA-PSK

# list of allowed IEEE 802.11 authentication algorithms
# should be set to OPEN for both WPA1/WPA2 (in less commonly environments SHARED or LEAP where needed)
auth_alg=OPEN

# list of accepted pairwise (unicast) ciphers for WPA
# CCMP = AES in Counter mode with CBC-MAC [RFC 3610, IEEE 802.11i/D7.0]
# TKIP = Temporal Key Integrity Protocol [IEEE 802.11i/D7.0]
# NONE = Use only Group Keys (deprecated, should not be included if APs support
# pairwise keys)
# Default:
# pairwise=CCMP TKIP
pairwise=CCMP

# WPA preshared key; 256-bit pre-shared key
# The key used in WPA-PSK mode can be entered either as 64 hex-digits, i.e.,
# 32 bytes or as an ASCII passphrase (in which case, the real PSK will be
# generated using the passphrase and SSID). ASCII passphrase must be between
# 8 and 63 characters (inclusive)
psk="YourFirstWlanKey"

}

# ---------------------------------

network={
# Network 2

# The SSID from the network 2
# (either as an ASCII string with double quotation or as hex string; network name)
ssid="YourSecondWLAN"

# Enable/Disable the scan for SSIDs
# (this can be used to find APs that do not accept broadcast SSID or use multiple SSIDs;
# this will add latency to scanning, so enable this only when needed)
scan_ssid=1

# list of accepted protocols
# Protocol type can be: RSN (for WP2) and WPA (for WPA1)
# WPA = WPA/IEEE 802.11i/D3.0
# RSN = WPA2/IEEE 802.11i (also WPA2 can be used as an alias for RSN)
# Default:
# proto=WPA RSN
proto=RSN

# list of accepted authenticated key management protocols
# WPA-PSK = WPA pre-shared key (this requires 'psk' field)
# WPA-EAP = WPA using EAP authentication (this can use an external
# program, e.g., Xsupplicant, for IEEE 802.1X EAP Authentication
# IEEE8021X = IEEE 802.1X using EAP authentication and (optionally) dynamically
# generated WEP keys
# NONE = WPA is not used; plaintext or static WEP could be used
# If not set, this defaults to: WPA-PSK WPA-EAP
# Default:
# key_mgmt=WPA-PSK WPA-EAP
key_mgmt=WPA-PSK

# list of allowed IEEE 802.11 authentication algorithms
# should be set to OPEN for both WPA1/WPA2 (in less commonly environments SHARED or LEAP where needed)
auth_alg=OPEN

# list of accepted pairwise (unicast) ciphers for WPA
# CCMP = AES in Counter mode with CBC-MAC [RFC 3610, IEEE 802.11i/D7.0]
# TKIP = Temporal Key Integrity Protocol [IEEE 802.11i/D7.0]
# NONE = Use only Group Keys (deprecated, should not be included if APs support
# pairwise keys)
# Default:
# pairwise=CCMP TKIP
pairwise=CCMP

# WPA preshared key; 256-bit pre-shared key
# The key used in WPA-PSK mode can be entered either as 64 hex-digits, i.e.,
# 32 bytes or as an ASCII passphrase (in which case, the real PSK will be
# generated using the passphrase and SSID). ASCII passphrase must be between
# 8 and 63 characters (inclusive)
psk="YourSecondWlanKey"

}

6.) The next step is optional and would disable the power saving functions (for the EDIMAX EW-7811UN USB WLAN Stick). If you do not disable them it might be that the WLAN connection gets disconnected. To disable the feature create a new config:

sudo nano /etc/modprobe.d/8192cu.conf

with the following content:

options 8192cu rtw_power_mgnt=0 rtw_enusbss=0

7.) Checking the connection could be done via two possible ways:

7a.)

sudo ifup wlan0

7b.)

or reboot the PI via (and disconnect it from the LAN wire):

sudo shutdown -r now

Once it is back online it should be connected to one from the configured WLANs.

 8.) You can check that later one via:

iwconfig

which will output something similar like:

wlan0     IEEE 802.11bgn  ESSID:"TestWLAN"
          Mode:Managed  Frequency:2.412 GHz  Access Point: 34:81:C4:AF:AF:AF
          Bit Rate=72.2 Mb/s   Tx-Power=31 dBm
          Retry short limit:7   RTS thr:off   Fragment thr:off
          Power Management:on
          Link Quality=47/70  Signal level=-63 dBm
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:15  Invalid misc:0   Missed beacon:0

lo        no wireless extensions.

 

Further Troubleshooting:

A.) Check the Syslog for any USB device issues

sudo nano /var/log/syslog

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay Connected

35FollowersFollow
- Advertisement -

Latest Articles