Pico W wifi scan with FreeRTOS

July 6, 2024, 16:28

kaisers

I'm trying to setup wifi scanning similar to the existing wifi scan example on the official repo (https://github.com/raspberrypi/pico-examples/tree/master/pico_w/wifi/wifi_scan). I've written some almost identical code to the functions written in the repo and the cyw43_wifi_scan function returns -4 whenever I call it. I can't find any documentation on this function regarding its error codes so I have no idea what that means. I suspect this issue is caused by the fact that I'm using the pico_cyw43_arch_lwip_sys_freertos library instead of the pico_cyw43_arch_lwip_threadsafe_background or pico_cyw43_arch_lwip_poll libraries used in the example. I need to use freeRTOS since using sockets without it is almost impossible so I'm kind of lost on how to get the cyw43_wifi_scan function to work with FreeRTOS. Has anyone else managed to achieve wifi scanning with the Pico W on FreeRTOS? Anyone have any suggestions on what to do? I've attached the code I'm running for those interested below (just prints "Failed to start scan: -4" repeatedly).
c++
int scanResult(void env, const cyw43_ev_scan_result_t result){
    if (result) {
        printf("ssid: %-32s rssi: %4d chan: %3d mac: %02x:%02x:%02x:%02x:%02x:%02x sec: %u\n",
            result->ssid, result->rssi, result->channel,
            result->bssid[0], result->bssid[1], result->bssid[2], result->bssid[3], result->bssid[4], result->bssid[5],
            result->auth_mode);
    }
    return 0;
}


bool WifiHelper::autoScan(){
    if (cyw43_arch_init()) {
        printf("failed to initialise\n");
        return 1;
    }
    cyw43_arch_enable_sta_mode();
    absolute_time_t scan_time = nil_time;
    bool scan_in_progress = false;

    while(true) {
        if (absolute_time_diff_us(get_absolute_time(), scan_time) < 0) {
            if (!scan_in_progress) {

                cyw43_wifi_scan_options_t scan_options = {0};
                int err = cyw43_wifi_scan(&cyw43_state, &scan_options, NULL, scanResult);
                
                if (err == 0) {
                    printf("\nPerforming wifi scan\n");
                    scan_in_progress = true;
                } else {
                    printf("Failed to start scan: %d\n", err);
                    scan_time = make_timeout_time_ms(10000); // wait 10s and scan again
                }
            } else if (!cyw43_wifi_scan_active(&cyw43_state)) {
                scan_time = make_timeout_time_ms(10000); // wait 10s and scan again
                scan_in_progress = false; 
            }
        }
    }
}

oops.se

You should use backtick ` not ' to mark code/cli. And plz don't post pictures of text, post text as text and read https://discord.com/channels/818384379197784084/1189386838788542464 before posting.