Wifi connect to same channel still scans all channels

homedad
Posts: 9
Joined: Sun Jan 23, 2022 2:53 am

Wifi connect to same channel still scans all channels

Postby homedad » Mon Jul 29, 2024 11:07 pm

Anybody understand why connecting takes so long and how I can skip the initial all-channels scan, even though I have a configured channel?

After waking up, I try to reconnect to the configured wifi access point in station mode, which starts a channel scan of all channels even though the channel is provided in the wifi_config_t structure.

Unnecessary channel scanning takes valuable time for scanning (2300 ms) while scanning just the expected channel would only take 120 ms. (SEE LOGS).

Then It configures channel 0, and then channel 1. This only takes 50ms so I can live with it.

Finally the configured channel is configured and the connection is attempted on channel 11. This only takes 60ms.

Time = current = battery life. Reducing 2400 ms to even 400 ms would give me back more than 20% of my battery capacity, which equates to many months before the batteries go into the trash.

Code: Select all

D (879) wifi:scan end: arg=0x0, status=0, ss_state=0x7
D (879) wifi:perform scan: ss_state=0xd, chan<1,0>, dur<0,120>
D (1009) wifi:scan end: arg=0x0, status=0, ss_state=0x7
D (1009) wifi:perform scan: ss_state=0xd, chan<2,0>, dur<0,120>
D (1129) wifi:scan end: arg=0x0, status=0, ss_state=0x7
D (1129) wifi:perform scan: ss_state=0xd, chan<3,0>, dur<0,120>
D (1249) wifi:scan end: arg=0x0, status=0, ss_state=0x7
D (1249) wifi:perform scan: ss_state=0xd, chan<4,0>, dur<0,120>
D (1369) wifi:scan end: arg=0x0, status=0, ss_state=0x7
D (1369) wifi:perform scan: ss_state=0xd, chan<5,0>, dur<0,120>
D (1489) wifi:scan end: arg=0x0, status=0, ss_state=0x7
D (1489) wifi:perform scan: ss_state=0xd, chan<6,0>, dur<0,120>
D (1609) wifi:scan end: arg=0x0, status=0, ss_state=0x7
D (1609) wifi:perform scan: ss_state=0xd, chan<7,0>, dur<0,120>
D (1739) wifi:scan end: arg=0x0, status=0, ss_state=0x7
D (1739) wifi:perform scan: ss_state=0xd, chan<8,0>, dur<0,120>
D (1859) wifi:scan end: arg=0x0, status=0, ss_state=0x7
D (1859) wifi:perform scan: ss_state=0xd, chan<9,0>, dur<0,120>
D (1979) wifi:scan end: arg=0x0, status=0, ss_state=0x7
D (1979) wifi:perform scan: ss_state=0xd, chan<10,0>, dur<0,120>
D (2099) wifi:scan end: arg=0x0, status=0, ss_state=0x7
D (2099) wifi:perform scan: ss_state=0xd, chan<12,0>, dur<360,360>
D (2459) wifi:scan end: arg=0x0, status=0, ss_state=0x7
D (2459) wifi:perform scan: ss_state=0xd, chan<13,0>, dur<360,360>
D (2819) wifi:scan end: arg=0x0, status=0, ss_state=0x7
D (2829) wifi:perform scan: ss_state=0xd, chan<14,0>, dur<360,360>
D (3189) wifi:scan end: arg=0x0, status=0, ss_state=0x7

D (3189) wifi:filter: set rx policy=4
D (3189) wifi:first chan=1
D (3189) wifi:handoff_cb: status=0
D (3189) wifi:ap found, mac=e2:db:d1:b5:84:ad
D (3189) wifi:going for connection with bssid=e2:db:d1:b5:84:ad
D (3199) wifi:new_bss=0x3fc9dba8, cur_bss=0x0, new_chan=<11,0>, cur_chan=1
D (3209) wifi:filter: set rx policy=5
I (3209) wifi:new:<11,0>, old:<1,0>, ap:<255,255>, sta:<11,0>, prof:1
D (3219) wifi:connect_op: status=0, auth=5, cipher=3 
D (3219) wifi:auth mode is not none
D (3219) wifi:connect_bss: auth=1, reconnect=0
I (3229) wifi:state: init -> auth (b0)
D (3229) wifi:start 1s AUTH timer
D (3229) wifi:clear scan ap list
I (3239) WIFI: Channel Change (state=2) from 1 to 11

D (3239) wifi:recv auth: seq=2, status=0
I (3249) wifi:state: auth -> assoc (0)
D (3249) wifi:restart connect 1s timer for assoc
D (3259) wifi:recv assoc: type=0x10
D (3259) wifi:filter: set rx policy=6
I (3259) wifi:state: assoc -> run (10)
D (3259) wifi:start 10s connect timer for 4 way handshake
I (3289) wifi:connected with XXXXXXXX, aid = 30, channel 11, BW20, bssid = e2:db:d1:b5:84:ad
I (3289) wifi:security: WPA2-PSK, phy: bgn, rssi: -50
D (3289) wifi:remove all except e2:db:d1:b5:84:ad from rc list
D (3289) wifi:clear blacklist
D (3299) wifi:filter: set rx policy=7
I (3299) wifi:pm start, type: 2

  // Code is super straight forward:

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
    ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_FLASH) );
    ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
    esp_wifi_set_ps( WIFI_PS_MAX_MODEM );
    ESP_ERROR_CHECK( esp_wifi_start() );

aliarifat794
Posts: 198
Joined: Sun Jun 23, 2024 6:18 pm

Re: Wifi connect to same channel still scans all channels

Postby aliarifat794 » Tue Jul 30, 2024 2:09 pm

You can use this code instead:

Code: Select all

#include "esp_wifi.h"
#include "esp_log.h"
#include "nvs_flash.h"

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);

    // Initialize WiFi
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
    ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));

    // Define WiFi configuration
    wifi_config_t wifi_config = {
        .sta = {
            .ssid = "your_ssid",
            .password = "your_password",
            .channel = 11, // Set to your AP's specific channel
            .threshold.authmode = WIFI_AUTH_WPA2_PSK, // Set to your AP's auth mode
        },
    };

    // Set WiFi configuration
    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
    ESP_ERROR_CHECK(esp_wifi_start());

    // Enable power save mode if needed
    esp_wifi_set_ps(WIFI_PS_MAX_MODEM);

    // Optionally, log the WiFi connection status
    ESP_LOGI("WIFI", "WiFi initialization finished.");
}

homedad
Posts: 9
Joined: Sun Jan 23, 2022 2:53 am

Re: Wifi connect to same channel still scans all channels

Postby homedad » Wed Jul 31, 2024 7:29 pm

Thanks, will try it.

Who is online

Users browsing this forum: thetitaniumtitan and 216 guests