Wi-Fi SoftAP 开启无线热点测试
该教程演示如何通过开发板开启无线热点,然后在手机搜索该无线热点并连接。
一、准备工作
- 准备1块BearPi-Pico H3863开发板
- 准备一个手机,用于连接开发板开启的热点
二、编译 WIFI SoftAP Sample代码
在Windows下编译操作
点击工具栏中的“系统配置”,打开配置界面.
选择Support WIFI SoftAP Sample
Application ---> [*] Enable Sample. [ ] Enable the Sample of peripheral. [*] Enable the Sample of WIFI. [ ] Support WIFI STA Sample [*] Support WIFI SoftAP Sample. [ ] Support UDP Client Sample. [ ] Enable the Sample of products.
按下"Save"键保存配置。
打开softap_sample.c,修改开发板开启热点的名称和密码。
关键代码,位于
application\samples\wifi\softap_sample\softap_sample.c
td_s32 example_softap_function(td_void) { /* SoftAp接口的信息 */ td_char ssid[WIFI_MAX_SSID_LEN] = "BearPi_Pico_H3863"; td_char pre_shared_key[WIFI_MAX_KEY_LEN] = "123456789"; softap_config_stru hapd_conf = {0}; softap_config_advance_stru config = {0}; td_char ifname[WIFI_IFNAME_MAX_SIZE + 1] = "ap0"; /* 创建的SoftAp接口名 */ struct netif *netif_p = TD_NULL; ip4_addr_t st_gw; ip4_addr_t st_ipaddr; ip4_addr_t st_netmask; IP4_ADDR(&st_ipaddr, 192, 168, 43, 1); /* IP地址设置:192.168.43.1 */ IP4_ADDR(&st_netmask, 255, 255, 255, 0); /* 子网掩码设置:255.255.255.0 */ IP4_ADDR(&st_gw, 192, 168, 43, 2); /* 网关地址设置:192.168.43.2 */ /* 配置SoftAp基本参数 */ (td_void)memcpy_s(hapd_conf.ssid, sizeof(hapd_conf.ssid), ssid, sizeof(ssid)); (td_void)memcpy_s(hapd_conf.pre_shared_key, WIFI_MAX_KEY_LEN, pre_shared_key, WIFI_MAX_KEY_LEN); hapd_conf.security_type = 3; /* 3: 加密方式设置为WPA_WPA2_PSK */ hapd_conf.channel_num = 13; /* 13:工作信道设置为13信道 */ hapd_conf.wifi_psk_type = 0; /* 配置SoftAp网络参数 */ config.beacon_interval = 100; /* 100:Beacon周期设置为100ms */ config.dtim_period = 2; /* 2:DTIM周期设置为2 */ config.gi = 0; /* 0:short GI默认关闭 */ config.group_rekey = 86400; /* 86400:组播秘钥更新时间设置为1天 */ config.protocol_mode = 4; /* 4:协议类型设置为802.11b + 802.11g + 802.11n + 802.11ax */ config.hidden_ssid_flag = 1; /* 1:不隐藏SSID */ if (wifi_set_softap_config_advance(&config) != 0) { return -1; } /* 启动SoftAp接口 */ if (wifi_softap_enable(&hapd_conf) != 0) { return -1; } /* 配置DHCP服务器 */ netif_p = netif_find(ifname); if (netif_p == TD_NULL) { (td_void)wifi_softap_disable(); return -1; } if (netifapi_netif_set_addr(netif_p, &st_ipaddr, &st_netmask, &st_gw) != 0) { (td_void)wifi_softap_disable(); return -1; } if (netifapi_dhcps_start(netif_p, NULL, 0) != 0) { (td_void)wifi_softap_disable(); return -1; } PRINT("%s::SoftAp start success.\r\n", WIFI_SOFTAP_SAMPLE_LOG); return 0; }
编译烧录固件
参考环境搭建教程编译烧录代码
在Ubuntu下编译操作
在MobaXterm中输入:
./build.py menuconfig ws63-liteos-app
选择Support WIFI SoftAP Sample
Application ---> [*] Enable Sample. [ ] Enable the Sample of peripheral. [*] Enable the Sample of WIFI. [ ] Support WIFI STA Sample [*] Support WIFI SoftAP Sample. [ ] Support UDP Client Sample. [ ] Enable the Sample of products.
按下"ESC"键退出并保存配置。
打开sta_sample.c,修改开发板开启热点的名称和密码。
关键代码,位于
application\samples\wifi\softap_sample\softap_sample.c
td_s32 example_softap_function(td_void) { /* SoftAp接口的信息 */ td_char ssid[WIFI_MAX_SSID_LEN] = "BearPi_Pico_H3863"; td_char pre_shared_key[WIFI_MAX_KEY_LEN] = "123456789"; softap_config_stru hapd_conf = {0}; softap_config_advance_stru config = {0}; td_char ifname[WIFI_IFNAME_MAX_SIZE + 1] = "ap0"; /* 创建的SoftAp接口名 */ struct netif *netif_p = TD_NULL; ip4_addr_t st_gw; ip4_addr_t st_ipaddr; ip4_addr_t st_netmask; IP4_ADDR(&st_ipaddr, 192, 168, 43, 1); /* IP地址设置:192.168.43.1 */ IP4_ADDR(&st_netmask, 255, 255, 255, 0); /* 子网掩码设置:255.255.255.0 */ IP4_ADDR(&st_gw, 192, 168, 43, 2); /* 网关地址设置:192.168.43.2 */ /* 配置SoftAp基本参数 */ (td_void)memcpy_s(hapd_conf.ssid, sizeof(hapd_conf.ssid), ssid, sizeof(ssid)); (td_void)memcpy_s(hapd_conf.pre_shared_key, WIFI_MAX_KEY_LEN, pre_shared_key, WIFI_MAX_KEY_LEN); hapd_conf.security_type = 3; /* 3: 加密方式设置为WPA_WPA2_PSK */ hapd_conf.channel_num = 13; /* 13:工作信道设置为13信道 */ hapd_conf.wifi_psk_type = 0; /* 配置SoftAp网络参数 */ config.beacon_interval = 100; /* 100:Beacon周期设置为100ms */ config.dtim_period = 2; /* 2:DTIM周期设置为2 */ config.gi = 0; /* 0:short GI默认关闭 */ config.group_rekey = 86400; /* 86400:组播秘钥更新时间设置为1天 */ config.protocol_mode = 4; /* 4:协议类型设置为802.11b + 802.11g + 802.11n + 802.11ax */ config.hidden_ssid_flag = 1; /* 1:不隐藏SSID */ if (wifi_set_softap_config_advance(&config) != 0) { return -1; } /* 启动SoftAp接口 */ if (wifi_softap_enable(&hapd_conf) != 0) { return -1; } /* 配置DHCP服务器 */ netif_p = netif_find(ifname); if (netif_p == TD_NULL) { (td_void)wifi_softap_disable(); return -1; } if (netifapi_netif_set_addr(netif_p, &st_ipaddr, &st_netmask, &st_gw) != 0) { (td_void)wifi_softap_disable(); return -1; } if (netifapi_dhcps_start(netif_p, NULL, 0) != 0) { (td_void)wifi_softap_disable(); return -1; } PRINT("%s::SoftAp start success.\r\n", WIFI_SOFTAP_SAMPLE_LOG); return 0; }
编译烧录固件
参考环境搭建教程编译烧录代码
三、测试
烧录固件后,复位开发板,在手机上搜索开发板开启的热点,并输入自己设置的秘密进行连接