The use of the ESP8266 in the world of IoT

User avatar
By crazyferajna
#6376 Hello everyone! I have written the code to get list of available networks, but it isn't working... The function wifi_station_scan always returns false. What am I doing wrong?

Code: Select all#include <espressif/esp_common.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <lwip/sockets.h>
#include <lwip/dns.h>
#include <lwip/netdb.h>
#include <udhcp/dhcpd.h>

void ICACHE_FLASH_ATTR scan_done_callback(void *arg, STATUS status)
{
    struct bss_info *bss = arg;

    bss = STAILQ_NEXT(bss, next); // ignore first

    while (bss)
    {
        printf("%s %d %d %d\n", bss->ssid, bss->channel, bss->rssi, bss->authmode);
        bss = STAILQ_NEXT(bss, next);
    }
}

void ICACHE_FLASH_ATTR scanner(void *pvParameters)
{
    printf("Starting scanning...");
    if (wifi_station_scan(NULL, scan_done_callback))
    {
        printf("OK!");
       
    }
    else
    {
        printf("Error...");
    }

    vTaskDelete(NULL);
}

void ICACHE_FLASH_ATTR user_init(void)
{
    wifi_set_opmode(STATION_MODE);
    wifi_station_set_auto_connect(FALSE);

    xTaskCreate(scanner, "scanner", 128, NULL, 2, NULL);
}
User avatar
By ofpaulus
#6684 In my opinion, you should use function system_init_done_cb from SDK\include\user_interface.h

Please try something like this.
Code: Select allvoid ICACHE_FLASH_ATTR init_done(void)
{
    xTaskCreate(scanner, "scanner", 128, NULL, 2, NULL);
}

void ICACHE_FLASH_ATTR user_init(void)
{
    wifi_set_opmode(STATION_MODE);
    wifi_station_set_auto_connect(FALSE);

    system_init_done_cb(init_done);
}
User avatar
By psychegr
#53721
crazyferajna wrote:Thank you for your reply. Unfortunately, in FreeRTOS version of the SDK this function is unavailable.


A bit late but i had the same issue as you. The code from the link will help you.
In sort you have to create a task and do the wifi scan in that task. For me it works great.

http://bbs.espressif.com/viewtopic.php?t=974