[文章]【HarmonyOS HiSpark Wi-Fi IoT 套件试用连载】Wifi操作,热点连接

阅读量0
0
0
       根据连老师的教程:鸿蒙第4节 Hi3861 Wifi操作,热点连接 - HarmonyOS技术社区 - 电子技术论坛 - 广受欢迎的专业电子论坛! (elecfans.com)。但是我用的liteOs直接复制过去还是不行的,仔细阅读源码后,修改好后可以连上热点了,下面分享一下我的例程:
1、在appdemosrc下新建wifi_demo.c:
然后建立如下代码:
  1. #include <stdio.h>

  2. #include <unistd.h>

  3. #include <unistd.h>
  4. #include "hi_wifi_api.h"
  5. #include "lwip/ip_addr.h"
  6. #include "lwip/netifapi.h"

  7. #define APP_INIT_VAP_NUM    2
  8. #define APP_INIT_USR_NUM    2

  9. static struct netif *g_lwip_netif = NULL;

  10. /* clear netif's ip, gateway and netmask */
  11. void hi_sta_reset_addr(struct netif *pst_lwip_netif)
  12. {
  13.     ip4_addr_t st_gw;
  14.     ip4_addr_t st_ipaddr;
  15.     ip4_addr_t st_netmask;
  16.     printf("%s %d rn", __FILE__, __LINE__);
  17.     if (pst_lwip_netif == NULL) {
  18.         printf("hisi_reset_addr::Null param of netdevrn");
  19.         return;
  20.     }

  21.     IP4_ADDR(&st_gw, 0, 0, 0, 0);
  22.     IP4_ADDR(&st_ipaddr, 0, 0, 0, 0);
  23.     IP4_ADDR(&st_netmask, 0, 0, 0, 0);

  24.     netifapi_netif_set_addr(pst_lwip_netif, &st_ipaddr, &st_netmask, &st_gw);
  25. }

  26. void wifi_wpa_event_cb(const hi_wifi_event *hisi_event)
  27. {
  28.     if (hisi_event == NULL)
  29.         return;

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

  50. int hi_wifi_start_connect(void)
  51. {
  52.     int ret;
  53.     errno_t rc;
  54.     hi_wifi_assoc_request assoc_req = {0};

  55.     /* copy SSID to assoc_req */
  56.     //热点名称
  57.     rc = memcpy_s(assoc_req.ssid, HI_WIFI_MAX_SSID_LEN + 1, "you ssid", sizeof("you ssid")); /* 9:ssid length */
  58.     if (rc != EOK) {
  59.         printf("%s %d rn", __FILE__, __LINE__);
  60.         return -1;
  61.     }

  62.     /*
  63.      * OPEN mode
  64.      * for WPA2-PSK mode:
  65.      * set assoc_req.auth as HI_WIFI_SECURITY_WPA2PSK,
  66.      * then memcpy(assoc_req.key, "12345678", 8).
  67.      */
  68.     //热点加密方式
  69.     assoc_req.auth = HI_WIFI_SECURITY_WPA2PSK;

  70.     /* 热点密码 */
  71.     memcpy(assoc_req.key, "you pwd", sizeof("you pwd"));


  72.     ret = hi_wifi_sta_connect(&assoc_req);
  73.     if (ret != HISI_OK) {
  74.         printf("%s %d rn", __FILE__, __LINE__);
  75.         return -1;
  76.     }
  77.     printf("%s %d rn", __FILE__, __LINE__);
  78.     //printf("%srn",g_lwip_netif->flags);
  79.     return 0;
  80. }

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

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

  90.     ret = hi_wifi_init(wifi_vap_res_num, wifi_user_res_num);
  91.     if (ret != HISI_OK) {
  92.         printf("%s %d rn", __FILE__, __LINE__);
  93.         //return -1;
  94.     }

  95.     printf("%s %d rn", __FILE__, __LINE__);
  96.     ret = hi_wifi_sta_start(ifname, &len);
  97.     if (ret != HISI_OK) {
  98.         printf("%s %d rn", __FILE__, __LINE__);
  99.         return -1;
  100.     }

  101.     /* register call back function to receive wifi event, etc scan results event,
  102.      * connected event, disconnected event.
  103.      */
  104.     ret = hi_wifi_register_event_callback(wifi_wpa_event_cb);
  105.     if (ret != HISI_OK) {
  106.         printf("register wifi event callback faiLEDn");
  107.     }

  108.     /* acquire netif for IP operation */
  109.     g_lwip_netif = netifapi_netif_find(ifname);
  110.     if (g_lwip_netif == NULL) {
  111.         printf("%s: get netif failedn", __FUNCTION__);
  112.         return -1;
  113.     }

  114.     /* start scan, scan results event will be received soon */
  115.     ret = hi_wifi_sta_scan();
  116.     if (ret != HISI_OK) {
  117.         printf("%s %d rn", __FILE__, __LINE__);
  118.         return -1;
  119.     }

  120.     sleep(5);   /* sleep 5s, waiting for scan result. */

  121.     hi_wifi_ap_info *pst_results = malloc(sizeof(hi_wifi_ap_info) * WIFI_SCAN_AP_LIMIT);
  122.     if (pst_results == NULL) {
  123.         printf("%s %d rn", __FILE__, __LINE__);
  124.         return -1;
  125.     }

  126.     ret = hi_wifi_sta_scan_results(pst_results, &num);
  127.     if (ret != HISI_OK) {
  128.         printf("%s %d rn", __FILE__, __LINE__);
  129.         free(pst_results);
  130.         return -1;
  131.     }

  132.     for (unsigned int loop = 0; (loop < num) && (loop < WIFI_SCAN_AP_LIMIT); loop++) {
  133.         printf("SSID: %sn", pst_results[loop].ssid);
  134.     }
  135.     free(pst_results);

  136.     /* if received scan results, select one SSID to connect */
  137.     ret = hi_wifi_start_connect();
  138.     if (ret != 0) {
  139.         printf("%s %d rn", __FILE__, __LINE__);
  140.         return -1;
  141.     }


  142.     return 0;
  143. }

  144. void hi_wifi_stop_sta(void)
  145. {
  146.     int ret;

  147.     ret = hi_wifi_sta_stop();
  148.     if (ret != HISI_OK) {
  149.         printf("failed to stop stan");
  150.     }

  151.     ret = hi_wifi_deinit();
  152.     if (ret != HISI_OK) {
  153.         printf("failed to deinit wifin");
  154.     }

  155.     g_lwip_netif = NULL;

  156. }

复制代码
大家把you ssid、you pwd更换成自己的wifi热点名称与密码。
然后在app_main函数中增加:
  1. extern int hi_wifi_start_sta(void);
  2. hi_wifi_start_sta();
复制代码
编后编译下载就可以连上自己家的路由器了:
连上热点.png

从路由器上看到连上一个客户端,ip地址为192.168.3.14,然后可以ping通:
ping通.png

【总结】经过这个示例的学习,可以设置自己的ssidpwd连上wifi。下一步学习tcpmqtt等示例。

回帖

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