跳到主要内容

WIFI & Bluetooth

Luckfox Lyra Pi 上搭载了AIC8800DC模块,支持Wi-Fi AX协议(WIFI6),支持 2.4GHz Wi-Fi 和蓝牙5.2/BLE。开发板 WiFi 使用需要外接天线。

1. Wi-Fi 测试

在无线通信中,Wi-Fi 设备通常可以运行在 AP(Access Point,接入点)模式STA(Station,站点)模式 这两种基本模式。AP 模式 让设备充当无线热点,允许其他设备连接,就像家庭中的 WiFi 路由器一样。而 STA 模式 则让设备作为 WiFi 客户端,连接到已有的无线网络,例如手机或笔记本电脑连接 WiFi 时所使用的模式。

1.1 Wi-Fi 连接(STA 模式)

  1. 使用 wifi-connect 连接 WiFi。

    wifi-connect.sh Luckfox-2.4G luckfox12345
    • Luckfox-2.4G:无线网络名称
    • luckfox12345:无线网络的密码
    • 根据自己实际无线网络名称和密码修改,其它地方不用修改。
  2. 连接无线网络:

    udhcpc -i wlan0
  3. 按照上述设置方法,重启后会失效,我们需要写开机脚本来启动。(外接天线)

    nano /etc/init.d/S99wlan0
    #!/bin/sh

    WIFI_SSID="Luckfox-2.4G"
    WIFI_PASSWD="luckfox12345"


    echo_log() {
    echo "[wifi-autostart] $1"
    }

    wait_for_interface() {
    for i in $(seq 1 5); do
    if ip link show wlan0 > /dev/null 2>&1; then
    return 0
    fi
    echo_log "Waiting for wlan0... ($i)"
    sleep 2
    done
    return 1
    }

    case "$1" in
    start)

    echo_log "Checking wlan0 interface..."
    if ! wait_for_interface; then
    echo_log "wlan0 not found after retries, aborting."
    exit 1
    fi

    echo_log "Connecting to WiFi: $WIFI_SSID"
    if [ -x /usr/bin/wifi-connect.sh ]; then
    /usr/bin/wifi-connect.sh "$WIFI_SSID" "$WIFI_PASSWD"
    else
    echo_log "wifi-connect.sh not found or not executable"
    fi

    echo_log "Requesting IP via udhcpc..."
    udhcpc -i wlan0

    echo_log "WiFi setup done."
    ;;
    stop)
    echo_log "Stop command received. No action defined."
    ;;
    *)
    echo "Usage: $0 {start|stop}"
    exit 1
    ;;
    esac
    chmod +x  /etc/init.d/S99wlan0
    reboot

1.2 AP 模式

在嵌入式 Linux 系统中,将开发板配置为 Wi-Fi 热点(AP 模式)主要依赖 hostapddnsmasq/udhcpd,它们分别负责无线网络的创建与客户端的管理:

  • hostapd:作为无线热点的核心组件,负责创建 Wi-Fi 网络,定义 SSID、加密方式等关键参数,使设备能够以 AP 模式运行。
  • dnsmasq/udhcpd:提供 DHCP 服务器功能,自动为连接的客户端分配 IP 地址(如 192.168.4.x),并支持 DNS 解析,确保设备无需手动配置网络参数。

通过 hostapd 负责信号广播,dnsmasq/udhcpd 进行地址分配,即可搭建一个完整的 Wi-Fi 热点,实现设备间的无线通信。

操作步骤:

  1. 打开 hostapd 配置文件,修改 Wi-Fi 名称。

    vim /etc/hostapd.conf 
    • Wi-Fi 名称(ssid):
    • Wi-Fi 密码(wpa_passphrase):
  2. 启用 wpa 加密。

    • 启用 WPA2 加密:
    • 基于 PSK(预共享密钥)认证(常见于家庭和小型网络):
  3. 运行创建热点。

    hostapd /etc/hostapd.conf &
    • 运行效果:
  4. 连接成功。

1.3 WiFi 速率测试

  1. 虚拟机或者主机端

    iperf3 -s -i 10 -p 5001
    • -s:指定 iperf3 运行在服务器模式
    • -i:设置了报告间隔的时间为 10 秒
    • -p:这个参数指定服务器端口为5001
  2. 开发板

    iperf3 -c 192.168.10.176 -p 5001 -f m -i 2 -t 24
    • -c:指定客户端模式,并设置要连接的服务器IP地址为192.168.10.176
    • -p:指定服务器端口为5001
    • -f:指定报告的格式。m 代表 Mbps,即报告的带宽单位为兆比特每秒
    • -i:指定报告的间隔时间为每1秒
    • -t :指定测试的持续时间为30秒

2. 蓝牙测试

在系统默认配置中,Wi-Fi 模块与蓝牙功能采用低功耗协同工作模式(BLE),当需要连接传统蓝牙设备(如音箱)时,必须手动切换蓝牙工作模式。

  1. /usr/bin/wifibt-util.sh中注释掉如下内容:

    # Aicsemi AIC8800DC       a69c:88dc       aic8800_fdrv.ko
  2. 重启开发板。

    reboot
  3. 启动蓝牙。

    root@luckfox:~# bluetoothctl 
    hci0 new_settings: powered bondable ssp br/edr le secure-conn
    Agent registered
    [CHG] Controller 38:54:39:03:10:70 Powered: yes
    [CHG] Controller 38:54:39:03:10:70 Pairable: yes
    [bluetooth]# power on #打开蓝牙
    [bluetooth]# scan on #扫描蓝牙设备
    [bluetooth]# trust 12:11:32:DE:A3:03 #信任蓝牙设备号
    [bluetooth]# pair 12:11:32:DE:A3:03
    [bluetooth]# connect 12:11:32:DE:A3:03 #连接蓝牙
    [M1]# exit