[讨论]【HarmonyOS HiSpark Wi-Fi IoT 套件试用连载】wifi连上不网

阅读量0
0
0
跟着这篇学习了一下wifi连网:【HarmonyOS HiSpark Wi-Fi IoT 套件试用连载】第5章 WiFi联网(STA模式) - HarmonyOS技术社区 - 电子技术论坛 - 广受欢迎的专业电子论坛! (elecfans.com)。
文件路径.png

wifi_app.c:
  1. /**
  2. ******************************************************************************
  3. * [url=home.php?mod=space&uid=1455510]@file[/url]                wifi_app.c
  4. * [url=home.php?mod=space&uid=40524]@author[/url]              BruceOu
  5. * [url=home.php?mod=space&uid=644434]@version[/url]             V1.0
  6. * @date                2022-06-19
  7. * [url=home.php?mod=space&uid=2676013]@blog[/url]               
  8. * [url=home.php?mod=space&uid=3179494]@Official[/url] Accounts   
  9. * [url=home.php?mod=space&uid=2666770]@Brief[/url]               
  10. ******************************************************************************
  11. */
  12. #include <stdio.h>
  13. #include <unistd.h>
  14. #include "ohos_init.h"
  15. #include "cmsis_os2.h"

  16. #include"hi_wifi_api.h"
  17. #include"lwip/ip_addr.h"
  18. #include "lwip/netifapi.h"

  19. #define SSID "HUAWEI-21321312"
  20. #define PASSWORD "12423424234@"

  21. static struct netif *g_lwip_netif = NULL;

  22. /**
  23. * @brief  Set netif's ip, gatewayand netmask
  24. * [url=home.php?mod=space&uid=3142012]@param[/url]  pst_lwip_netif
  25. * @retval None
  26. */
  27. void hi_sta_set_addr(struct netif *pst_lwip_netif)
  28. {
  29.     ip4_addr_t st_gw;
  30.     ip4_addr_t st_ipaddr;
  31.     ip4_addr_t st_netmask;
  32.     if (pst_lwip_netif == NULL)
  33.     {
  34.         printf("hisi_reset_addr::Nullparam of netdevrn");
  35.         return;
  36.     }
  37.     IP4_ADDR(&st_gw, 192, 168, 3, 1);
  38.     IP4_ADDR(&st_ipaddr, 192, 168, 3,100);
  39.     IP4_ADDR(&st_netmask, 255, 255, 255,0);
  40.     netifapi_netif_set_addr(pst_lwip_netif,&st_ipaddr, &st_netmask, &st_gw);
  41. }
  42. /**
  43. * @brief  Wifi connect
  44. * @param  None
  45. * @retval None
  46. */
  47. int hi_wifi_start_connect(void)
  48. {
  49.     int ret;
  50.     errno_t rc;
  51.     hi_wifi_assoc_request assoc_req = {0};
  52.     // Copy SSID to assoc_req
  53.     rc = memcpy_s(assoc_req.ssid,HI_WIFI_MAX_SSID_LEN + 1, SSID, strlen(PASSWORD));
  54.     if (rc != EOK)
  55.     {
  56.         printf("[Wifi Connnect]hi_wifi_sta_connect fail");
  57.         printf("%s %d rn",__FILE__, __LINE__);
  58.         return -1;
  59.     }
  60.     //Set encryption method
  61.     assoc_req.auth = HI_WIFI_SECURITY_WPA2PSK;
  62.     // Wifi password
  63.     memcpy(assoc_req.key, PASSWORD,strlen(PASSWORD));
  64.     ret = hi_wifi_sta_connect(&assoc_req);
  65.     if (ret != HISI_OK)
  66.     {
  67.        printf("[WifiConnnect] hi_wifi_sta_connect fail");
  68.         printf("%s %d rn",__FILE__, __LINE__);
  69.         return -1;
  70.     }
  71.        return 0;
  72. }
  73. /**
  74. * @brief  wifi task
  75. * @param  None
  76. * @retval None
  77. */
  78. void wifi_task(void)
  79. {
  80.     int ret;
  81.     char ifname[WIFI_IFNAME_MAX_SIZE + 1] ={0};
  82.     int len = sizeof(ifname);
  83.     unsigned int  num = WIFI_SCAN_AP_LIMIT;
  84.        //Step 1: Start STA mode, AT+STARTSTA
  85.     ret = hi_wifi_sta_start(ifname, &len);
  86.     if (ret != HISI_OK)
  87.     {
  88.         printf("[Wifi Connnect]hi_wifi_sta_start fail");
  89.         printf("%s %d rn",__FILE__, __LINE__);
  90.         return;
  91.     }
  92.     // Step 2: Connect to the specified AP:,AT+CONN="SSID", ,2,"PASSWORD"
  93.     ret = hi_wifi_start_connect();
  94.     if (ret != 0)
  95.     {
  96.         printf("[Wifi Connnect]hi_wifi_start_connect fail");
  97.         printf("%s %d rn",__FILE__, __LINE__);
  98.         return ;
  99.     }
  100.     // Step 3: DHCP requests the IP address ofwlan0 from the AP, AT+DHCP=wlan0,1  
  101.     g_lwip_netif = netifapi_netif_find(ifname);
  102.     if(NULL == g_lwip_netif)
  103.     {
  104.         printf("[Wifi Connnect]netifapi_netif_find fail");
  105.         printf("%s %d rn",__FILE__, __LINE__);
  106.         return;
  107.     }     
  108.     //DHCP automatically assigns IP
  109.     if(ret !=netifapi_dhcp_start(g_lwip_netif))
  110.     {
  111.         printf("[Wifi Connnect]netifapi_dhcp_start fail");
  112.         return;
  113.     }     
  114.     printf("[Wifi Connnect] Connect towifi successfullyn");
  115. }
  116. SYS_RUN(wifi_task);
复制代码
BUILD.gn:
  1. static_library("wifi_app") {
  2.     sources = [
  3.         "wifi_app.c"
  4.     ]
  5.     include_dirs = [
  6.         "//utils/native/lite/include"
  7.     ]
  8. }
复制代码
app/BUILD.gn为:
  1. import("//build/lite/config/component/lite_component.gni")

  2. lite_component("app") {
  3.     features = [
  4.         "iothardware:led_example",
  5.         "wifi_connect:wifi_app"
  6.     ]
  7. }
复制代码
编译通过,下载后显示wifi连接成功,但是报错:
  1. ready to OS start
  2. sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
  3. FileSystem mount ok.
  4. wifi init success!
  5. hilog will init.

  6. hievent will init.

  7. hievent init success.

  8. [Wifi Connnect] Connect towifi successfully
  9. hiview init success.
  10. No crash dump found!
  11. +NOTICE:SCANFINISH
  12. +NOTICE:NETWORK NOT FIND
  13. +NOTICE:SCANFINISH
  14. +NOTICE:NETWORK NOT FIND
复制代码
麻烦各位大佬帮解答一下。

回帖

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