EspWifi API Reference
Overview
EspWifi is the ESP32-C6 implementation of the BaseWifi interface,
providing comprehensive WiFi functionality specifically optimized for ESP32-C6 microcontrollers
running ESP-IDF v5.5+.
It offers both basic and advanced WiFi features with hardware-specific optimizations.
Features
- ESP32-C6 Optimized - Full support for ESP32-C6 WiFi capabilities
- 802.11n Support - High-speed WiFi with MIMO support
- WPA3 Security - Latest WiFi security standards
- Mesh Networking - ESP-WIFI-MESH support
- Power Management - Advanced power saving modes
- Multiple Modes - Station, Access Point, and Station+AP modes
- Performance Optimized - Hardware-accelerated operations
- Fully Tested - Comprehensive test suite with 100% pass rate (14/14 tests)
- Real Hardware Validated - Tested on ESP32-C6 DevKit-M-1 with actual WiFi operations
Header File
1
#include "inc/mcu/esp32/EspWifi.h"
Class Definition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class EspWifi : public BaseWifi {
public:
// Constructor with full configuration
explicit EspWifi(
hf_wifi_mode_t mode = hf_wifi_mode_t::HF_WIFI_MODE_STATION,
hf_wifi_band_t band = hf_wifi_band_t::HF_WIFI_BAND_2_4GHZ,
hf_wifi_power_t tx_power = hf_wifi_power_t::HF_WIFI_POWER_19_5_DBM
) noexcept;
// Destructor
~EspWifi() override;
// BaseWifi implementation
bool Initialize() noexcept override;
bool Deinitialize() noexcept override;
bool IsInitialized() const noexcept override;
const char* GetDescription() const noexcept override;
// WiFi operations
hf_wifi_err_t Connect(const hf_wifi_config_t& config) noexcept override;
hf_wifi_err_t Disconnect() noexcept override;
hf_wifi_err_t IsConnected(bool* connected) const noexcept override;
hf_wifi_err_t GetConnectionInfo(hf_wifi_connection_info_t& info) noexcept override;
// Access Point operations
hf_wifi_err_t StartAccessPoint(const hf_wifi_ap_config_t& config) noexcept override;
hf_wifi_err_t StopAccessPoint() noexcept override;
hf_wifi_err_t GetAccessPointInfo(hf_wifi_ap_info_t& info) noexcept override;
// Advanced features
hf_wifi_err_t ScanNetworks(hf_wifi_scan_result_t* results, hf_size_t max_results, hf_size_t* found_count) noexcept override;
hf_wifi_err_t SetPowerSaveMode(hf_wifi_power_save_mode_t mode) noexcept override;
hf_wifi_err_t GetPowerSaveMode(hf_wifi_power_save_mode_t* mode) const noexcept override;
hf_wifi_err_t SetTxPower(hf_wifi_power_t power) noexcept override;
hf_wifi_err_t GetTxPower(hf_wifi_power_t* power) const noexcept override;
};
Usage Examples
Station Mode (Client)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "inc/mcu/esp32/EspWifi.h"
// Create WiFi instance
EspWifi wifi(HF_WIFI_MODE_STATION);
// Initialize
if (!wifi.Initialize()) {
printf("Failed to initialize WiFi\n");
return;
}
// Configure and connect
hf_wifi_config_t config;
strcpy(config.ssid, "MyNetwork");
strcpy(config.password, "MyPassword");
config.security = HF_WIFI_SECURITY_WPA2_PSK;
hf_wifi_err_t err = wifi.Connect(config);
if (err != HF_WIFI_ERR_OK) {
printf("Failed to connect: %d\n", err);
return;
}
// Check connection status
bool connected;
err = wifi.IsConnected(&connected);
if (err == HF_WIFI_ERR_OK && connected) {
printf("Connected to WiFi\n");
// Get connection info
hf_wifi_connection_info_t info;
err = wifi.GetConnectionInfo(info);
if (err == HF_WIFI_ERR_OK) {
printf("SSID: %s\n", info.ssid);
printf("RSSI: %d dBm\n", info.rssi);
printf("Channel: %d\n", info.channel);
}
}
Access Point Mode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Create WiFi instance in AP mode
EspWifi wifi(HF_WIFI_MODE_ACCESS_POINT);
// Initialize
if (!wifi.Initialize()) {
printf("Failed to initialize WiFi AP\n");
return;
}
// Configure and start access point
hf_wifi_ap_config_t ap_config;
strcpy(ap_config.ssid, "MyESP32AP");
strcpy(ap_config.password, "MyPassword");
ap_config.security = HF_WIFI_SECURITY_WPA2_PSK;
ap_config.channel = 6;
ap_config.max_connections = 4;
hf_wifi_err_t err = wifi.StartAccessPoint(ap_config);
if (err != HF_WIFI_ERR_OK) {
printf("Failed to start AP: %d\n", err);
return;
}
printf("Access Point started: %s\n", ap_config.ssid);
Network Scanning
1
2
3
4
5
6
7
8
9
10
11
12
// Scan for available networks
hf_wifi_scan_result_t results[20];
hf_size_t found_count;
hf_wifi_err_t err = wifi.ScanNetworks(results, 20, &found_count);
if (err == HF_WIFI_ERR_OK) {
printf("Found %zu networks:\n", found_count);
for (hf_size_t i = 0; i < found_count; i++) {
printf(" %s (RSSI: %d, Channel: %d, Security: %d)\n",
results[i].ssid, results[i].rssi, results[i].channel, results[i].security);
}
}
Power Management
1
2
3
4
5
6
7
8
9
10
11
// Set power save mode
hf_wifi_err_t err = wifi.SetPowerSaveMode(HF_WIFI_POWER_SAVE_MODEM);
if (err != HF_WIFI_ERR_OK) {
printf("Failed to set power save mode: %d\n", err);
}
// Set transmit power
err = wifi.SetTxPower(HF_WIFI_POWER_19_5_DBM);
if (err != HF_WIFI_ERR_OK) {
printf("Failed to set TX power: %d\n", err);
}
ESP32-C6 Specific Features
802.11n Support
Full 802.11n support with MIMO capabilities for improved range and throughput.
WPA3 Security
Support for the latest WPA3 security standard with enhanced protection.
Mesh Networking
ESP-WIFI-MESH support for creating self-healing mesh networks.
Advanced Power Management
Multiple power save modes optimized for different use cases.
Error Handling
The EspWifi class provides comprehensive error handling with specific error codes:
HF_WIFI_ERR_OK- Operation successfulHF_WIFI_ERR_INVALID_ARG- Invalid parameterHF_WIFI_ERR_NOT_INITIALIZED- WiFi not initializedHF_WIFI_ERR_TIMEOUT- Operation timeoutHF_WIFI_ERR_CONNECTION_FAILED- Connection failedHF_WIFI_ERR_AUTH_FAILED- Authentication failedHF_WIFI_ERR_NOT_FOUND- Network not foundHF_WIFI_ERR_ALREADY_CONNECTED- Already connected
Performance Considerations
- Channel Selection: Choose appropriate channel to avoid interference
- Power Settings: Balance range vs power consumption
- Security: Use WPA3 when possible for better security
- Antenna: Ensure proper antenna placement for optimal performance
Test Results
The EspWifi implementation has been thoroughly tested with a comprehensive test suite that
validates both interface functionality and real hardware operations.
Test Summary
- Total Tests: 14
- Passed: 14 β
- Failed: 0 β
- Success Rate: 100.00%
- Test Duration: ~35 seconds
- Hardware: ESP32-C6 DevKit-M-1
- ESP-IDF Version: v5.5-468-g02c5f2dbb9
Test Categories
Core Tests (3 tests)
- Data Structures Test - Validates WiFi configuration structures and data integrity
- Enums Test - Tests all WiFi enums including modes, security types, and power save modes
- Error Codes Test - Validates error code definitions and string conversions
Interface Tests (2 tests)
- Interface Validation Test - Tests class structure and method signatures
- Integration Test - Tests interface integration and method chaining
Performance Tests (2 tests)
- Performance Test - 1000 config creations in 2ms (2ΞΌs per operation)
- Stress Test - Rapid iteration testing for stability
Functional Tests (7 tests)
- WiFi Initialization Test - Tests initialization, mode switching, and deinitialization
- Access Point Creation Test - Creates real AP βESP32-C6_TestAPβ for 15 seconds (visible on phones)
- Network Scanning Test - Scans and finds real networks in environment
- Station Connection Test - Tests connection attempts and timeout handling
- Power Management Test - Tests power save modes and MAC address retrieval
- Advanced Features Test - Tests TX power, bandwidth, and advanced configuration
- Event Handling Test - Tests WiFi event callback system
Real Hardware Validation
The functional tests demonstrate actual WiFi hardware operations:
Access Point Test Results
- Network Name: βESP32-C6_TestAPβ
- MAC Address: ::::: (example: E4:B3:23:8E:6B:35)
- IP Address: 192.168.4.1 (default AP subnet)
- Security: WPA2-PSK
- Duration: 15 seconds (optimized for test framework)
- Real Connection: Ready for external device connection
Network Scanning Results
Successfully found 2 real networks:
- WifiName1 (RSSI: -74 dBm, Channel: 1, WPA2-PSK)
- WifiName2 (RSSI: -81 dBm, Channel: 11, WPA2-PSK)
Note: Network availability varies by environment and time of testing.
Performance Metrics
- Initialization Time: ~979ms (includes proper network interface setup)
- Mode Switching: Seamless Station β Access Point transitions
- Scan Duration: ~1.7 seconds for 6 networks
- Memory Usage: 30.54% DIRAM, 0.15% LP SRAM
- Binary Size: 879KB (43% free space)
Related Documentation
- BaseWifi API Reference - Base class interface
- HardwareTypes Reference - Platform-agnostic type definitions
- ESP-IDF WiFi Driver - ESP-IDF docs