我是靠谱客的博主 忧虑秋天,最近开发中收集的这篇文章主要介绍BL602 WIFI的使用,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1、打开工程目录下的proj_config.mk文件,配置CONFIG_WIFI:=1

CONFIG_WIFI:=1

2、打开工程目录下的Makefile文件,添加WIFI依赖的配置,如下面的COMPONENTS_WIFI

COMPONENTS_BLSYS   := bltime blfdt blmtd bloop loopadc looprt loopset
COMPONENTS_VFS     := romfs
COMPONENTS_WIFI    := bl602_wifi bl602_wifidrv lwip lwip_dhcpd dns_server

INCLUDE_COMPONENTS += freertos_riscv_ram bl602 bl602_std hal_drv vfs yloop utils cli blog blog_testc
INCLUDE_COMPONENTS += easyflash4
INCLUDE_COMPONENTS += $(COMPONENTS_BLSYS)
INCLUDE_COMPONENTS += $(COMPONENTS_VFS)
INCLUDE_COMPONENTS += $(COMPONENTS_WIFI)
INCLUDE_COMPONENTS += $(PROJECT_NAME)

3、在bfl_main函数中调用tcpip_init

tcpip_init(NULL, NULL);

4、下面创建WIFI的任务,在aos_loop_proc函数中调用user_wifi_init

 user_wifi.c文件如下

#include "user_wifi.h"

//===================================================================
//							变量定义
//===================================================================
static wifi_conf_t conf =
{
    .country_code = "CN",
};


/********************************************************************
 *@brief	使能WIFI协议栈
 *@input    
 *@return
********************************************************************/
static void cmd_stack_wifi()
{
    /*wifi fw stack and thread stuff*/
    static uint8_t stack_wifi_init  = 0;

    if (1 == stack_wifi_init) {
        puts("Wi-Fi Stack Started already!!!rn");
        return;
    }
    stack_wifi_init = 1;

    printf("Start Wi-Fi fw @%lumsrn", bl_timer_now_us()/1000);
    hal_wifi_start_firmware_task();
    /*Trigger to start Wi-Fi*/
    printf("Start Wi-Fi fw is Done @%lumsrn", bl_timer_now_us()/1000);
    aos_post_event(EV_WIFI, CODE_WIFI_ON_INIT_DONE, 0);
}


/********************************************************************
 *@brief	连接指定的WIFI
 *@input    
 *@return
********************************************************************/
static void wifi_sta_connect(char *ssid, char *password)
{
    wifi_interface_t wifi_interface;

    wifi_interface = wifi_mgmr_sta_enable();
    wifi_mgmr_sta_connect(wifi_interface, ssid, password, NULL, NULL, 0, 0);
}


/********************************************************************
 *@brief	WIFI事件
 *@input    
 *@return
********************************************************************/
static void user_wifi_event_cb(input_event_t *event, void *private_data)
{
    static char *ssid;
    static char *password;

    switch (event->code) {
        case CODE_WIFI_ON_INIT_DONE:
        {
            printf("[APP] [EVT] INIT DONE %lldrn", aos_now_ms());
            wifi_mgmr_start_background(&conf);
        }
        break;
        case CODE_WIFI_ON_MGMR_DONE:
        {
            printf("[APP] [EVT] MGMR DONE %lld, now %lumsrn", aos_now_ms(), bl_timer_now_us()/1000);
            
            //开始连接WIFI
            wifi_sta_connect(USER_WIFI_SSID,USER_WIFI_PASS);
        }
        break;
        case CODE_WIFI_ON_MGMR_DENOISE:
        {
            printf("[APP] [EVT] Microwave Denoise is ON %lldrn", aos_now_ms());
        }
        break;
        case CODE_WIFI_ON_SCAN_DONE:
        {
            printf("[APP] [EVT] SCAN Done %lldrn", aos_now_ms());
            wifi_mgmr_cli_scanlist();
        }
        break;
        case CODE_WIFI_ON_SCAN_DONE_ONJOIN:
        {
            printf("[APP] [EVT] SCAN On Join %lldrn", aos_now_ms());
        }
        break;
        case CODE_WIFI_ON_DISCONNECT:
        {
            printf("[APP] [EVT] disconnect %lld, Reason: %srn",
                aos_now_ms(),
                wifi_mgmr_status_code_str(event->value)
            );
        }
        break;
        case CODE_WIFI_ON_CONNECTING:
        {
            printf("[APP] [EVT] Connecting %lldrn", aos_now_ms());
        }
        break;
        case CODE_WIFI_CMD_RECONNECT:
        {
            printf("[APP] [EVT] Reconnect %lldrn", aos_now_ms());
        }
        break;
        case CODE_WIFI_ON_CONNECTED:
        {
            printf("[APP] [EVT] connected %lldrn", aos_now_ms());
        }
        break;
        case CODE_WIFI_ON_PRE_GOT_IP:
        {
            printf("[APP] [EVT] connected %lldrn", aos_now_ms());
        }
        break;
        case CODE_WIFI_ON_GOT_IP:
        {
            printf("[APP] [EVT] GOT IP %lldrn", aos_now_ms());
            printf("[SYS] Memory left is %d Bytesrn", xPortGetFreeHeapSize());
        }
        break;
        case CODE_WIFI_ON_EMERGENCY_MAC:
        {
            printf("[APP] [EVT] EMERGENCY MAC %lldrn", aos_now_ms());
            hal_reboot();//one way of handling emergency is reboot. Maybe we should also consider solutions
        }
        break;
        case CODE_WIFI_ON_PROV_SSID:
        {
            printf("[APP] [EVT] [PROV] [SSID] %lld: %srn",
                    aos_now_ms(),
                    event->value ? (const char*)event->value : "UNKNOWN"
            );
            if (ssid) {
                vPortFree(ssid);
                ssid = NULL;
            }
            ssid = (char*)event->value;
        }
        break;
        case CODE_WIFI_ON_PROV_BSSID:
        {
            printf("[APP] [EVT] [PROV] [BSSID] %lld: %srn",
                    aos_now_ms(),
                    event->value ? (const char*)event->value : "UNKNOWN"
            );
            if (event->value) {
                vPortFree((void*)event->value);
            }
        }
        break;
        case CODE_WIFI_ON_PROV_PASSWD:
        {
            printf("[APP] [EVT] [PROV] [PASSWD] %lld: %srn", aos_now_ms(),
                    event->value ? (const char*)event->value : "UNKNOWN"
            );
            if (password) {
                vPortFree(password);
                password = NULL;
            }
            password = (char*)event->value;
        }
        break;
        case CODE_WIFI_ON_PROV_CONNECT:
        {
            printf("[APP] [EVT] [PROV] [CONNECT] %lldrn", aos_now_ms());
            printf("connecting to %s:%s...rn", ssid, password);
            wifi_sta_connect(ssid, password);
        }
        break;
        case CODE_WIFI_ON_PROV_DISCONNECT:
        {
            printf("[APP] [EVT] [PROV] [DISCONNECT] %lldrn", aos_now_ms());
        }
        break;
        case CODE_WIFI_ON_AP_STA_ADD:
        {
            printf("[APP] [EVT] [AP] [ADD] %lld, sta idx is %lurn", aos_now_ms(), (uint32_t)event->value);
        }
        break;
        case CODE_WIFI_ON_AP_STA_DEL:
        {
            printf("[APP] [EVT] [AP] [DEL] %lld, sta idx is %lurn", aos_now_ms(), (uint32_t)event->value);
        }
        break;
        default:
        {
            printf("[APP] [EVT] Unknown code %u, %lldrn", event->code, aos_now_ms());
            /*nothing*/
        }
    }
}


/********************************************************************
 *@brief	WIFI任务
 *@input    
 *@return
********************************************************************/
static void user_wifi_task(void *arg)
{
    aos_register_event_filter(EV_WIFI, user_wifi_event_cb, NULL);
    cmd_stack_wifi(NULL, 0, 0, NULL);

	while(1)
	{
        vTaskDelay(1000/portTICK_RATE_MS);
	}	

	vTaskDelete(NULL);
}


/********************************************************************
 *@brief	WIFI初始化
 *@input    
 *@return
********************************************************************/
void user_wifi_init()
{
    printf("user_wifi_initrn");
    xTaskCreate(user_wifi_task, "", 512, NULL, 2, NULL);
}




user_wifi.h文件如下

#ifndef __USER_WIFI_H__
#define __USER_WIFI_H__


#include <bl60x_fw_api.h>
#include <wifi_mgmr_ext.h>

#include <lwip/tcpip.h>
#include <lwip/sockets.h>
#include <lwip/netdb.h>
#include <lwip/tcp.h>
#include <lwip/err.h>

//===================================================================
//							常量定义
//===================================================================
#define USER_WIFI_SSID      "WIFI_TEST"
#define USER_WIFI_PASS      "12345678"


//===================================================================
//							结构体定义
//===================================================================




//===================================================================
//							函数声明
//===================================================================
void user_wifi_init();



#endif

最后

以上就是忧虑秋天为你收集整理的BL602 WIFI的使用的全部内容,希望文章能够帮你解决BL602 WIFI的使用所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部