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

描述

本文来源电子发烧友社区,作者:华仔stm32, 帖子地址:https://bbs.elecfans.com/jishu_2287654_1_1.html


跟着这篇学习了一下wifi连网:【HarmonyOS HiSpark Wi-Fi IoT 套件试用连载】第5章 WiFi联网(STA模式) - HarmonyOS技术社区 - 电子技术论坛 - 广受欢迎的专业电子论坛! (elecfans.com)。
wi-fi
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
  13. #include
  14. #include "ohos_init.h"
  15. #include "cmsis_os2.h"
  16.  
  17. #include"hi_wifi_api.h"
  18. #include"lwip/ip_addr.h"
  19. #include "lwip/netifapi.h"
  20.  
  21. #define SSID "HUAWEI-21321312"
  22. #define PASSWORD "12423424234@"
  23.  
  24. static struct netif *g_lwip_netif = NULL;
  25.  
  26. /**
  27. * @brief  Set netif's ip, gatewayand netmask
  28. * [url=home.php?mod=space&uid=3142012]@param[/url]  pst_lwip_netif
  29. * @retval None
  30. */
  31. void hi_sta_set_addr(struct netif *pst_lwip_netif)
  32. {
  33.     ip4_addr_t st_gw;
  34.     ip4_addr_t st_ipaddr;
  35.     ip4_addr_t st_netmask;
  36.     if (pst_lwip_netif == NULL)
  37.     {
  38.         printf("hisi_reset_addr::Nullparam of netdevrn");
  39.         return;
  40.     }
  41.     IP4_ADDR(&st_gw, 192, 168, 3, 1);
  42.     IP4_ADDR(&st_ipaddr, 192, 168, 3,100);
  43.     IP4_ADDR(&st_netmask, 255, 255, 255,0);
  44.     netifapi_netif_set_addr(pst_lwip_netif,&st_ipaddr, &st_netmask, &st_gw);
  45. }
  46. /**
  47. * @brief  Wifi connect
  48. * @param  None
  49. * @retval None
  50. */
  51. int hi_wifi_start_connect(void)
  52. {
  53.     int ret;
  54.     errno_t rc;
  55.     hi_wifi_assoc_request assoc_req = {0};
  56.     // Copy SSID to assoc_req
  57.     rc = memcpy_s(assoc_req.ssid,HI_WIFI_MAX_SSID_LEN + 1, SSID, strlen(PASSWORD));
  58.     if (rc != EOK)
  59.     {
  60.         printf("[Wifi Connnect]hi_wifi_sta_connect fail");
  61.         printf("%s %d rn",__FILE__, __LINE__);
  62.         return -1;
  63.     }
  64.     //Set encryption method
  65.     assoc_req.auth = HI_WIFI_SECURITY_WPA2PSK;
  66.     // Wifi password
  67.     memcpy(assoc_req.key, PASSWORD,strlen(PASSWORD));
  68.     ret = hi_wifi_sta_connect(&assoc_req);
  69.     if (ret != HISI_OK)
  70.     {
  71.        printf("[WifiConnnect] hi_wifi_sta_connect fail");
  72.         printf("%s %d rn",__FILE__, __LINE__);
  73.         return -1;
  74.     }
  75.        return 0;
  76. }
  77. /**
  78. * @brief  wifi task
  79. * @param  None
  80. * @retval None
  81. */
  82. void wifi_task(void)
  83. {
  84.     int ret;
  85.     char ifname[WIFI_IFNAME_MAX_SIZE + 1] ={0};
  86.     int len = sizeof(ifname);
  87.     unsigned int  num = WIFI_SCAN_AP_LIMIT;
  88.        //Step 1: Start STA mode, AT+STARTSTA
  89.     ret = hi_wifi_sta_start(ifname, &len);
  90.     if (ret != HISI_OK)
  91.     {
  92.         printf("[Wifi Connnect]hi_wifi_sta_start fail");
  93.         printf("%s %d rn",__FILE__, __LINE__);
  94.         return;
  95.     }
  96.     // Step 2: Connect to the specified AP:,AT+CONN="SSID", ,2,"PASSWORD"
  97.     ret = hi_wifi_start_connect();
  98.     if (ret != 0)
  99.     {
  100.         printf("[Wifi Connnect]hi_wifi_start_connect fail");
  101.         printf("%s %d rn",__FILE__, __LINE__);
  102.         return ;
  103.     }
  104.     // Step 3: DHCP requests the IP address ofwlan0 from the AP, AT+DHCP=wlan0,1  
  105.     g_lwip_netif = netifapi_netif_find(ifname);
  106.     if(NULL == g_lwip_netif)
  107.     {
  108.         printf("[Wifi Connnect]netifapi_netif_find fail");
  109.         printf("%s %d rn",__FILE__, __LINE__);
  110.         return;
  111.     }     
  112.     //DHCP automatically assigns IP
  113.     if(ret !=netifapi_dhcp_start(g_lwip_netif))
  114.     {
  115.         printf("[Wifi Connnect]netifapi_dhcp_start fail");
  116.         return;
  117.     }     
  118.     printf("[Wifi Connnect] Connect towifi successfullyn");
  119. }
  120. SYS_RUN(wifi_task);
  121.  
复制代码
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.  
  3. lite_component("app") {
  4.     features = [
  5.         "iothardware:led_example",
  6.         "wifi_connect:wifi_app"
  7.     ]
  8. }
  9.  
复制代码
编译通过,下载后显示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.  
  7. hievent will init.
  8.  
  9. hievent init success.
  10.  
  11. [Wifi Connnect] Connect towifi successfully
  12. hiview init success.
  13. No crash dump found!
  14. +NOTICE:SCANFINISH
  15. +NOTICE:NETWORK NOT FIND
  16. +NOTICE:SCANFINISH
  17. +NOTICE:NETWORK NOT FIND
复制代码
麻烦各位大佬帮解答一下。
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分