[文章]【HarmonyOS开发板试用】IOT板子连接wifi

阅读量0
0
0


最开始使用Hi3861的时候,是使用AT指令去控制wifi的状态。不过在实际使用中,如果使用AT指令需要额外使用一个芯片去控制,很不方便。
Hi3861 为我们提供了很多wifi操作的API,可以通过调用这些API完成对wifi的扫描连接等操作。
主要代码如下:
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include "ohos_init.h"
  4. #include "cmsis_os2.h"
  5. #include <unistd.h>
  6. #include "hi_wifi_api.h"
  7. #include "lwip/ip_addr.h"
  8. #include "lwip/netifapi.h"

  9. #define APP_INIT_VAP_NUM    2
  10. #define APP_INIT_USR_NUM    2

  11. #define SSID_NAME     需要连接的wifi的名称
  12. #define SSID_PASSWORD 需要连接的wifi的密码

  13. static struct netif *g_lwip_netif = NULL;

  14. /* 清除netif的ip、网关和网络掩码 */
  15. void hi_sta_reset_addr(struct netif *pst_lwip_netif)
  16. {
  17.     ip4_addr_t st_gw;
  18.     ip4_addr_t st_ipaddr;
  19.     ip4_addr_t st_netmask;
  20.     printf("%s %d rn", __FILE__, __LINE__);
  21.     if (pst_lwip_netif == NULL) {
  22.         printf("hisi_reset_addr::Null param of netdevrn");
  23.         return;
  24.     }

  25.     IP4_ADDR(&st_gw, 0, 0, 0, 0);
  26.     IP4_ADDR(&st_ipaddr, 0, 0, 0, 0);
  27.     IP4_ADDR(&st_netmask, 0, 0, 0, 0);

  28.     netifapi_netif_set_addr(pst_lwip_netif, &st_ipaddr, &st_netmask, &st_gw);
  29. }

  30. void wifi_wpa_event_cb(const hi_wifi_event *hisi_event)
  31. {
  32.     if (hisi_event == NULL)
  33.         return;

  34.     switch (hisi_event->event) {
  35.         case HI_WIFI_EVT_SCAN_DONE:
  36.             printf("WiFi: Scan results availablen");
  37.             break;
  38.         case HI_WIFI_EVT_CONNECTED:
  39.             printf("WiFi: Connectedn");
  40.             netifapi_dhcp_start(g_lwip_netif);
  41.             break;
  42.         case HI_WIFI_EVT_DISCONNECTED:
  43.             printf("WiFi: Disconnectedn");
  44.             netifapi_dhcp_stop(g_lwip_netif);
  45.             hi_sta_reset_addr(g_lwip_netif);
  46.             break;
  47.         case HI_WIFI_EVT_WPS_TIMEOUT:
  48.             printf("WiFi: wps is timeoutn");
  49.             break;
  50.         default:
  51.             break;
  52.     }
  53. }

  54. int hi_wifi_start_connect(void)
  55. {
  56.     int ret;
  57.     errno_t rc;
  58.     hi_wifi_assoc_request assoc_req = {0};

  59.     /* 将SSID复制到assoc_req */
  60.     rc = memcpy_s(assoc_req.ssid, HI_WIFI_MAX_SSID_LEN + 1, SSID_NAME, strlen(SSID_NAME));
  61.     if (rc != EOK) {
  62.         printf("%s %d rn", __FILE__, __LINE__);
  63.         return -1;
  64.     }

  65.     //热点加密方式
  66.     assoc_req.auth = HI_WIFI_SECURITY_WPA2PSK;

  67.     /* 热点密码 */
  68.     memcpy(assoc_req.key, SSID_PASSWORD, strlen(SSID_PASSWORD));


  69.     ret = hi_wifi_sta_connect(&assoc_req);
  70.     if (ret != HISI_OK) {
  71.         printf("%s %d rn", __FILE__, __LINE__);
  72.         return -1;
  73.     }
  74.     printf("%s %d rn", __FILE__, __LINE__);
  75.     return 0;
  76. }

  77. int hi_wifi_start_sta(void)
  78. {
  79.     int ret;
  80.     char ifname[WIFI_IFNAME_MAX_SIZE + 1] = {0};
  81.     int len = sizeof(ifname);
  82.     const unsigned char wifi_vap_res_num = APP_INIT_VAP_NUM;
  83.     const unsigned char wifi_user_res_num = APP_INIT_USR_NUM;
  84.     unsigned int  num = WIFI_SCAN_AP_LIMIT;

  85.     printf("%s %d rn", __FILE__, __LINE__);

  86.     ret = hi_wifi_init(wifi_vap_res_num, wifi_user_res_num);
  87.     if (ret != HISI_OK) {
  88.         printf("%s %d rn", __FILE__, __LINE__);
  89.         //return -1;
  90.     }

  91.     printf("%s %d rn", __FILE__, __LINE__);
  92.     ret = hi_wifi_sta_start(ifname, &len);
  93.     if (ret != HISI_OK) {
  94.         printf("%s %d rn", __FILE__, __LINE__);
  95.         return -1;
  96.     }

  97.     /* 注册回拨功能,接收wifi事件、etc扫描结果事件、已连接事件、已断开连接事件 */
  98.     ret = hi_wifi_register_event_callback(wifi_wpa_event_cb);
  99.     if (ret != HISI_OK) {
  100.         printf("register wifi event callback faiLEDn");
  101.     }

  102.     /* 获取用于IP操作的netif */
  103.     g_lwip_netif = netifapi_netif_find(ifname);
  104.     if (g_lwip_netif == NULL) {
  105.         printf("%s: get netif failedn", __FUNCTION__);
  106.         return -1;
  107.     }

  108.     /* 开始扫描,扫描结果事件将很快收到 */
  109.     ret = hi_wifi_sta_scan();
  110.     if (ret != HISI_OK) {
  111.         printf("%s %d rn", __FILE__, __LINE__);
  112.         return -1;
  113.     }

  114.     sleep(5);   /* 睡眠5秒,等待扫描结果 */

  115.     hi_wifi_ap_info *pst_results = malloc(sizeof(hi_wifi_ap_info) * WIFI_SCAN_AP_LIMIT);
  116.     if (pst_results == NULL) {
  117.         printf("%s %d rn", __FILE__, __LINE__);
  118.         return -1;
  119.     }

  120.     ret = hi_wifi_sta_scan_results(pst_results, &num);
  121.     if (ret != HISI_OK) {
  122.         printf("%s %d rn", __FILE__, __LINE__);
  123.         free(pst_results);
  124.         return -1;
  125.     }

  126.     for (unsigned int loop = 0; (loop < num) && (loop < WIFI_SCAN_AP_LIMIT); loop++) {
  127.         printf("SSID: %sn", pst_results[loop].ssid);
  128.     }
  129.     free(pst_results);

  130.     /* 如果收到扫描结果,请选择一个要连接的SSID */
  131.     ret = hi_wifi_start_connect();
  132.     if (ret != 0) {
  133.         printf("%s %d rn", __FILE__, __LINE__);
  134.         return -1;
  135.     }


  136.     return 0;
  137. }

  138. void hi_wifi_stop_sta(void)
  139. {
  140.     int ret;

  141.     ret = hi_wifi_sta_stop();
  142.     if (ret != HISI_OK) {
  143.         printf("failed to stop stan");
  144.     }

  145.     ret = hi_wifi_deinit();
  146.     if (ret != HISI_OK) {
  147.         printf("failed to deinit wifin");
  148.     }

  149.     g_lwip_netif = NULL;

  150.    
  151. }

  152. void WiFiExampleEntry(void)
  153. {
  154.     hi_wifi_start_sta();
  155. }

  156. SYS_RUN(WiFiExampleEntry);
复制代码
运行之后log如下
  1. ready to OS start

  2. sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00

  3. formatting spiffs...
  4. FileSystem mount ok.
  5. wifi init success!
  6. ../../applications/sample/wifi-iot/app/wifi_demo/wifi_demo.c 111

  7. ../../applications/sample/wifi-iot/app/wifi_demo/wifi_demo.c 115

  8. ../../applications/sample/wifi-iot/app/wifi_demo/wifi_demo.c 119


  9. +NOTICE:SCANFINISH
  10. WiFi: Scan results available
  11. SSID: ChinaNet-9qDC
  12. SSID: SuperSpeedTurbo
  13. SSID:
  14. SSID:
  15. SSID: 浜嶱HICOMM_F1
  16. SSID: CMCC-TnpK
  17. SSID: Lenovo Wi-Fi AP R3200
  18. SSID: st
  19. SSID: ChinaNet-3MFk
  20. SSID:
  21. SSID: ChinaNet-yLWz
  22. SSID: CU_HAfG
  23. SSID: ChinaNet-bwEU
  24. SSID: Tenda_445420
  25. SSID: CU_9JXc
  26. SSID: FAST_4EAC
  27. SSID: @PHICOMM_35
  28. SSID: 瀛欐垚鎴愭槸瀚╃埞
  29. SSID: 绫冲
  30. ../../applications/sample/wifi-iot/app/wifi_demo/wifi_demo.c 98

  31. 00 00:00:00 0 196 D 0/HIVIEW: hilog init success.
  32. 00 00:00:00 0 196 D 0/HIVIEW: log limit init success.
  33. 00 00:00:00 0 196 I 1/SAMGR: Bootstrap core services(count:3).
  34. 00 00:00:00 0 196 I 1/SAMGR: Init service:0x4aea5c TaskPool:0xfc3e8
  35. 00 00:00:00 0 196 I 1/SAMGR: Init service:0x4aea80 TaskPool:0xfc408
  36. 00 00+NOTICE:CONNECTED
  37. :00:00 0 196 I 1/SAMGR: Init service:0x4aeb90 TaskPool:0xfc7bc
  38. 00 00:00:00 0 228 I 1/SAMGR: Init service 0x4aea80 <time: 5570ms> success!
  39. 00 00:00:00 0 28 I 1/SAMGR: Init service 0x4aea5c <time: 5570ms> success!
  40. 00 00:00:00 0 72 D 0/HIVIEW: hiview init success.
  41. 00 00:00:00 0 72 I 1/SAMGR: Init service 0x4aeb90 <time: 5570ms> success!
  42. 00 00:00:00 0 72 I 1/SAMGR: Initialized all core system services!
  43. 00 00:00:00 0 28 I 1/SAMGR: Bootstrap system and application services(count:0).
  44. 00 00:00:00 0 28 I 1/SAMGR: Initialized all system and application services!
  45. 00 00:00:00 0 28 I 1/SAMGR: Bootstrap dynamic registered services(count:0).
  46. WiFi: Connected
复制代码
其中最后打印的WiFi: Connected代表已经成功连接到wifi

回帖

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。 侵权投诉
链接复制成功,分享给好友