描述
本文来源电子发烧友社区,作者:华仔stm32, 帖子地址:
https://bbs.elecfans.com/jishu_2287654_1_1.html跟着这篇学习了一下wifi连网:【HarmonyOS HiSpark Wi-Fi IoT 套件试用连载】第5章 WiFi联网(STA模式) - HarmonyOS技术社区 - 电子技术论坛 - 广受欢迎的专业电子论坛! (elecfans.com)。
wifi_app.c:
-
/**
-
******************************************************************************
-
* [url=home.php?mod=space&uid=1455510]@file[/url] wifi_app.c
-
* [url=home.php?mod=space&uid=40524]@author[/url] BruceOu
-
* [url=home.php?mod=space&uid=644434]@version[/url] V1.0
-
* @date 2022-06-19
-
* [url=home.php?mod=space&uid=2676013]@blog[/url]
-
* [url=home.php?mod=space&uid=3179494]@Official[/url] Accounts
-
* [url=home.php?mod=space&uid=2666770]@Brief[/url]
-
******************************************************************************
-
*/
-
#include
-
#include
-
#include "ohos_init.h"
-
#include "cmsis_os2.h"
-
-
#include"hi_wifi_api.h"
-
#include"lwip/ip_addr.h"
-
#include "lwip/netifapi.h"
-
-
#define SSID "HUAWEI-21321312"
-
#define PASSWORD "12423424234@"
-
-
static struct netif *g_lwip_netif = NULL;
-
-
/**
-
* @brief Set netif's ip, gatewayand netmask
-
* [url=home.php?mod=space&uid=3142012]@param[/url] pst_lwip_netif
-
* @retval None
-
*/
-
void hi_sta_set_addr(struct netif *pst_lwip_netif)
-
{
-
ip4_addr_t st_gw;
-
ip4_addr_t st_ipaddr;
-
ip4_addr_t st_netmask;
-
if (pst_lwip_netif == NULL)
-
{
-
printf("hisi_reset_addr::Nullparam of netdevrn");
-
return;
-
}
-
IP4_ADDR(&st_gw, 192, 168, 3, 1);
-
IP4_ADDR(&st_ipaddr, 192, 168, 3,100);
-
IP4_ADDR(&st_netmask, 255, 255, 255,0);
-
netifapi_netif_set_addr(pst_lwip_netif,&st_ipaddr, &st_netmask, &st_gw);
-
}
-
/**
-
* @brief Wifi connect
-
* @param None
-
* @retval None
-
*/
-
int hi_wifi_start_connect(void)
-
{
-
int ret;
-
errno_t rc;
-
hi_wifi_assoc_request assoc_req = {0};
-
// Copy SSID to assoc_req
-
rc = memcpy_s(assoc_req.ssid,HI_WIFI_MAX_SSID_LEN + 1, SSID, strlen(PASSWORD));
-
if (rc != EOK)
-
{
-
printf("[Wifi Connnect]hi_wifi_sta_connect fail");
-
printf("%s %d rn",__FILE__, __LINE__);
-
return -1;
-
}
-
//Set encryption method
-
assoc_req.auth = HI_WIFI_SECURITY_WPA2PSK;
-
// Wifi password
-
memcpy(assoc_req.key, PASSWORD,strlen(PASSWORD));
-
ret = hi_wifi_sta_connect(&assoc_req);
-
if (ret != HISI_OK)
-
{
-
printf("[WifiConnnect] hi_wifi_sta_connect fail");
-
printf("%s %d rn",__FILE__, __LINE__);
-
return -1;
-
}
-
return 0;
-
}
-
/**
-
* @brief wifi task
-
* @param None
-
* @retval None
-
*/
-
void wifi_task(void)
-
{
-
int ret;
-
char ifname[WIFI_IFNAME_MAX_SIZE + 1] ={0};
-
int len = sizeof(ifname);
-
unsigned int num = WIFI_SCAN_AP_LIMIT;
-
//Step 1: Start STA mode, AT+STARTSTA
-
ret = hi_wifi_sta_start(ifname, &len);
-
if (ret != HISI_OK)
-
{
-
printf("[Wifi Connnect]hi_wifi_sta_start fail");
-
printf("%s %d rn",__FILE__, __LINE__);
-
return;
-
}
-
// Step 2: Connect to the specified AP:,AT+CONN="SSID", ,2,"PASSWORD"
-
ret = hi_wifi_start_connect();
-
if (ret != 0)
-
{
-
printf("[Wifi Connnect]hi_wifi_start_connect fail");
-
printf("%s %d rn",__FILE__, __LINE__);
-
return ;
-
}
-
// Step 3: DHCP requests the IP address ofwlan0 from the AP, AT+DHCP=wlan0,1
-
g_lwip_netif = netifapi_netif_find(ifname);
-
if(NULL == g_lwip_netif)
-
{
-
printf("[Wifi Connnect]netifapi_netif_find fail");
-
printf("%s %d rn",__FILE__, __LINE__);
-
return;
-
}
-
//DHCP automatically assigns IP
-
if(ret !=netifapi_dhcp_start(g_lwip_netif))
-
{
-
printf("[Wifi Connnect]netifapi_dhcp_start fail");
-
return;
-
}
-
printf("[Wifi Connnect] Connect towifi successfullyn");
-
}
-
SYS_RUN(wifi_task);
-
复制代码
BUILD.gn:
-
static_library("wifi_app") {
-
sources = [
-
"wifi_app.c"
-
]
-
include_dirs = [
-
"//utils/native/lite/include"
-
]
-
}
复制代码
app/BUILD.gn为:
-
import("//build/lite/config/component/lite_component.gni")
-
-
lite_component("app") {
-
features = [
-
"iothardware:led_example",
-
"wifi_connect:wifi_app"
-
]
-
}
-
复制代码
编译通过,下载后显示wifi连接成功,但是报错:
-
ready to OS start
-
sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
-
FileSystem mount ok.
-
wifi init success!
-
hilog will init.
-
-
hievent will init.
-
-
hievent init success.
-
-
[Wifi Connnect] Connect towifi successfully
-
hiview init success.
-
No crash dump found!
-
+NOTICE:SCANFINISH
-
+NOTICE:NETWORK NOT FIND
-
+NOTICE:SCANFINISH
-
+NOTICE:NETWORK NOT FIND
复制代码
麻烦各位大佬帮解答一下。
打开APP阅读更多精彩内容