Search code examples
wifiesp32

ESP32 Doesn't see Wifi network while scanning?


I am trying to use ESP32 to scan networks. While my code running on ESP32-Wrover-kit works without problem and returns lots of networks, same code od ESP32 Wrover-IB doesn't do that, it only returns one.

What would be the reason that old ESP32 on kit works and new one doesn't.

New ESP32 datasheet: https://hr.mouser.com/datasheet/2/891/esp32-wrover-b_datasheet_en-1384674.pdf

ESP32 Wrover kit: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/hw-reference/esp32/get-started-wrover-kit.html

#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_log.h"
#include "esp_event.h"
#include "nvs_flash.h"

/* UART */
#include "driver/uart.h"
#include "driver/gpio.h"
#define BUF_SIZE (1024)
/* UART */

#define DEFAULT_SCAN_LIST_SIZE CONFIG_EXAMPLE_SCAN_LIST_SIZE

#define ECHO_TEST_TXD  (GPIO_NUM_4)
#define ECHO_TEST_RXD  (GPIO_NUM_5)
#define ECHO_TEST_RTS  (UART_PIN_NO_CHANGE)
#define ECHO_TEST_CTS  (UART_PIN_NO_CHANGE)


static const char *TAG = "scan";


void uart_init() {
    const uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .source_clk = UART_SCLK_APB,
    };
    uart_param_config(UART_NUM_1, &uart_config);

    uart_set_pin(UART_NUM_1, GPIO_NUM_25, GPIO_NUM_27, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);

    uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL, 0);
}

int sendData(const char* logName, const char* data)
{
    const int len = strlen(data);
    const int txBytes = uart_write_bytes(UART_NUM_1, data, len);
    //ESP_LOGI(logName, "Wrote %d bytes", txBytes);
    return txBytes;
}


/* Initialize Wi-Fi as sta and set scan method */
static void wifi_scan(void)
{

    char buffer[64];

    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());
    esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
    assert(sta_netif);

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));

    uint16_t number = DEFAULT_SCAN_LIST_SIZE;
    wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
    uint16_t ap_count = 0;
    memset(ap_info, 0, sizeof(ap_info));

    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    ESP_ERROR_CHECK(esp_wifi_start());

    while(1){
        ESP_ERROR_CHECK(esp_wifi_scan_start(NULL, true));
        ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
        ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));

        ESP_LOGI(TAG, "Total APs scanned = %u", ap_count);
        ESP_LOGI(TAG, "-------------------------------");
        for (int i = 0; (i < DEFAULT_SCAN_LIST_SIZE) && (i < ap_count); i++) {

            ESP_LOGI(TAG, "{\"macAddress\":\"%x:%x:%x:%x:%x:%x\",\"signalStrength\": %d}",ap_info[i].bssid[0],
                                                    ap_info[i].bssid[1],            
                                                    ap_info[i].bssid[2],
                                                    ap_info[i].bssid[3],
                                                    ap_info[i].bssid[4],
                                                    ap_info[i].bssid[5],
                                                    ap_info[i].rssi);
        }
        ESP_LOGI(TAG, "-------------------------------");

        vTaskDelay(4000 / portTICK_PERIOD_MS);
    }


}

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


    uart_init();
    wifi_scan();

}

Solution

  • External antenna on this exact module is required, adding antenna fixed the problem.

    enter image description here