• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Bearpi开发板HarmonyOS:WiFi STA联网

武飞扬头像
风雨依依
帮助1

wifi_device.h接口简介

  • 启用Wifi STA 模式
    WifiErrorCode EnableWifi(void);

  • 禁用Wifi STA 模式
    WifiErrorCode DisableWifi(void);

  • 检查Wifi STA模式是否启用
    int IsWifiActive(void);

  • 扫描热点信息
    WifiErrorCode Scan(void);

  • 获取所有扫描到的热点列表
    WifiErrorCode GetScanInfoList(WifiScanInfo* result, unsigned int* size);

  • 配置连接到热点信息
    WifiErrorCode AddDeviceConfig(const WifiDeviceConfig* config, int* result);

  • 获取配置连接到热点信息
    WifiErrorCode GetDeviceConfigs(WifiDeviceConfig* result, unsigned int* size);

  • 删除指定的热点配置信息
    WifiErrorCode RemoveDevice(int networkId);

  • 接到指定的热点
    WifiErrorCode ConnectTo(int networkId);

  • 断开Wifi连接
    WifiErrorCode Disconnect(void);

  • 获取热点连接信息
    WifiErrorCode GetLinkedInfo(WifiLinkedInfo* result);

  • 获取设备的MAC地址
    WifiErrorCode GetDeviceMacAddress(unsigned char* result);

连接到指定wifi代码

#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include <string.h>
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "wifiiot_adc.h"
#include "wifiiot_errno.h"
#include "wifiiot_uart.h"
#include "lwip/netifapi.h"
#include "wifi_hotspot.h"
#include "wifi_device.h"
#include "wifi_error_code.h"

#include "lwip/netif.h"
#include "lwip/ip4_addr.h"
#include "lwip/api_shell.h"


#define DEF_TIMEOUT 15
#define ONE_SECOND 1

static void WiFiInit(void);
static void WaitSacnResult(void);
static int WaitConnectResult(void);
static void OnWifiScanStateChangedHandler(int state, int size);
static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info);
static void OnHotspotStaJoinHandler(StationInfo *info);
static void OnHotspotStateChangedHandler(int state);
static void OnHotspotStaLeaveHandler(StationInfo *info);

static int g_staScanSuccess = 0;
static int g_ConnectSuccess = 0;
static int ssid_count = 0;
WifiEvent g_wifiEventHandler = {0};
WifiErrorCode error;

#define SELECT_WLAN_PORT "wlan0"

#define SELECT_WIFI_SSID "JamesZhou"
#define SELECT_WIFI_PASSWORD "12345678"
#define SELECT_WIFI_SECURITYTYPE WIFI_SEC_TYPE_PSK


static void WiFiInit(void)
{
  g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
  g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
  g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
  g_wifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChangedHandler;
  g_wifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChangedHandler;

  error = RegisterWifiEvent(&g_wifiEventHandler);

    if (error != WIFI_SUCCESS)
    {
        printf("register wifi event fail!\r\n");
    }
    else
    {
        printf("register wifi event succeed!\r\n");
    }

}


static void OnWifiScanStateChangedHandler(int state, int size)
{
  (void)state;
  if(size > 0)
  {
    ssid_count = size;
    g_staScanSuccess = 1;
  }
}

static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info)
{
    (void)info;
    if(state > 0)
    {
       g_ConnectSuccess = 1;
       printf("callback function for wifi connect\r\n");
    }
    else
    {
        printf("connect error,please check password\r\n");
    }

}
static void OnHotspotStateChangedHandler(int state)
{
    printf("HotspotStateChanged:state is %d.\n", state);
   
}

static void OnHotspotStaJoinHandler(StationInfo *info)
{
    (void)info;
    printf("STA join AP\n");
}

static void OnHotspotStaLeaveHandler(StationInfo *info)
{
    (void)info;
    printf("HotspotStaLeave:info is null.\n");

}


static void WaitSacnResult(void)
{

   int scanTimeout = DEF_TIMEOUT;
   while(scanTimeout)
   {
        sleep(ONE_SECOND);
        scanTimeout--;
        if (g_staScanSuccess == 1)
        {
            printf("WaitSacnResult:wait success[%d]s\n", (DEF_TIMEOUT - scanTimeout));
            break;
        }
   }
  if (scanTimeout <= 0)
  {
       printf("WaitSacnResult:timeout!\n");
  }

}

static int WaitConnectResult(void)
{
    int ConnectTimeout = DEF_TIMEOUT;
    while (ConnectTimeout > 0)
    {
        sleep(1);
        ConnectTimeout--;
        if (g_ConnectSuccess == 1)
        {
            printf("WaitConnectResult:wait success[%d]s\n", (DEF_TIMEOUT - ConnectTimeout));
            break;
        }
    }
    if (ConnectTimeout <= 0)
    {
        printf("WaitConnectResult:timeout!\n");
        return 0;
    }

    return 1;
}


static BOOL WifiSTATask(void)
{

   WifiScanInfo *info = NULL;
   unsigned int size = WIFI_SCAN_HOTSPOT_LIMIT;
    static struct netif *g_lwip_netif = NULL;
    WifiDeviceConfig select_ap_config = {0};

    osDelay(200);
    printf("<--System Init-->\r\n");

  //初始化WIFI
    WiFiInit();

    //使能WIFI
    if (EnableWifi() != WIFI_SUCCESS)
    {
        printf("EnableWifi failed, error = %d\n", error);
        return -1;
    }

    //判断WIFI是否激活
    if (IsWifiActive() == 0)
    {
        printf("Wifi station is not actived.\n");
        return -1;
    }

    info = malloc(sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT);
     if (info == NULL)
    {
        return -1;
    }

    //轮询查找WiFi列表


    do{
        //重置标志位
        ssid_count = 0;
        g_staScanSuccess = 0;

        //开始扫描
        Scan();

        //等待扫描结果
        WaitSacnResult();

        //获取扫描列表
        error = GetScanInfoList(info, &size);

    }while(g_staScanSuccess != 1);

     //打印WiFi列表
    printf("********************\r\n");
    for(uint8_t i = 0; i < ssid_count; i  )
    {
        printf("no:d, ssid:%-30s, rssi:]\r\n", i 1, info[i].ssid, info[i].rssi/100);
    }
    printf("********************\r\n");

     //连接指定的WiFi热点
    for(uint8_t i = 0; i < ssid_count; i  )
    {
        if (strcmp(SELECT_WIFI_SSID, info[i].ssid) == 0)
        {
            int result;

            printf("Select:= wireless, Waiting...\r\n", i 1);

            //拷贝要连接的热点信息
            strcpy(select_ap_config.ssid, info[i].ssid);
            strcpy(select_ap_config.preSharedKey, SELECT_WIFI_PASSWORD);
            select_ap_config.securityType = SELECT_WIFI_SECURITYTYPE;

            if (AddDeviceConfig(&select_ap_config, &result) == WIFI_SUCCESS)
            {
                if (ConnectTo(result) == WIFI_SUCCESS && WaitConnectResult() == 1)
                {
                    printf("WiFi connect succeed!\r\n");
                    g_lwip_netif = netifapi_netif_find(SELECT_WLAN_PORT);
                    break;
                }
            }
        }

        if(i == ssid_count-1)
        {
            printf("ERROR: No wifi as expected\r\n");
            while(1) osDelay(100);
        }
    }

      //启动DHCP
    if (g_lwip_netif)
    {
        dhcp_start(g_lwip_netif);
        printf("begain to dhcp");
    }

       //等待DHCP
    for(;;)
    {
        if(dhcp_is_bound(g_lwip_netif) == ERR_OK)
        {
            printf("<-- DHCP state:OK -->\r\n");

            //打印获取到的IP信息
            netifapi_netif_common(g_lwip_netif, dhcp_clients_info_show, NULL);
            break;
        }

        printf("<-- DHCP state:Inprogress -->\r\n");
        osDelay(100);
    }
   //执行其他操作
    for(;;)
    {
        osDelay(100);
    }

}
 
static void my_led_example(void)
{
   
    
     osThreadAttr_t attr;
     attr.attr_bits = 0;
     attr.name = "WifiSTATask";
     attr.cb_mem = NULL;
     attr.cb_size = 0;
     attr.priority = 24;
     attr.stack_size = 10240;

     if(osThreadNew((osThreadFunc_t)WifiSTATask,NULL,&attr) == NULL)
     {
        printf("Falied to create adc_func!\n");
    }
}

SYS_RUN(my_led_example);
学新通

运行效果

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhibback
系列文章
更多 icon
同类精品
更多 icon
继续加载