BPI-M2-Zero, Armbian_22.11.0, The method of connecting to WiFi, using wpa_cli, wpa_supplicant, can create automatic connection

20230605155836

The wpa_supplicant tool includes two programs, wpa_supplicant and wpa_cli, among which the wpa_supplicant program runs in the background as a server to serve the request of the wpa_cli client, thereby realizing the configuration and connection of WiFi.

use wpa_supplicant

To run wpa_cli, you must run wpa_supplicant first. As the server of wpa_cli, wpa_supplicant must be started before it can be accessed by wpa_cli. Use the command:

wpa_supplicant -D nl80211 -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf -B
//-D nl80211: Indicates that the driver type name is nl80211
//-i wlan0: Indicates that the network interface name is wlan0
// -c /etc/wpa_supplicant/wpa_supplicant.conf:
// The path of the configuration file wpa_supplicant.conf,
// The path in BPI-M2-Zero Armbian_22.11.0 is this.
//-B: means hang the daemon process wpa_supplicant to run in the background

use wpa_cli

Start the wpa_cli application

$ wpa_cli -i wlan0 scan // Search for nearby wifi networks
$ wpa_cli -i wlan0 scan_result // Print search wifi network results
$ wpa_cli -i wlan0 list_network // List existing network connection ids
$ wpa_cli -i wlan0 add_network // Add a network connection and return an id number

If the connection encryption method is [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS] (wpa encryption), wifi name is name, wifi password is: psk, network connection id is 0, add Multiples will be incremented.

$ wpa_cli -i wlan0 set_network 0 ssid '"name"'
$ wpa_cli -i wlan0 set_network 0 psk '"psk"'
$ wpa_cli -i wlan0 select_network 0
$ wpa_cli -i wlan0 enable_network 0

If the connection encryption method is [WEP][ESS] (wep encryption), the wifi name is name, and the wifi password is psk.

$ wpa_cli -i wlan0 set_network 0 ssid '"name"'
$ wpa_cli -i wlan0 set_network 0 key_mgmt NONE
$ wpa_cli -i wlan0 set_network 0 wep_key0 '"psk"'
$ wpa_cli -i wlan0 select_network 0
$ wpa_cli -i wlan0 enable_network 0

If the connection encryption method is [ESS] (no encryption), the wifi name is name.

$ wpa_cli -i wlan0 set_network 0 ssid '"name"'
$ wpa_cli -i wlan0 set_network 0 key_mgmt NONE
$ wpa_cli -i wlan0 select_network 0
$ wpa_cli -i wlan0 enable_network 0

assign ip/netmask/gateway/dns

$ udhcpc -i wlan0 -q

If command not found is encountered, install a necessary tool udhcpc for assigning ip/netmask/gateway/dns.

sudo apt update
sudo apt install udhcpc

If you encounter:

Warning: /etc/resolv.conf is not a symbolic link to /run/resolvconf/resolv.conf`

this way:

// Compare the two files
$ cat /run/resolvconf/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "systemd-resolve --status" to see details about the actual nameservers.

nameserver 192.168.223.150
nameserver 127.0.0.53

$ cat /etc/resolv.conf
// /etc/resolv.conf probably doesn't have the contents of /run/resolvconf/resolv.conf, delete it and ln again
$ sudo rm /etc/resolv.conf
$ sudo ln -s /run/resolvconf/resolv.conf /etc/resolv.conf

$ udhcpc -i wlan0 -q
udhcpc: started, v1.30.1
udhcpc: sending discover
udhcpc: sending select for 192.168.223.189
udhcpc: lease of 192.168.223.189 obtained, lease time 3599

After the execution is complete, you can connect to the network.

// verify
$ifconfig
$ ping baidu.com
$ ping google.com
// save the connection
$ wpa_cli -i wlan0 save_config
// Disconnect
$ wpa_cli -i wlan0 disable_network 0
// connect to an existing connection
$ wpa_cli -i wlan0 list_network // list all saved connections
$ wpa_cli -i wlan0 select_network 0 // connect to the first saved connection
$ wpa_cli -i wlan0 enable_network 0 // Enable the first saved connection
// disconnect wifi
$ ifconfig wlan0 down
$ killall udhcpc
$ killall wpa_supplicant

Edit How to save WiFi

ip link set wlan0 up
iw dev wlan0 scan | grep SSID

vim /etc/wpa_supplicant/wpa_supplicant.conf
network={
ssid="ssid"
psk="password"
priority=1
}

wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf -B
dhclient wlan0

After editing once, it can be automatically connected after power failure or reset.

1 Like