[文章]

鸿蒙硬件HI3861-连接WIFI

2021-2-2 10:11:33  207 Hi3861 WiFi 鸿蒙开发板 鸿蒙操作系统
分享
0
鸿蒙硬件HI3861-连接WIFI鸿蒙其他教程请看https://blog.csdn.net/qq_33259323/category_10520249.html
目录是这样的

BUILD.gn
  1. static_library("wifi_demo") {
  2.     sources = [
  3.         #"wifi_scan_demo.c",
  4.         "wifi_connect_demo.c",
  5.     ]

  6.     include_dirs = [
  7.         "//utils/native/lite/include",
  8.         "//kernel/liteos_m/components/cmsis/2.0",
  9.         "//base/iot_hardware/inteRFaces/kits/wifiiot_lite",
  10.         "//foundation/communication/interfaces/kits/wifi_lite/wifiservice",
  11.         "//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include/",
  12.     ]
  13. }
复制代码
wifi_connect_demo.c
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>

  4. #include "ohos_init.h"
  5. #include "cmsis_os2.h"
  6. #include "wifi_device.h"

  7. #include "lwip/netifapi.h"
  8. #include "lwip/api_shell.h"

  9. #include "wifi_connect_demo.h"
  10. #include "mqtt_test.h"

  11. int WIFI_STATE = 0;


  12. // static char* SecurityTypeName(WifiSecurityType type)
  13. // {
  14. //     switch (type)
  15. //     {
  16. //     case WIFI_SEC_TYPE_OPEN:
  17. //         return "OPEN";
  18. //     case WIFI_SEC_TYPE_WEP:
  19. //         return "WEP";
  20. //     case WIFI_SEC_TYPE_PSK:
  21. //         return "PSK";
  22. //     case WIFI_SEC_TYPE_SAE:
  23. //         return "SAE";
  24. //     default:
  25. //         break;
  26. //     }
  27. //     return "unkow";
  28. // }

  29. static void PrintLinkedInfo(WifiLinkedInfo* info)
  30. {
  31.     if (!info) return;

  32.     static char macAddress[32] = {0};
  33.     unsigned char* mac = info->bssid;
  34.     snprintf(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
  35.         mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  36.     printf("bssid: %s, rssi: %d, connState: %d, reason: %d, ssid: %s\r\n",
  37.         macAddress, info->rssi, info->connState, info->disconnectedReason, info->ssid);
  38. }

  39. static int g_connected = 0;
  40. static void OnWifiConnectionChanged(int state, WifiLinkedInfo* info)
  41. {
  42.     if (!info) return;

  43.     printf("%s %d, state = %d, info = \r\n", __FUNCTION__, __LINE__, state);
  44.     PrintLinkedInfo(info);

  45.     if (state == WIFI_STATE_AVALIABLE) {
  46.         g_connected = 1;
  47.     } else {
  48.         g_connected = 0;
  49.     }
  50. }

  51. static void OnWifiScanStateChanged(int state, int size)
  52. {
  53.     printf("%s %d, state = %X, size = %d\r\n", __FUNCTION__, __LINE__, state, size);
  54. }

  55. static void Delay(uint32_t ms)
  56. {
  57.     uint32_t usPerTicks = (1000*1000) / osKernelGetTickFreq();
  58.     // printf("usPerTicks: %d\r\n", usPerTicks);
  59.     osDelay((ms * 1000) / usPerTicks);
  60.     usleep((ms * 1000) % usPerTicks);
  61. }

  62. static void WifiConnectTask(void *arg)
  63. {
  64.     (void)arg;
  65.     WifiErrorCode errCode;
  66.     WifiEvent eventListener = {
  67.         .OnWifiConnectionChanged = OnWifiConnectionChanged,
  68.         .OnWifiScanStateChanged = OnWifiScanStateChanged
  69.     };
  70.     WifiDeviceConfig apConfig = {};
  71.     int netId = -1;

  72.     Delay(10);
  73.     errCode = RegisterWifiEvent(&eventListener);
  74.     printf("RegisterWifiEvent: %d\r\n", errCode);

  75.     strcpy(apConfig.ssid, "WIFI名字");
  76.     strcpy(apConfig.preSharedKey, "密码");
  77.     apConfig.securityType = WIFI_SEC_TYPE_PSK;

  78.     while (1) {
  79.         errCode = EnableWifi();
  80.         printf("EnableWifi: %d\r\n", errCode);
  81.         Delay(100);

  82.         errCode = AddDeviceConfig(&apConfig, &netId);
  83.         printf("AddDeviceConfig: %d\r\n", errCode);

  84.         g_connected = 0;
  85.         errCode = ConnectTo(netId);
  86.         printf("ConnectTo(%d): %d\r\n", netId, errCode);

  87.         while (!g_connected) {
  88.             Delay(10);
  89.         }
  90.         printf("g_connected: %d\r\n", g_connected);
  91.         Delay(3000);

  92.         // 联网业务开始
  93.         // 这里是网络业务代码...
  94.         struct netif* iface = netifapi_netif_find("wlan0");
  95.         if (iface) {
  96.             err_t ret = netifapi_dhcp_start(iface);
  97.             printf("netifapi_dhcp_start: %d\r\n", ret);

  98.             Delay(2000); // wait DHCP server give me IP
  99.             ret = netifapi_netif_common(iface, dhcp_clients_info_show, NULL);
  100.             printf("netifapi_netif_common: %d\r\n", ret);
  101.         }
  102.         WIFI_STATE = 1;

  103.         break;
  104.         // 联网业务结束

  105.         Disconnect(); // disconnect with your AP

  106.         RemoveDevice(netId); // remove AP config

  107.         errCode = DisableWifi();
  108.         printf("DisableWifi: %d\r\n", errCode);
  109.         Delay(500);
  110.     }
  111. }

  112. void WifiConnectDemo(void){
  113.     osThreadAttr_t attr;

  114.     attr.name = "WifiConnectTask";
  115.     attr.attr_bits = 0U;
  116.     attr.cb_mem = NULL;
  117.     attr.cb_size = 0U;
  118.     attr.stack_mem = NULL;
  119.     attr.stack_size = 10240;
  120.     attr.priority = osPriorityNormal;

  121.     if (osThreadNew(WifiConnectTask, NULL, &attr) == NULL) {
  122.         printf("[WifiConnectDemo] Falied to create WifiConnectTask!\n");
  123.     }
  124. }

  125. //APP_FEATURE_INIT(WifiConnectDemo);
复制代码
wifi_connect_demo.h
  1. #ifndef WIFI_CONNECT_DEMO_H
  2. #define WIFI_CONNECT_DEMO_H

  3. extern int WIFI_STATE;


  4. void WifiConnectDemo(void);


  5. #endif
复制代码
然后在项目中使用
在BUILD.gn的features数组添加"//domains/iot/link/wifi:wifi_demo"
然后在hello_word.c中使用
#include "wifi_connect_demo.h"
void HelloWorld(void){
WifiConnectDemo();
}


如果有啥问题可以来B站联系我https://space.bilibili.com/309103931

评论

您需要登录后才可以回帖 登录 | 注册

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