mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-04-19 06:23:44 +02:00
Improvements and refactors after CR, add option to modify mdns name and simplify setup tool
This commit is contained in:
@@ -1,32 +1,38 @@
|
||||
#include "WiFiScanner.hpp"
|
||||
#include <cstring>
|
||||
|
||||
static const char *TAG = "WiFiScanner";
|
||||
|
||||
WiFiScanner::WiFiScanner() {}
|
||||
|
||||
void WiFiScanner::scanResultCallback(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data) {
|
||||
auto* scanner = static_cast<WiFiScanner*>(arg);
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_SCAN_DONE) {
|
||||
void WiFiScanner::scanResultCallback(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data)
|
||||
{
|
||||
auto *scanner = static_cast<WiFiScanner *>(arg);
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_SCAN_DONE)
|
||||
{
|
||||
uint16_t ap_count = 0;
|
||||
esp_wifi_scan_get_ap_num(&ap_count);
|
||||
|
||||
if (ap_count == 0) {
|
||||
|
||||
if (ap_count == 0)
|
||||
{
|
||||
ESP_LOGI(TAG, "No access points found");
|
||||
return;
|
||||
}
|
||||
|
||||
wifi_ap_record_t* ap_records = new wifi_ap_record_t[ap_count];
|
||||
wifi_ap_record_t *ap_records = new wifi_ap_record_t[ap_count];
|
||||
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, ap_records));
|
||||
|
||||
scanner->networks.clear();
|
||||
for (uint16_t i = 0; i < ap_count; i++) {
|
||||
for (uint16_t i = 0; i < ap_count; i++)
|
||||
{
|
||||
WiFiNetwork network;
|
||||
network.ssid = std::string(reinterpret_cast<char*>(ap_records[i].ssid));
|
||||
network.ssid = std::string(reinterpret_cast<char *>(ap_records[i].ssid));
|
||||
network.channel = ap_records[i].primary;
|
||||
network.rssi = ap_records[i].rssi;
|
||||
memcpy(network.mac, ap_records[i].bssid, 6);
|
||||
network.auth_mode = ap_records[i].authmode;
|
||||
|
||||
|
||||
scanner->networks.push_back(network);
|
||||
}
|
||||
|
||||
@@ -35,81 +41,88 @@ void WiFiScanner::scanResultCallback(void* arg, esp_event_base_t event_base,
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<WiFiNetwork> WiFiScanner::scanNetworks() {
|
||||
// todo this is garbage
|
||||
std::vector<WiFiNetwork> WiFiScanner::scanNetworks()
|
||||
{
|
||||
std::vector<WiFiNetwork> scan_results;
|
||||
|
||||
|
||||
// Check if WiFi is initialized
|
||||
wifi_mode_t mode;
|
||||
esp_err_t err = esp_wifi_get_mode(&mode);
|
||||
if (err == ESP_ERR_WIFI_NOT_INIT) {
|
||||
if (err == ESP_ERR_WIFI_NOT_INIT)
|
||||
{
|
||||
ESP_LOGE(TAG, "WiFi not initialized");
|
||||
return scan_results;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Give WiFi more time to be ready
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
|
||||
|
||||
// Stop any ongoing scan
|
||||
esp_wifi_scan_stop();
|
||||
|
||||
|
||||
// Try sequential channel scanning as a workaround
|
||||
bool try_sequential_scan = true; // Enable sequential scan
|
||||
|
||||
if (!try_sequential_scan) {
|
||||
bool try_sequential_scan = true; // Enable sequential scan
|
||||
|
||||
if (!try_sequential_scan)
|
||||
{
|
||||
// Normal all-channel scan
|
||||
wifi_scan_config_t scan_config = {
|
||||
.ssid = nullptr,
|
||||
.bssid = nullptr,
|
||||
.channel = 0, // 0 means scan all channels
|
||||
.channel = 0, // 0 means scan all channels
|
||||
.show_hidden = true,
|
||||
.scan_type = WIFI_SCAN_TYPE_ACTIVE, // Active scan
|
||||
.scan_type = WIFI_SCAN_TYPE_ACTIVE, // Active scan
|
||||
.scan_time = {
|
||||
.active = {
|
||||
.min = 120, // Min per channel
|
||||
.max = 300 // Max per channel
|
||||
.min = 120, // Min per channel
|
||||
.max = 300 // Max per channel
|
||||
},
|
||||
.passive = 360
|
||||
},
|
||||
.home_chan_dwell_time = 0, // 0 for default
|
||||
.channel_bitmap = 0 // 0 for all channels
|
||||
.passive = 360},
|
||||
.home_chan_dwell_time = 0, // 0 for default
|
||||
.channel_bitmap = 0 // 0 for all channels
|
||||
};
|
||||
|
||||
err = esp_wifi_scan_start(&scan_config, false);
|
||||
if (err != ESP_OK) {
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to start scan: %s", esp_err_to_name(err));
|
||||
return scan_results;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
// Sequential channel scan - scan each channel individually
|
||||
std::vector<wifi_ap_record_t> all_records;
|
||||
|
||||
for (uint8_t ch = 1; ch <= 13; ch++) {
|
||||
|
||||
for (uint8_t ch = 1; ch <= 13; ch++)
|
||||
{
|
||||
wifi_scan_config_t scan_config = {
|
||||
.ssid = nullptr,
|
||||
.bssid = nullptr,
|
||||
.channel = ch, // Specific channel
|
||||
.channel = ch,
|
||||
.show_hidden = true,
|
||||
.scan_type = WIFI_SCAN_TYPE_ACTIVE,
|
||||
.scan_time = {
|
||||
.active = {
|
||||
.min = 100,
|
||||
.max = 200
|
||||
},
|
||||
.passive = 300
|
||||
},
|
||||
.max = 200},
|
||||
.passive = 300},
|
||||
.home_chan_dwell_time = 0,
|
||||
.channel_bitmap = 0
|
||||
};
|
||||
|
||||
.channel_bitmap = 0};
|
||||
|
||||
err = esp_wifi_scan_start(&scan_config, true); // Blocking scan
|
||||
if (err == ESP_OK) {
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
uint16_t ch_count = 0;
|
||||
esp_wifi_scan_get_ap_num(&ch_count);
|
||||
if (ch_count > 0) {
|
||||
wifi_ap_record_t* ch_records = new wifi_ap_record_t[ch_count];
|
||||
if (esp_wifi_scan_get_ap_records(&ch_count, ch_records) == ESP_OK) {
|
||||
for (uint16_t i = 0; i < ch_count; i++) {
|
||||
if (ch_count > 0)
|
||||
{
|
||||
wifi_ap_record_t *ch_records = new wifi_ap_record_t[ch_count];
|
||||
if (esp_wifi_scan_get_ap_records(&ch_count, ch_records) == ESP_OK)
|
||||
{
|
||||
for (uint16_t i = 0; i < ch_count; i++)
|
||||
{
|
||||
all_records.push_back(ch_records[i]);
|
||||
}
|
||||
}
|
||||
@@ -118,59 +131,66 @@ std::vector<WiFiNetwork> WiFiScanner::scanNetworks() {
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
}
|
||||
|
||||
|
||||
// Process all collected records
|
||||
for (const auto& record : all_records) {
|
||||
for (const auto &record : all_records)
|
||||
{
|
||||
WiFiNetwork network;
|
||||
network.ssid = std::string(reinterpret_cast<const char*>(record.ssid));
|
||||
network.ssid = std::string(reinterpret_cast<const char *>(record.ssid));
|
||||
network.channel = record.primary;
|
||||
network.rssi = record.rssi;
|
||||
memcpy(network.mac, record.bssid, 6);
|
||||
network.auth_mode = record.authmode;
|
||||
scan_results.push_back(network);
|
||||
}
|
||||
|
||||
|
||||
// Skip the normal result processing
|
||||
return scan_results;
|
||||
}
|
||||
|
||||
|
||||
// Wait for scan completion with timeout
|
||||
int timeout_ms = 15000; // 15 second timeout
|
||||
int elapsed_ms = 0;
|
||||
|
||||
while (elapsed_ms < timeout_ms) {
|
||||
|
||||
while (elapsed_ms < timeout_ms)
|
||||
{
|
||||
uint16_t temp_count = 0;
|
||||
esp_err_t count_err = esp_wifi_scan_get_ap_num(&temp_count);
|
||||
|
||||
if (count_err == ESP_OK) {
|
||||
|
||||
if (count_err == ESP_OK)
|
||||
{
|
||||
// Wait a bit longer after finding networks to ensure scan is complete
|
||||
if (temp_count > 0 && elapsed_ms > 5000) {
|
||||
if (temp_count > 0 && elapsed_ms > 5000)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(200));
|
||||
elapsed_ms += 200;
|
||||
}
|
||||
|
||||
if (elapsed_ms >= timeout_ms) {
|
||||
|
||||
if (elapsed_ms >= timeout_ms)
|
||||
{
|
||||
ESP_LOGE(TAG, "Scan timeout after %d ms", timeout_ms);
|
||||
esp_wifi_scan_stop();
|
||||
return scan_results;
|
||||
}
|
||||
|
||||
|
||||
// Get scan results
|
||||
uint16_t ap_count = 0;
|
||||
esp_wifi_scan_get_ap_num(&ap_count);
|
||||
|
||||
if (ap_count == 0) {
|
||||
|
||||
if (ap_count == 0)
|
||||
{
|
||||
ESP_LOGI(TAG, "No access points found");
|
||||
return scan_results;
|
||||
}
|
||||
|
||||
wifi_ap_record_t* ap_records = new wifi_ap_record_t[ap_count];
|
||||
wifi_ap_record_t *ap_records = new wifi_ap_record_t[ap_count];
|
||||
err = esp_wifi_scan_get_ap_records(&ap_count, ap_records);
|
||||
if (err != ESP_OK) {
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to get scan records: %s", esp_err_to_name(err));
|
||||
delete[] ap_records;
|
||||
return scan_results;
|
||||
@@ -178,28 +198,26 @@ std::vector<WiFiNetwork> WiFiScanner::scanNetworks() {
|
||||
|
||||
// Build the results vector and track channels found
|
||||
bool channels_found[15] = {false}; // Track channels 0-14
|
||||
|
||||
for (uint16_t i = 0; i < ap_count; i++) {
|
||||
|
||||
for (uint16_t i = 0; i < ap_count; i++)
|
||||
{
|
||||
WiFiNetwork network;
|
||||
network.ssid = std::string(reinterpret_cast<char*>(ap_records[i].ssid));
|
||||
network.ssid = std::string(reinterpret_cast<char *>(ap_records[i].ssid));
|
||||
network.channel = ap_records[i].primary;
|
||||
network.rssi = ap_records[i].rssi;
|
||||
memcpy(network.mac, ap_records[i].bssid, 6);
|
||||
network.auth_mode = ap_records[i].authmode;
|
||||
|
||||
if (network.channel <= 14) {
|
||||
|
||||
if (network.channel <= 14)
|
||||
{
|
||||
channels_found[network.channel] = true;
|
||||
}
|
||||
|
||||
|
||||
scan_results.push_back(network);
|
||||
}
|
||||
|
||||
|
||||
delete[] ap_records;
|
||||
ESP_LOGI(TAG, "Found %d access points", ap_count);
|
||||
|
||||
// Also update the member variable for compatibility
|
||||
networks = scan_results;
|
||||
|
||||
|
||||
return scan_results;
|
||||
}
|
||||
@@ -7,7 +7,8 @@
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
struct WiFiNetwork {
|
||||
struct WiFiNetwork
|
||||
{
|
||||
std::string ssid;
|
||||
uint8_t channel;
|
||||
int8_t rssi;
|
||||
@@ -15,14 +16,14 @@ struct WiFiNetwork {
|
||||
wifi_auth_mode_t auth_mode;
|
||||
};
|
||||
|
||||
class WiFiScanner {
|
||||
class WiFiScanner
|
||||
{
|
||||
public:
|
||||
WiFiScanner();
|
||||
std::vector<WiFiNetwork> scanNetworks();
|
||||
static void scanResultCallback(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
|
||||
static void scanResultCallback(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
||||
|
||||
private:
|
||||
static constexpr char const* TAG = "WiFiScanner";
|
||||
std::vector<WiFiNetwork> networks;
|
||||
};
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
static auto WIFI_MANAGER_TAG = "[WIFI_MANAGER]";
|
||||
|
||||
// Define the global variables declared as extern in the header
|
||||
int s_retry_num = 0;
|
||||
EventGroupHandle_t s_wifi_event_group;
|
||||
|
||||
@@ -19,9 +18,9 @@ void WiFiManagerHelpers::event_handler(void *arg, esp_event_base_t event_base,
|
||||
}
|
||||
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
|
||||
{
|
||||
wifi_event_sta_disconnected_t* disconnected = (wifi_event_sta_disconnected_t*) event_data;
|
||||
const auto *disconnected = static_cast<wifi_event_sta_disconnected_t *>(event_data);
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Disconnect reason: %d", disconnected->reason);
|
||||
|
||||
|
||||
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY)
|
||||
{
|
||||
esp_wifi_connect();
|
||||
@@ -51,55 +50,52 @@ void WiFiManager::SetCredentials(const char *ssid, const char *password)
|
||||
{
|
||||
// Clear the config first
|
||||
memset(&_wifi_cfg, 0, sizeof(_wifi_cfg));
|
||||
|
||||
|
||||
// Copy SSID with null termination
|
||||
size_t ssid_len = std::min(strlen(ssid), sizeof(_wifi_cfg.sta.ssid) - 1);
|
||||
memcpy(_wifi_cfg.sta.ssid, ssid, ssid_len);
|
||||
_wifi_cfg.sta.ssid[ssid_len] = '\0';
|
||||
|
||||
|
||||
// Copy password with null termination
|
||||
size_t pass_len = std::min(strlen(password), sizeof(_wifi_cfg.sta.password) - 1);
|
||||
memcpy(_wifi_cfg.sta.password, password, pass_len);
|
||||
_wifi_cfg.sta.password[pass_len] = '\0';
|
||||
|
||||
|
||||
// Set other required fields
|
||||
// Use open auth if no password, otherwise allow any WPA variant
|
||||
if (strlen(password) == 0) {
|
||||
if (strlen(password) == 0)
|
||||
{
|
||||
_wifi_cfg.sta.threshold.authmode = WIFI_AUTH_OPEN;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
// IMPORTANT: Set threshold to WEP to accept ANY security mode >= WEP
|
||||
// This allows WPA, WPA2, WPA3, etc. The driver will negotiate the highest common mode
|
||||
_wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WEP;
|
||||
}
|
||||
|
||||
|
||||
// CRITICAL: Disable PMF completely - this often causes handshake timeouts
|
||||
_wifi_cfg.sta.pmf_cfg.capable = false;
|
||||
_wifi_cfg.sta.pmf_cfg.required = false;
|
||||
|
||||
|
||||
// IMPORTANT: Set scan method to ALL channels
|
||||
_wifi_cfg.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
|
||||
_wifi_cfg.sta.bssid_set = 0; // Don't use specific BSSID
|
||||
_wifi_cfg.sta.channel = 0; // Scan all channels
|
||||
|
||||
_wifi_cfg.sta.bssid_set = 0; // Don't use specific BSSID
|
||||
_wifi_cfg.sta.channel = 0; // Scan all channels
|
||||
|
||||
// Additional settings that might help with compatibility
|
||||
_wifi_cfg.sta.listen_interval = 0; // Default listen interval
|
||||
_wifi_cfg.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL; // Connect to strongest signal
|
||||
|
||||
_wifi_cfg.sta.listen_interval = 0; // Default listen interval
|
||||
_wifi_cfg.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL; // Connect to strongest signal
|
||||
|
||||
// IMPORTANT: For WPA/WPA2 Personal networks
|
||||
_wifi_cfg.sta.threshold.rssi = -127; // Accept any signal strength
|
||||
_wifi_cfg.sta.sae_pwe_h2e = WPA3_SAE_PWE_UNSPECIFIED; // Let driver decide SAE mode
|
||||
|
||||
_wifi_cfg.sta.threshold.rssi = -127; // Accept any signal strength
|
||||
_wifi_cfg.sta.sae_pwe_h2e = WPA3_SAE_PWE_UNSPECIFIED; // Let driver decide SAE mode
|
||||
|
||||
// Log what we're trying to connect to with detailed debugging
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Setting credentials for SSID: '%s' (length: %d)", ssid, (int)strlen(ssid));
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Password: '%s' (length: %d)", password, (int)strlen(password));
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Auth mode: %d, PMF capable: %d",
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Auth mode: %d, PMF capable: %d",
|
||||
_wifi_cfg.sta.threshold.authmode, _wifi_cfg.sta.pmf_cfg.capable);
|
||||
|
||||
// Print hex dump of SSID to catch any hidden characters
|
||||
ESP_LOG_BUFFER_HEX_LEVEL(WIFI_MANAGER_TAG, _wifi_cfg.sta.ssid, strlen((char*)_wifi_cfg.sta.ssid), ESP_LOG_INFO);
|
||||
|
||||
// Print hex dump of password to catch any hidden characters
|
||||
ESP_LOG_BUFFER_HEX_LEVEL(WIFI_MANAGER_TAG, _wifi_cfg.sta.password, strlen((char*)_wifi_cfg.sta.password), ESP_LOG_INFO);
|
||||
}
|
||||
|
||||
void WiFiManager::ConnectWithHardcodedCredentials()
|
||||
@@ -164,11 +160,11 @@ void WiFiManager::ConnectWithStoredCredentials()
|
||||
// Stop WiFi once before the loop
|
||||
esp_wifi_stop();
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
|
||||
|
||||
// Ensure we're in STA mode
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
|
||||
for (const auto& network : networks)
|
||||
for (const auto &network : networks)
|
||||
{
|
||||
// Reset retry counter for each network attempt
|
||||
s_retry_num = 0;
|
||||
@@ -177,19 +173,14 @@ void WiFiManager::ConnectWithStoredCredentials()
|
||||
|
||||
// Update config without stopping WiFi again
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Attempting to connect to SSID: '%s'", network.ssid.c_str());
|
||||
|
||||
// Double-check the actual config being sent to WiFi driver
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Final SSID in config: '%s' (len: %d)",
|
||||
(char*)_wifi_cfg.sta.ssid, (int)strlen((char*)_wifi_cfg.sta.ssid));
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Final password in config: '%s' (len: %d)",
|
||||
(char*)_wifi_cfg.sta.password, (int)strlen((char*)_wifi_cfg.sta.password));
|
||||
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &_wifi_cfg));
|
||||
xQueueSend(this->eventQueue, &event, 10);
|
||||
|
||||
// Start WiFi if not already started
|
||||
esp_err_t start_err = esp_wifi_start();
|
||||
if (start_err != ESP_OK && start_err != ESP_ERR_WIFI_STATE) {
|
||||
if (start_err != ESP_OK && start_err != ESP_ERR_WIFI_STATE)
|
||||
{
|
||||
ESP_LOGE(WIFI_MANAGER_TAG, "Failed to start WiFi: %s", esp_err_to_name(start_err));
|
||||
continue;
|
||||
}
|
||||
@@ -214,7 +205,7 @@ void WiFiManager::ConnectWithStoredCredentials()
|
||||
}
|
||||
ESP_LOGE(WIFI_MANAGER_TAG, "Failed to connect to SSID:%s, trying next stored network",
|
||||
network.ssid.c_str());
|
||||
|
||||
|
||||
// Disconnect before trying next network
|
||||
esp_wifi_disconnect();
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
@@ -249,107 +240,117 @@ void WiFiManager::SetupAccessPoint()
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "AP started.");
|
||||
}
|
||||
|
||||
std::vector<WiFiNetwork> WiFiManager::ScanNetworks() {
|
||||
wifi_mode_t current_mode;
|
||||
esp_err_t err = esp_wifi_get_mode(¤t_mode);
|
||||
|
||||
if (err == ESP_ERR_WIFI_NOT_INIT) {
|
||||
ESP_LOGE(WIFI_MANAGER_TAG, "WiFi not initialized for scanning");
|
||||
return std::vector<WiFiNetwork>();
|
||||
std::vector<WiFiNetwork> WiFiManager::ScanNetworks()
|
||||
{
|
||||
wifi_mode_t current_mode;
|
||||
esp_err_t err = esp_wifi_get_mode(¤t_mode);
|
||||
|
||||
if (err == ESP_ERR_WIFI_NOT_INIT)
|
||||
{
|
||||
ESP_LOGE(WIFI_MANAGER_TAG, "WiFi not initialized for scanning");
|
||||
return std::vector<WiFiNetwork>();
|
||||
}
|
||||
|
||||
// If we're in AP-only mode, we need STA interface for scanning
|
||||
if (current_mode == WIFI_MODE_AP)
|
||||
{
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "AP mode detected, checking for STA interface");
|
||||
|
||||
// Check if STA netif already exists
|
||||
esp_netif_t *sta_netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
|
||||
bool sta_netif_exists = (sta_netif != nullptr);
|
||||
|
||||
if (!sta_netif_exists)
|
||||
{
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Creating STA interface for scanning");
|
||||
sta_netif = esp_netif_create_default_wifi_sta();
|
||||
}
|
||||
|
||||
// If we're in AP-only mode, we need STA interface for scanning
|
||||
if (current_mode == WIFI_MODE_AP) {
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "AP mode detected, checking for STA interface");
|
||||
|
||||
// Check if STA netif already exists
|
||||
esp_netif_t* sta_netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
|
||||
bool sta_netif_exists = (sta_netif != nullptr);
|
||||
|
||||
if (!sta_netif_exists) {
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Creating STA interface for scanning");
|
||||
sta_netif = esp_netif_create_default_wifi_sta();
|
||||
}
|
||||
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Switching to APSTA mode for scanning");
|
||||
err = esp_wifi_set_mode(WIFI_MODE_APSTA);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(WIFI_MANAGER_TAG, "Failed to set APSTA mode: %s", esp_err_to_name(err));
|
||||
if (!sta_netif_exists && sta_netif) {
|
||||
esp_netif_destroy(sta_netif);
|
||||
}
|
||||
return std::vector<WiFiNetwork>();
|
||||
}
|
||||
|
||||
// Configure STA with empty config to prevent auto-connect
|
||||
wifi_config_t empty_config = {};
|
||||
esp_wifi_set_config(WIFI_IF_STA, &empty_config);
|
||||
|
||||
// Ensure STA is disconnected and not trying to connect
|
||||
esp_wifi_disconnect();
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
|
||||
// Longer delay for mode to stabilize and enable all channels
|
||||
vTaskDelay(pdMS_TO_TICKS(1500));
|
||||
|
||||
// Perform scan
|
||||
auto networks = wifiScanner->scanNetworks();
|
||||
|
||||
// Restore AP-only mode
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Restoring AP-only mode");
|
||||
esp_wifi_set_mode(WIFI_MODE_AP);
|
||||
|
||||
// Clean up STA interface if we created it
|
||||
if (!sta_netif_exists && sta_netif) {
|
||||
esp_netif_destroy(sta_netif);
|
||||
}
|
||||
|
||||
return networks;
|
||||
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Switching to APSTA mode for scanning");
|
||||
err = esp_wifi_set_mode(WIFI_MODE_APSTA);
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(WIFI_MANAGER_TAG, "Failed to set APSTA mode: %s", esp_err_to_name(err));
|
||||
if (!sta_netif_exists && sta_netif)
|
||||
{
|
||||
esp_netif_destroy(sta_netif);
|
||||
}
|
||||
return std::vector<WiFiNetwork>();
|
||||
}
|
||||
|
||||
// If already in STA or APSTA mode, scan directly
|
||||
return wifiScanner->scanNetworks();
|
||||
|
||||
// Configure STA with empty config to prevent auto-connect
|
||||
wifi_config_t empty_config = {};
|
||||
esp_wifi_set_config(WIFI_IF_STA, &empty_config);
|
||||
|
||||
// Ensure STA is disconnected and not trying to connect
|
||||
esp_wifi_disconnect();
|
||||
// Longer delay for mode to stabilize and enable all channels
|
||||
vTaskDelay(pdMS_TO_TICKS(2000));
|
||||
|
||||
// Perform scan
|
||||
auto networks = wifiScanner->scanNetworks();
|
||||
|
||||
// Restore AP-only mode
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Restoring AP-only mode");
|
||||
esp_wifi_set_mode(WIFI_MODE_AP);
|
||||
|
||||
// Clean up STA interface if we created it
|
||||
if (!sta_netif_exists && sta_netif)
|
||||
{
|
||||
esp_netif_destroy(sta_netif);
|
||||
}
|
||||
|
||||
return networks;
|
||||
}
|
||||
|
||||
// If already in STA or APSTA mode, scan directly
|
||||
return wifiScanner->scanNetworks();
|
||||
}
|
||||
|
||||
WiFiState_e WiFiManager::GetCurrentWiFiState() {
|
||||
return this->stateManager->GetWifiState();
|
||||
WiFiState_e WiFiManager::GetCurrentWiFiState()
|
||||
{
|
||||
return this->stateManager->GetWifiState();
|
||||
}
|
||||
|
||||
void WiFiManager::TryConnectToStoredNetworks() {
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Manual WiFi connection attempt requested");
|
||||
|
||||
// Check current WiFi mode
|
||||
wifi_mode_t current_mode;
|
||||
esp_err_t err = esp_wifi_get_mode(¤t_mode);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(WIFI_MANAGER_TAG, "Failed to get WiFi mode: %s", esp_err_to_name(err));
|
||||
return;
|
||||
void WiFiManager::TryConnectToStoredNetworks()
|
||||
{
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Manual WiFi connection attempt requested");
|
||||
|
||||
// Check current WiFi mode
|
||||
wifi_mode_t current_mode;
|
||||
esp_err_t err = esp_wifi_get_mode(¤t_mode);
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(WIFI_MANAGER_TAG, "Failed to get WiFi mode: %s", esp_err_to_name(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// If in AP mode, we need to properly transition to STA mode
|
||||
if (current_mode == WIFI_MODE_AP || current_mode == WIFI_MODE_APSTA)
|
||||
{
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Currently in AP mode, transitioning to STA mode");
|
||||
|
||||
// Stop WiFi first
|
||||
esp_wifi_stop();
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
|
||||
// Check if STA interface exists, create if needed
|
||||
esp_netif_t *sta_netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
|
||||
if (sta_netif == nullptr)
|
||||
{
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Creating STA interface");
|
||||
sta_netif = esp_netif_create_default_wifi_sta();
|
||||
}
|
||||
|
||||
// If in AP mode, we need to properly transition to STA mode
|
||||
if (current_mode == WIFI_MODE_AP || current_mode == WIFI_MODE_APSTA) {
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Currently in AP mode, transitioning to STA mode");
|
||||
|
||||
// Stop WiFi first
|
||||
esp_wifi_stop();
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
|
||||
// Check if STA interface exists, create if needed
|
||||
esp_netif_t* sta_netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
|
||||
if (sta_netif == nullptr) {
|
||||
ESP_LOGI(WIFI_MANAGER_TAG, "Creating STA interface");
|
||||
sta_netif = esp_netif_create_default_wifi_sta();
|
||||
}
|
||||
|
||||
// Set to STA mode
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
|
||||
// Reset retry counter and clear all event bits
|
||||
s_retry_num = 0;
|
||||
xEventGroupClearBits(s_wifi_event_group, WIFI_FAIL_BIT | WIFI_CONNECTED_BIT);
|
||||
this->ConnectWithStoredCredentials();
|
||||
|
||||
// Set to STA mode
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
|
||||
// Reset retry counter and clear all event bits
|
||||
s_retry_num = 0;
|
||||
xEventGroupClearBits(s_wifi_event_group, WIFI_FAIL_BIT | WIFI_CONNECTED_BIT);
|
||||
this->ConnectWithStoredCredentials();
|
||||
}
|
||||
|
||||
void WiFiManager::Begin()
|
||||
@@ -363,18 +364,6 @@ void WiFiManager::Begin()
|
||||
wifi_init_config_t esp_wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&esp_wifi_init_config));
|
||||
|
||||
// Set WiFi country to enable all channels (1-14)
|
||||
wifi_country_t country_config = {
|
||||
.cc = "JP", // Japan allows channels 1-14 (most permissive)
|
||||
.schan = 1,
|
||||
.nchan = 14,
|
||||
.max_tx_power = 20,
|
||||
.policy = WIFI_COUNTRY_POLICY_AUTO
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_wifi_set_country(&country_config));
|
||||
|
||||
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
&WiFiManagerHelpers::event_handler,
|
||||
@@ -387,8 +376,8 @@ void WiFiManager::Begin()
|
||||
&instance_got_ip));
|
||||
|
||||
_wifi_cfg = {};
|
||||
_wifi_cfg.sta.threshold.authmode = WIFI_AUTH_OPEN; // Start with open, will be set properly by SetCredentials
|
||||
_wifi_cfg.sta.pmf_cfg.capable = false; // Disable PMF by default
|
||||
_wifi_cfg.sta.threshold.authmode = WIFI_AUTH_OPEN; // Start with open, will be set properly by SetCredentials
|
||||
_wifi_cfg.sta.pmf_cfg.capable = false; // Disable PMF by default
|
||||
_wifi_cfg.sta.pmf_cfg.required = false;
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
|
||||
@@ -19,9 +19,6 @@
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
extern int s_retry_num;
|
||||
extern EventGroupHandle_t s_wifi_event_group;
|
||||
|
||||
namespace WiFiManagerHelpers
|
||||
{
|
||||
void event_handler(void *arg, esp_event_base_t event_base,
|
||||
|
||||
Reference in New Issue
Block a user