These days, WE are studying the soft bus component. Because we need to connect WiFi for debugging, it will definitely be inappropriate if we follow the following steps in the official document:
In the community to find the teacher “Hi3861 WiFi operation, hot connection” and netizens Double__ organized “Hi3861 WiFi connection”, the reference code can run and connect WiFi, but the personal feeling is still slightly complex/complex, so I studied it myself.
First of all, the above official steps can be simplified as:
Step 1: AT+STARTSTA # STARTSTA mode
Step 2: AT+CONN=”SSID”,,2,”PASSWORD” step 2: AT+CONN=”SSID”,,2,”PASSWORD
Step 3: AT+DHCP=wlan0,1 # Request the IP address of Wlan0 to the AP through DHCP
The other steps in the middle can be omitted entirely.
Let’s go to the AT module and have a look:
Hi_void app_main (hi_void) {# if defined (CONFIG_AT_COMMAND) | | defined (CONFIG_FACTORY_TEST_MODE) / / the AT command module initialization. If the initialization succeeds, the registration of various AT instructions begins. 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(); / / same as above at_wifi. C # endif / / vendor/hisi/hi3861 hi3861 / components/at/SRC/at_hipriv hi_at_hipriv_cmd_register c (); #ifndef CONFIG_FACTORY_TEST_MODE #ifdef LOSCFG_APP_MESH hi_at_mesh_cmd_register(); / / same as above at_wifi. C # endif / / vendor/hisi/hi3861 hi3861 / components/at/SRC/at_lowpower hi_at_lowpower_cmd_register c (); #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(); / / same as above at_hipriv. C / / vendor/hisi/hi3861 hi3861 / components/at/SRC/at_io hi_at_io_cmd_register c (); }Copy the code
Hi_at_sys_cmd_register () registers all AT commands supported by Hi3861 project. For details, please refer to the code.
** Step 1: AT+STARTSTA: ** AT at_wifi.c, call hi_wifi_STA_start (ifname, &len) to implement the function
{“+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 at_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 is located AT at_general. C, and netifapi_netif_find(argv[0]) and netifapi_DHcp_start (netif_p) are called to realize the function
{“+DHCP“, 5, HI_NULL, HI_NULL, (at_call_back_func)at_setup_dhcp, HI_NULL}
WifiLink() encapsulates the above three steps into the API: WifiLink()
#include "hi_wifi_api.h" #include "lwip/netifapi.h" void WifiLink(void) { static BOOL fgWifiConnected = FALSE; If (fgWifiConnected) // Prevent WiFi return; printf("[WifiLink] Begin: fgWifiConnected[F]\n"); //step 1: AT+STARTSTA // # 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; Hi_wifi_assoc_request request = {0}; //step 2: AT+CONN="SSID",,2,"PASSWORD" // 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; Struct netif* p_netif = netifapi_netif_find(ifname); struct netif = netifapi_netif_find(ifname); if(NULL == p_netif) { printf("[WifiLink] netifapi_netif_find fail\n"); return; } #if 1 //DHCP automatically assigns IP addresses if(HISI_OK! = netifapi_dhcp_start(p_netif)) { printf("[WifiLink] netifapi_dhcp_start fail\n"); return; } #else // Set fixed 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); 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; }Copy the code
Note that include_dirs in build. gn should add:
“//vendor/hisi/hi3861/hi3861/include”,
“//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include”,
You can call the above function SYS_RUN(WifiLink) or place it somewhere in your code to automatically connect to WiFi.
My local log:
[WifiLink] Begin: fgWifiConnected[F]
[WifiLink] End. fgWifiConnected[T]
+NOTICE:SCANFINISH
+NOTICE:CONNECTED
Then you can use AT+STASTAT, AT+IFCFG, AT+PING=www.baidu.com and other commands to confirm the connection status. It is completely OK.
Author: liangkz
For more information, visit Harmonyos.51cto.com, a collaboration between 51CTO and Huawei