[已解决] WIFI获取CSI信息功能的问题

gzy302577
Posts: 11
Joined: Sun May 30, 2021 1:03 pm

[已解决] WIFI获取CSI信息功能的问题

Postby gzy302577 » Tue Jun 29, 2021 10:36 am

我在使用ESP32-C3尝试获取CSI信息的时候,CSI的回调函数往往只能被执行一次或着一次都不会被执行(像是没收到CSI?)。
我使用的开发板是ESP32-C3和ESP32-WROOM模组,idf是4.4版本,代码是基于idf中examples\wifi\getting_started\station例程改写的,具体方式是在WIFI_EVENT_STA_START事件后依次调用esp_wifi_set_csi(1)、esp_wifi_set_csi_config(&configuration_csi)、esp_wifi_set_csi_rx_cb(&_wifi_csi_cb, NULL)。但似乎最终并没有正确收到CSI。
我参考了https://stevenmhernandez.github.io/ESP32-CSI-Tool/的代码,但直接编译该tool-kit代码现象也是没有收到CSI。
CSI的功能具体需要怎么使用呢?在idf文档里也没有找到相关资料。

我的代码如下,除了用注释和*标出来的以外,都是station例程里的内容(因为具体是哪里的问题也不知道只能全放上来了):

Code: Select all

#define EXAMPLE_ESP_MAXIMUM_RETRY  CONFIG_ESP_MAXIMUM_RETRY

/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;

/* The event group allows multiple bits for each event, but we only care about two events:
 * - we are connected to the AP with an IP
 * - we failed to connect after the maximum amount of retries */
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT      BIT1

static const char *TAG = "wifi station";

static int s_retry_num = 0;
void csi_init(void);

static void event_handler(void* arg, esp_event_base_t event_base,
                                int32_t event_id, void* event_data)
{
    if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
    	csi_init();//***********************************************************************************
        esp_wifi_connect();
    } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
        if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
            esp_wifi_connect();
            s_retry_num++;
            ESP_LOGI(TAG, "retry to connect to the AP");
        } else {
            xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
        }
        ESP_LOGI(TAG,"connect to the AP fail");
    } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
        ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
        ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
        s_retry_num = 0;
        xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
    }
}
//***********************************************************************************
void _wifi_csi_cb(void *ctx, wifi_csi_info_t *data) {
    wifi_csi_info_t d = data[0];
    char mac[20] = {0};
    sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", d.mac[0], d.mac[1], d.mac[2], d.mac[3], d.mac[4], d.mac[5]);

    printf("CSI_DATA,");
    printf("%s,", mac);

    // https://github.com/espressif/esp-idf/blob/9d0ca60398481a44861542638cfdc1949bb6f312/components/esp_wifi/include/esp_wifi_types.h#L314
    printf("%d,", d.rx_ctrl.rssi);
    。。。

    printf("\n");
    vTaskDelay(0);
}

void _print_csi_csv_header() {
    char *header_str = "type,role,mac,rssi,rate,sig_mode,mcs,bandwidth,smoothing,not_sounding,aggregation,stbc,fec_coding,sgi,noise_floor,ampdu_cnt,channel,secondary_channel,local_timestamp,ant,sig_len,rx_state,real_time_set,real_timestamp,len,CSI_DATA\n";
    printf(header_str);
}

void csi_init() {
    ESP_ERROR_CHECK(esp_wifi_set_csi(1));
    // @See: https://github.com/espressif/esp-idf/blob/master/components/esp_wifi/include/esp_wifi_types.h#L401
    wifi_csi_config_t configuration_csi;
    configuration_csi.lltf_en = 1;
    configuration_csi.htltf_en = 1;
    configuration_csi.stbc_htltf2_en = 1;
    configuration_csi.ltf_merge_en = 1;
    configuration_csi.channel_filter_en = 1;
    configuration_csi.manu_scale = 0;
    ESP_ERROR_CHECK(esp_wifi_set_csi_config(&configuration_csi));
    ESP_ERROR_CHECK(esp_wifi_set_csi_rx_cb(&_wifi_csi_cb, NULL));
    _print_csi_csv_header();
}
//***********************************************************************************

void wifi_init_sta(void)
{
    s_wifi_event_group = xEventGroupCreate();

    ESP_ERROR_CHECK(esp_netif_init());

    ESP_ERROR_CHECK(esp_event_loop_create_default());
    esp_netif_create_default_wifi_sta();

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    cfg.csi_enable = 1;//***********************************************************************************
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
    esp_wifi_set_ps(WIFI_PS_NONE);


    esp_event_handler_instance_t instance_any_id;
    esp_event_handler_instance_t instance_got_ip;
    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
                                                        ESP_EVENT_ANY_ID,
                                                        &event_handler,
                                                        NULL,
                                                        &instance_any_id));
    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
                                                        IP_EVENT_STA_GOT_IP,
                                                        &event_handler,
                                                        NULL,
                                                        &instance_got_ip));

    wifi_config_t wifi_config = {
        .sta = {
            .ssid = EXAMPLE_ESP_WIFI_SSID,
            .password = EXAMPLE_ESP_WIFI_PASS,
			.channel = 8,//***********************************************************************************
            /* Setting a password implies station will connect to all security modes including WEP/WPA.
             * However these modes are deprecated and not advisable to be used. Incase your Access point
             * doesn't support WPA2, these mode can be enabled by commenting below line */
        },
    };
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
    ESP_ERROR_CHECK(esp_wifi_start() );

    ESP_LOGI(TAG, "wifi_init_sta finished.");

    /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
     * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
    EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
            WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
            pdFALSE,
            pdFALSE,
            portMAX_DELAY);

    /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
     * happened. */
    if (bits & WIFI_CONNECTED_BIT) {
        ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
                 EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
    } else if (bits & WIFI_FAIL_BIT) {
        ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
                 EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
    } else {
        ESP_LOGE(TAG, "UNEXPECTED EVENT");
    }

    /* The event will not be processed after unregister */
    ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
    ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
    vEventGroupDelete(s_wifi_event_group);
}

void app_main(void)
{
    //Initialize NVS
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
      ESP_ERROR_CHECK(nvs_flash_erase());
      ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
    wifi_init_sta();
}
日志如下,在619行的时候收到了一条CSI,后续就没反应了,最后显示连接到路由器后一直没有其他输出了。

Code: Select all

I (439) wifi_init: tcp mbox: 6
I (439) wifi_init: tcp tx win: 5744
I (449) wifi_init: tcp rx win: 5744
I (449) wifi_init: tcp mss: 1440
I (449) wifi_init: WiFi IRAM OP enabled
I (459) wifi_init: WiFi RX IRAM OP enabled
I (459) wifi:Set ps type: 0

I (469) phy_init: phy_version 500,985899c,Apr 19 2021,16:05:08
I (579) wifi:set rx active PTI: 0, rx ack PTI: 0, and default PTI: 0
I (589) wifi:mode : sta (7c:df:a1:66:d4:50)
I (589) wifi:enable tsf
type,role,mac,rssi,rate,sig_mode,mcs,bandwidth,smoothing,not_sounding,aggregatio
n,stbc,fec_coding,sgi,noise_floor,ampdu_cnt,channel,secondary_channel,local_time
stamp,ant,sig_len,rx_state,real_time_set,real_timestamp,len,CSI_DATA
I (609) wifi station: wifi_init_sta finished.
I (609) wifi:new:<1,1>, old:<1,0>, ap:<255,255>, sta:<1,1>, prof:1
I (619) wifi:state: init -> auth (b0)
CSI_DATA,0E:96:E6:79:97:B1,-74,11,0,0,0,0,0,0,0,0,0,-96,0,1,1,611964,0,419,0,
I(629) wifi:state: auth -> assoc (0)
I (639) wifi:state: assoc -> run (10)
I (649) wifi:connected with TP-LINK_235, aid = 2, channel 1, 40U, bssid = 34:96:
72:38:fb:70
I (649) wifi:security: WPA2-PSK, phy: bgn, rssi: -49
I (649) wifi:pm start, type: 0

I (659) wifi:set rx beacon pti, rx_bcn_pti: 0, bcn_timeout: 0, mt_pti: 25000, mt
_time: 10000
W (669) wifi:<ba-add>idx:0 (ifx:0, 34:96:72:38:fb:70), tid:0, ssn:0, winSize:64
I (699) wifi:BcnInt:102400, DTIM:1
I (1349) esp_netif_handlers: sta ip: 192.168.2.102, mask: 255.255.255.0, gw: 192
.168.2.1
I (1349) wifi station: got ip:192.168.2.102
I (1349) wifi station: connected to ap SSID:*** password:***

ESP_Gargamel
Posts: 786
Joined: Wed Nov 14, 2018 8:45 am

Re: WIFI获取CSI信息功能的问题

Postby ESP_Gargamel » Mon Jul 05, 2021 8:33 am

因为此时 ESP 设备是被动收包,有两个方式,1、你去 ping 你的设备;2、csi 初始化时,加上 ESP_ERROR_CHECK(esp_wifi_set_promiscuous(true));,这里有述:https://docs.espressif.com/projects/esp ... -configure

gzy302577
Posts: 11
Joined: Sun May 30, 2021 1:03 pm

Re: WIFI获取CSI信息功能的问题

Postby gzy302577 » Mon Jul 05, 2021 11:26 am

ESP_Gargamel wrote:
Mon Jul 05, 2021 8:33 am
因为此时 ESP 设备是被动收包,有两个方式,1、你去 ping 你的设备;2、csi 初始化时,加上 ESP_ERROR_CHECK(esp_wifi_set_promiscuous(true));,这里有述:https://docs.espressif.com/projects/esp ... -configure
感谢,已解决

Who is online

Users browsing this forum: No registered users and 142 guests