uploaded 5 hours ago
ESP32 WiFi Configuration

readme

# ESP32 Wi-Fi Connect

This component helps with Wi-Fi connection for the device.

It first tries to connect to a Wi-Fi network using the credentials stored in the flash. If this fails, it starts an access point and a web server to allow the user to connect to a Wi-Fi network.

The URL to access the web server is `http://192.168.4.1`.

### Screenshot: Wi-Fi Configuration

<img src="assets/ap_v3.png" width="320" alt="Wi-Fi Configuration">

### Screenshot: Advanced Options

<img src="assets/ap_v3_advanced.png" width="320" alt="Advanced Configuration">

## Changelog: v3.0.0

- Added WifiManager class for unified WiFi connection management.
- Improved DnsServer and WifiConfigurationAp classes for better resource handling.
- Updated HTML for configuration success message to use exit endpoint instead of reboot.
- Enhanced error handling and state management in WifiStation.
- Cleaned up unused code and improved thread safety across components.

## Changelog: v2.6.0

- Add support for ESP32C5 5G mode.

## Changelog: v2.4.0

- Add ja / zh-TW languages.
- Add advanced tab.
- Add "Connection: close" headers to save open sockets.

## Changelog: v2.3.0

- Add support for language request.

## Changelog: v2.2.0

- Add support for ESP32 SmartConfig(ESPTouch v2)

## Changelog: v2.1.0

- Improve Wi-Fi connection logic.

## Changelog: v2.0.0

- Add support for multiple Wi-Fi SSID management.
- Auto switch to the best Wi-Fi network.
- Captive portal for Wi-Fi configuration.
- Support for multiple languages (English, Chinese).

## Configuration

The Wi-Fi credentials are stored in the flash under the "wifi" namespace.

The keys are "ssid", "ssid1", "ssid2" ... "ssid9", "password", "password1", "password2" ... "password9".

## Usage

```cpp
#include <wifi_manager.h>
#include <ssid_manager.h>

// Initialize the default event loop
ESP_ERROR_CHECK(esp_event_loop_create_default());

// Initialize NVS flash for Wi-Fi configuration
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);

// Get the WifiManager singleton
auto& wifi_manager = WifiManager::GetInstance();

// Initialize with configuration
WifiManagerConfig config;
config.ssid_prefix = "ESP32";  // AP mode SSID prefix
config.language = "zh-CN";     // Web UI language
wifi_manager.Initialize(config);

// Set event callback to handle WiFi events
wifi_manager.SetEventCallback([](WifiEvent event) {
    switch (event) {
        case WifiEvent::Scanning:
            ESP_LOGI("WiFi", "Scanning for networks...");
            break;
        case WifiEvent::Connecting:
            ESP_LOGI("WiFi", "Connecting to network...");
            break;
        case WifiEvent::Connected:
            ESP_LOGI("WiFi", "Connected successfully!");
            break;
        case WifiEvent::Disconnected:
            ESP_LOGW("WiFi", "Disconnected from network");
            break;
        case WifiEvent::ConfigModeEnter:
            ESP_LOGI("WiFi", "Entered config mode");
            break;
        case WifiEvent::ConfigModeExit:
            ESP_LOGI("WiFi", "Exited config mode");
            break;
    }
});

// Check if there are saved Wi-Fi credentials
auto& ssid_list = SsidManager::GetInstance().GetSsidList();
if (ssid_list.empty()) {
    // No credentials saved, start config AP mode
    wifi_manager.StartConfigAp();
} else {
    // Try to connect to the saved Wi-Fi network
    wifi_manager.StartStation();
}
```

Please check https://github.com/78/xiaozhi-esp32 for more usage.

## Publishing to IDF Component Registry

Để publish component này lên IDF Component Registry, thực hiện các bước sau:

### Chuẩn bị

1. Đảm bảo đã cài đặt `compote` (IDF Component Manager CLI):
```bash
pip install idf-component-manager
```

2. Đăng nhập vào IDF Component Registry:
```bash
compote registry login
```

### Publish Component

1. (Tùy chọn) Tạo component archive để kiểm tra:
```bash
compote component pack --name esp-wifi-connect
```
Archive sẽ được tạo trong thư mục `dist/`

2. Upload component lên registry:
```bash
compote component upload --namespace <your-namespace> --name esp-wifi-connect
```

Hoặc với namespace đã cấu hình sẵn:
```bash
compote component upload --name esp-wifi-connect
```

3. Kiểm tra trạng thái upload:
```bash
compote component upload-status
```

**Lưu ý quan trọng:**
- Xác nhận version trong file `idf_component.yml` đã được cập nhật đúng trước khi publish
- `--name` là bắt buộc cho cả pack và upload
- `--namespace` có thể được cấu hình trong config file hoặc truyền qua command line

### Lưu ý

- Mỗi version chỉ có thể publish một lần, không thể ghi đè
- Đảm bảo file `idf_component.yml` có đầy đủ thông tin: dependencies, description, license, repository, url và version
- Component sẽ được publish với định dạng: `<namespace>/esp-wifi-connect`

### Sử dụng Component đã Publish

Sau khi publish, người dùng có thể thêm component vào project của họ bằng cách thêm vào `idf_component.yml`:
```yaml
dependencies:
  <your-namespace>/esp-wifi-connect: "^3.0.0"
```

Hoặc sử dụng command:
```bash
idf.py add-dependency "<your-namespace>/esp-wifi-connect^3.0.0"
```

Links

Supports all targets

License: MIT

To add this component to your project, run:

idf.py add-dependency "min9802/esp-wifi-connect^3.0.2"

download archive

Stats

  • Archive size
    Archive size ~ 119.03 KB
  • Downloaded in total
    Downloaded in total 0 times
  • Downloaded this version
    This version: 0 times

Badge

min9802/esp-wifi-connect version: 3.0.2
|