ESP32-C6: When to call "esp_wifi_set_channel"?

max1337
Posts: 4
Joined: Thu May 11, 2023 8:58 am

ESP32-C6: When to call "esp_wifi_set_channel"?

Postby max1337 » Thu May 11, 2023 9:07 am

I am using the ESP32-C6-WROOM-1 board with ESP-IDF v5.2-dev-503-g17451f1fb3.
I am based off of the simple station_example and I would like to change the channel of my module but I am unsure where to do this because the documentation says:
  • This API should be called after esp_wifi_start()
  • When device is in STA mode, this API should not be called when STA is scanning or connecting to an external AP
I tried it like this:

Code: Select all

    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_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B|WIFI_PROTOCOL_11G|WIFI_PROTOCOL_11N) );
	ESP_ERROR_CHECK( esp_wifi_set_csi_rx_cb(&csi_rx_cb, NULL) );
    ESP_ERROR_CHECK( esp_wifi_start() );
	ESP_ERROR_CHECK( esp_wifi_set_channel(1, WIFI_SECOND_CHAN_ABOVE) );
	ESP_ERROR_CHECK( esp_wifi_set_csi(true) );
    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);
    }
which is after esp_wifi_start but I get the runtime error:
wifi:STA is scanning or connecting, or AP has connected with external STAs, cannot set channel
ESP_ERROR_CHECK failed: esp_err_t 0xffffffff (ESP_FAIL) at 0x420096fa
file: "./main/station_example_main.c" line 147
func: wifi_init_sta
expression: esp_wifi_set_channel(1, WIFI_SECOND_CHAN_ABOVE)
and I tried it like this:

Code: Select all

    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_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B|WIFI_PROTOCOL_11G|WIFI_PROTOCOL_11N) );
	ESP_ERROR_CHECK( esp_wifi_set_csi_rx_cb(&csi_rx_cb, NULL) );
    ESP_ERROR_CHECK( esp_wifi_start() );
	ESP_ERROR_CHECK( esp_wifi_set_csi(true) );

    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_ERROR_CHECK( esp_wifi_set_channel(1, WIFI_SECOND_CHAN_ABOVE) );
        ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
                 EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
    }
because that's where the connection has been established, but still, I get this error:
wifi station: got ip:192.168.252.149
wifi:STA is scanning or connecting, or AP has connected with external STAs, cannot set channel
ESP_ERROR_CHECK failed: esp_err_t 0xffffffff (ESP_FAIL) at 0x42009756
file: "./main/station_example_main.c" line 162
func: wifi_init_sta
expression: esp_wifi_set_channel(1, WIFI_SECOND_CHAN_ABOVE)
So my question comes down to how to properly set the channel?

max1337
Posts: 4
Joined: Thu May 11, 2023 8:58 am

Re: ESP32-C6: When to call "esp_wifi_set_channel"?

Postby max1337 » Mon Jun 12, 2023 8:56 am

No one? Please

Who is online

Users browsing this forum: Majestic-12 [Bot] and 127 guests