WiFi Function
The GW102 gateway is equipped with a built-in WiFi module, providing WiFi 2.4G wireless functionality and enabling connection to other WiFi hotspots.
The physical picture of the WiFi antenna holder is as follows.:
Prepare
The WiFi module is connected to the system via USB signal. By using "lsusb", one can check whether the device has been recognized on the USB bus.
root@gw102d512e4:~# lsusb
Bus 002 Device 003: ID 0bda:f179 Realtek Semiconductor Corp.
Bus 001 Device 002: ID 1782:4d11 Spreadtrum Communications Inc.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 002: ID 1a40:0101 Terminus Technology Inc. Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Here, we can see that 0bda:f179 Realteck Semiconductor is a WiFi device, which indicates that the identification has been successful.
Check whether the WiFi driver has been successfully loaded and whether the wlan0 device exists.
root@gw102d512e4:~# dmesg| grep rtl8188
[ 1073.079427] usbcore: registered new interface driver rtl8188eu
root@i2x-6ub-gw:~# ifconfig wlan0
wlan0 Link encap:Ethernet HWaddr 00:13:EF:F5:35:6A
BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
The above two points confirm that the USB WiFi module has been recognized by the system and the driver is functioning properly.
The wpa_supplicant command is used to connect to a network.
Before connecting, it is necessary to configure the access information of WiFi first, including the WiFi hotspot name and password. The wpa_passphrase command can conveniently generate the required configuration file. Here, as an example, the WiFi name we need to connect to is i2SOM and the password is helloworld.
root@gw102d512e4:~# wpa_passphrase "i2SOM" > wifi.conf
helloworld
Then, by using the wpa_supplicant command, one can connect to a WiFi network.
root@gw102d512e4:~# wpa_supplicant -B -c wifi.conf -i wlan0
Successfully initialized wpa_supplicant
Do not execute the wpa_supplicant command repeatedly. Instead, you can use the ps command to check whether the wpa_supplicant command has already been running.
If available, the kill command can be used to terminate the corresponding process.
The gateway is set to default for the wlan0 device on the system. It is configured to obtain IP address via DHCP on systemd. The content of the configuration file is as follows:
root@gw102d512e4:~# cat /etc/systemd/network/wlan0.network
[Match]
Name=wlan0
[Network]
DHCP=yes
It can also be changed to a static IP address. The modified content is as follows:
root@gw102d512e4:~# cat /etc/systemd/network/wlan0.network
[Match]
Name=wlan0
[Network]
Address=192.168.8.100/24
If the wlan0 device is not configured on systemd, it is also possible to obtain the IP address manually by using udhcpc from the gateway device of the connected network.
root@gw102d512e4:~# udhcpc -i wlan0
udhcpc: started, v1.29.2
udhcpc: sending discover
udhcpc: sending discover
udhcpc: sending select for 192.168.2.122
udhcpc: lease of 192.168.2.122 obtained, lease time 86400
/etc/udhcpc.d/50default: Adding DNS 192.168.2.1
现在就可以使用ping命令来测试与WiFi网络内其他设备的连通性了。
如果需要访问互联网,可以设置DNS地址。
root@gw102d512e4:~# echo "nameserver 114.114.114.114" > /etc/resolv.conf
root@gw102d512e4:~# echo "nameserver 8.8.8.8" >> /etc/resolv.conf
root@gw102d512e4:~# ping baidu.com
PING baidu.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=52 time=21.2 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=52 time=21.1 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=52 time=21.2 ms
systemd uses the wpa_supplicant service
The wpa_supplicant service uses the file located at /etc/wpa_supplicant/wpa_supplicant-wlan0.conf as the default configuration file. If such file does not exist, please manually create the directory and the corresponding file. The content of the file is as follows
root@gw102d512e4:~# cat /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=0
update_config=1
network={
}
使用systemd启动wpa_supplicant服务
systemctl enable wpa_supplicant@wlan0
/etc/systemd/system/multi-user.target.wants/wpa_supplicant@wlan0.service
systemctl start wpa_supplicant@wlan0
检查服务是否启动成功
使用systemctl status命令可以查看服务是否有异常退出的信息,没有说明启动正常
systemctl restart systemd-networkd
使用ps命令来查看wpa_supplicant进程是否运行
root@gw102d512e4:~# ps | grep wpa_supplicant
932 root 8992 S /usr/sbin/wpa_supplicant -c/etc/wpa_supplicant/wpa_supplicant-wlan0.conf -iwlan0
1085 root 2388 S grep wpa_supplicant
以上说明wpa_supplicant服务已经运行了。
wpa_supplicant服务在后台运行,可以通过wpa_cli命令来对网络配置。
搜索和查看当前环境下可连接的WiFi网络
wpa_cli -i wlan0 scan
wpa_cli -i wlan0 scan_result
scan是搜索,scan_result是显示搜索的结果。
查看当前网络连接状态
wpa_cli -i wlan0 status
查看已经配置的WiFi设置,可以有多个配置,每个配置由network id来区分。
获取已经配置的wifi设置
wpa_cli -i wlan0 list_networks
新建WiFi配置,会返回network id,比如0,1,2
wpa_cli -i wlan0 add_network
设置WiFi的SSID名称
wpa_cli -i wlan0 set_network 0 ssid '"TP-LINK"'
设置WiFi的BSSID名称,如果WiFi名称中有空格或特殊字符,建议使用BSSID来连接网络。
wpa_cli -i wlan0 bssid 0 00:11:22:33:44:55
如果要连接加密方式是[WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP]ESS
wpa_cli -i wlan0 set_network 0 psk '"psk"'
如果要连接加密方式是[WEP][ESS] (wep加密)
wpa_cli -i wlan0 set_network 0 key_mgmt NONE
wpa_cli -i wlan0 set_network 0 wep_key0 '"psk"'
如果要连接加密方式是ESS
wpa_cli -i wlan0 set_network 0 key_mgmt NONE
wpa_cli -i wlan0 enable_network 0
新配置的wifi默认是禁用的,启用,启用后如果没有连接wifi,新增wifi可见就会自动连接的
wpa_cli -i wlan0 enable_network 0
保存wifi配置,最好在启用wifi后再保存,否则保存的wifi是默认禁用的
wpa_cli -i wlan0 save_config
如果有多个WiFi配置,可以通过如下命令选择WiFi配置
wpa_cli -i wlan0 select_network 0
最后编辑:SteveChen 更新时间:2025-04-23 00:46