Hi3861_WiFi IoT工程:WiFi自動連接


這些天在研究軟總線組件,因為要連接WiFi進行調試,如果按照官方文檔的如下步驟進行操作,肯定不合適:

Hi3861_WiFi IoT工程:WiFi自動連接

在社區上找到連志安老師的《Hi3861 WiFi操作,熱點連接》以及網友double__整理的《Hi3861 WiFi連接》,參考代碼可以運行和連接WiFi,但個人感覺仍稍顯復雜/繁雜,於是我自己就研究了一下。

 

首先,上面官方的步驟,我們可以簡化為:

step 1: AT+STARTSTA                                         # 啟動STA模式

step 2: AT+CONN="SSID", ,2,"PASSWORD"   # 連接指定AP,其中SSID/PASSWORD為待連接的熱點名稱和密碼

step 3: AT+DHCP=wlan0,1                                # 通過DHCP向AP請求wlan0的IP地址

中間的其他步驟,完全可以省略。

 

我們到 at 模塊去看一下:

hi_void app_main(hi_void) { #if defined(CONFIG_AT_COMMAND) || defined(CONFIG_FACTORY_TEST_MODE) //AT指令模塊的初始化。如果初始化成功,則開始注冊各類AT指令。 ret = hi_at_init(); // @//vendor/hisi/hi3861/hi3861/components/at/src/hi_at.c if (ret == HI_ERR_SUCCESS) { hi_at_sys_cmd_register(); // 同上 hi_at.c } #endif }
hi_void hi_at_sys_cmd_register(hi_void) { //vendor/hisi/hi3861/hi3861/components/at/src/at_general.c hi_at_general_cmd_register(); #ifndef CONFIG_FACTORY_TEST_MODE //vendor/hisi/hi3861/hi3861/components/at/src/at_wifi.c hi_at_sta_cmd_register(); hi_at_softap_cmd_register(); //同上 at_wifi.c #endif //vendor/hisi/hi3861/hi3861/components/at/src/at_hipriv.c hi_at_hipriv_cmd_register(); #ifndef CONFIG_FACTORY_TEST_MODE #ifdef LOSCFG_APP_MESH hi_at_mesh_cmd_register(); //同上 at_wifi.c #endif //vendor/hisi/hi3861/hi3861/components/at/src/at_lowpower.c hi_at_lowpower_cmd_register(); #endif hi_at_general_factory_test_cmd_register(); //同上 at_general.c hi_at_sta_factory_test_cmd_register(); //同上 at_wifi.c hi_at_hipriv_factory_test_cmd_register(); //同上 at_hipriv.c //vendor/hisi/hi3861/hi3861/components/at/src/at_io.c hi_at_io_cmd_register(); } 

hi_at_sys_cmd_register() 注冊了Hi3861工程所支持的所有 AT 指令,詳情請各位可以自己去查閱代碼,我們只看上面三條指令:

step 1: AT+STARTSTA:位於 at_wifi.c,調用 hi_wifi_sta_start(ifname, &len) 實現功能

{"+STARTSTA", 9, HI_NULL, HI_NULL, (at_call_back_func)cmd_sta_start_adv, (at_call_back_func)cmd_sta_start}

step 2: AT+CONN="SSID", ,2,"PASSWORD"      位於 at_wifi.c ,調用 hi_wifi_sta_connect(&assoc_req) 實現功能

{"+CONN", 5, HI_NULL, HI_NULL, (at_call_back_func)cmd_sta_connect, HI_NULL}

step 3: AT+DHCP=wlan0,1  位於 at_general.c,調用 netifapi_netif_find(argv[0]) 和 netifapi_dhcp_start(netif_p) 實現功能

{"+DHCP", 5, HI_NULL, HI_NULL, (at_call_back_func)at_setup_dhcp, HI_NULL}

 

把上面三步封裝到 API: WifiLink(),實現如下:

#include "hi_wifi_api.h" #include "lwip/netifapi.h" void WifiLink(void) { static BOOL fgWifiConnected = FALSE; if(fgWifiConnected) //防止重復連接WiFi return; printf("[WifiLink] Begin: fgWifiConnected[F]\n"); //step 1: AT+STARTSTA // #啟動STA模式 char ifname[WIFI_IFNAME_MAX_SIZE] = {0}; //“wlan0” int len = WIFI_IFNAME_MAX_SIZE; if (HISI_OK != hi_wifi_sta_start(ifname, &len)) { printf("[WifiLink] hi_wifi_sta_start fail\n"); return; } //step 2: AT+CONN="SSID", ,2,"PASSWORD" //# 連接指定AP,其中SSID/PASSWORD為待連接的熱點名稱和密碼 hi_wifi_assoc_request request = {0}; request.auth = HI_WIFI_SECURITY_WPA2PSK; //2 char* ssid = "SSID"; //Your SSID, HI_WIFI_MAX_SSID_LEN 32 Byte char* pswd = "PASSWORD"; //Your PSWD, HI_WIFI_MAX_KEY_LEN 64 Byte memcpy(request.ssid, ssid, strlen(ssid)); memcpy(request.key, pswd, strlen(pswd)); if (HISI_OK != hi_wifi_sta_connect(&request)) { printf("[wifilink] hi_wifi_sta_connect fail\n"); return; } //step 3: AT+DHCP=wlan0,1 //# 通過DHCP向AP請求wlan0的IP地址 struct netif* p_netif = netifapi_netif_find(ifname); if(NULL == p_netif) { printf("[WifiLink] netifapi_netif_find fail\n"); return; } #if 1 //DHCP 自動分配IP if(HISI_OK != netifapi_dhcp_start(p_netif)) { printf("[WifiLink] netifapi_dhcp_start fail\n"); return; } #else //設置固定 IP ip4_addr_t gw; ip4_addr_t ipaddr; ip4_addr_t netmask; IP4_ADDR(&gw, 192, 168, 1, 1); IP4_ADDR(&ipaddr, 192, 168, 1, 200); //固定到這個 IP IP4_ADDR(&netmask, 255, 255, 255, 0); if (HISI_OK != netifapi_netif_set_addr(p_netif, &ipaddr, &netmask, &gw)) { printf("[WifiLink] netifapi_netif_set_addr fail\n"); return; } if (HISI_OK != hi_wifi_start_connect()) { printf("[WifiLink] hi_wifi_start_connect fail\n"); return; } #endif fgWifiConnected = TRUE; printf("[WifiLink] End. fgWifiConnected[T]\n"); return; }

注意在 BUILD.gn 的include_dirs要添加:

"//vendor/hisi/hi3861/hi3861/include",
"//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include",

上面這個函數你可以把它做成 SYS_RUN(WifiLink), 也可以放到你的代碼中合適的地方去調用,就可以實現WiFi的自動連接了。

 

我本地log:

[WifiLink] Begin: fgWifiConnected[F]
[WifiLink] End.   fgWifiConnected[T]
+NOTICE:SCANFINISH
+NOTICE:CONNECTED

然后可以通過AT+STASTAT、AT+IFCFG、AT+PING=www.baidu.com等指令去確認連接狀態,完全OK。

作者:liangkz

想了解更多內容,請訪問51CTO和華為合作共建的鴻蒙社區:https://harmonyos.51cto.com


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM