BL602&BL604 WIFI连接

本节实现WIFI的连接,本节的例程可参考后面的TCP CLIENT或者TCP SERVER 的相关例程

 

TCP CLIENT地址:

https://github.com/SmartArduino/Doiting_BL/tree/master/examples/WIFI_CLIENT

 

WIFI_CLIENT TCP SERVER地址:

https://github.com/SmartArduino/Doiting_BL/tree/master/examples/WIFI_SERVER

 

系统初始化:

void user_main(void){

    wifi_init(wifi_event_handler);
    wifi_setup_sta();
}

 

WIFI初始化相关函数:

/*
    wifi初始化
 */
void wifi_init(wifi_event_cb_t user_wifi_event_cb) {
    LOGI(TAG, "wifi init");
    cmd_stack_wifi(NULL, 0, 0, NULL);
    static_wifi_cb = user_wifi_event_cb;
void wifi_set_event_cb(void (*user_wifi_cb)(input_event_t *event, void *private_data));
wifi_set_event_cb(event_cb_wifi_event);

}
static void cmd_stack_wifi(char *buf, int len, int argc, char **argv)
{
    /*wifi fw stack and thread stuff*/
    static uint8_t stack_wifi_init  = 0;

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

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

}
/*
    wifi连接状态
 */
void wifi_event_handler(wifi_event_indicate_t event){
    switch (event)
    {
        case WIFI_EVENT_CONNECT:
            LOGE(TAG, "wifi_connect");
            break;
        case WIFI_EVENT_DISCONNECT:
            LOGE(TAG, "wifi_disconnect");
            break;
        default:
            LOGE(TAG, "wifi_event: %d",event);
            break;
    }
} 
static void event_cb_wifi_event(input_event_t *event, void *private_data)
{    
    switch (event->code) {
        case CODE_WIFI_ON_DISCONNECT:
        {
            LOGI(TAG, "wifi disconnect");
            if(static_wifi_connect_status != 0){
                static_wifi_connect_status = 0;
                if(static_wifi_cb != NULL){
                    static_wifi_cb(WIFI_EVENT_DISCONNECT);
                }
            }
        }
        break;
        case CODE_WIFI_ON_CONNECTED:
        {
            LOGI(TAG, "wifi connect");
        }
        break;
        case CODE_WIFI_ON_GOT_IP:
        {
            LOGI(TAG, "wifi get ip");
            if(static_wifi_connect_status != 1){
                static_wifi_connect_status = 1;
                if(static_wifi_cb != NULL){
                    static_wifi_cb(WIFI_EVENT_CONNECT);
                }
            }
        }
        break;
    }
}

Wifi连接成功后会输出连接的相关信息

 

posted @ 2020-12-12 18:10  深圳四博智联科技有限公司  阅读(899)  评论(0编辑  收藏  举报