ESP32 扫描时没有看到 Wifi 网络?
ESP32 Doesn't see Wifi network while scanning?
我正在尝试使用 ESP32 扫描网络。虽然我在 ESP32-Wrover-kit 上的代码 运行 可以正常工作并且 returns 很多网络,但 ESP32 Wrover-IB 的相同代码不会那样做,它只有 returns 一个。
套件上的旧 ESP32 工作而新的不工作的原因是什么。
新的 ESP32 数据表:https://hr.mouser.com/datasheet/2/891/esp32-wrover-b_datasheet_en-1384674.pdf
ESP32 漫游者套件: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();
}
此模块需要外部天线,添加天线可解决问题。
我正在尝试使用 ESP32 扫描网络。虽然我在 ESP32-Wrover-kit 上的代码 运行 可以正常工作并且 returns 很多网络,但 ESP32 Wrover-IB 的相同代码不会那样做,它只有 returns 一个。
套件上的旧 ESP32 工作而新的不工作的原因是什么。
新的 ESP32 数据表:https://hr.mouser.com/datasheet/2/891/esp32-wrover-b_datasheet_en-1384674.pdf
ESP32 漫游者套件: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();
}
此模块需要外部天线,添加天线可解决问题。