bolar_1 date 2026-7-1 d:/proc
This commit is contained in:
1
components/esp-wifi-connect/.component_hash
Normal file
1
components/esp-wifi-connect/.component_hash
Normal file
@@ -0,0 +1 @@
|
||||
69b3280e3afc94547929a8cbeed20dd8f442c3569b1597865a600e4ea100731b
|
||||
1
components/esp-wifi-connect/.gitignore
vendored
Normal file
1
components/esp-wifi-connect/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
dist/
|
||||
21
components/esp-wifi-connect/CMakeLists.txt
Normal file
21
components/esp-wifi-connect/CMakeLists.txt
Normal file
@@ -0,0 +1,21 @@
|
||||
idf_component_register(
|
||||
SRCS
|
||||
"wifi_configuration_ap.cc"
|
||||
"wifi_station.cc"
|
||||
"ssid_manager.cc"
|
||||
"dns_server.cc"
|
||||
INCLUDE_DIRS
|
||||
"include"
|
||||
EMBED_TXTFILES
|
||||
"assets/wifi_configuration.html"
|
||||
"assets/wifi_configuration_done.html"
|
||||
"assets/server_cert.pem"
|
||||
"assets/server_key.pem"
|
||||
REQUIRES
|
||||
"esp_timer"
|
||||
"esp_https_server"
|
||||
"esp_http_server"
|
||||
"esp_wifi"
|
||||
"nvs_flash"
|
||||
"json"
|
||||
)
|
||||
79
components/esp-wifi-connect/README.md
Normal file
79
components/esp-wifi-connect/README.md
Normal file
@@ -0,0 +1,79 @@
|
||||
# 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: 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
|
||||
// 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 Wi-Fi configuration
|
||||
auto& ssid_list = SsidManager::GetInstance().GetSsidList();
|
||||
if (ssid_list.empty()) {
|
||||
// Start the Wi-Fi configuration AP
|
||||
auto& ap = WifiConfigurationAp::GetInstance();
|
||||
ap.SetSsidPrefix("ESP32");
|
||||
ap.Start();
|
||||
return;
|
||||
}
|
||||
|
||||
// Otherwise, connect to the Wi-Fi network
|
||||
WifiStation::GetInstance().Start();
|
||||
```
|
||||
|
||||
BIN
components/esp-wifi-connect/assets/ap.png
Normal file
BIN
components/esp-wifi-connect/assets/ap.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
BIN
components/esp-wifi-connect/assets/ap_v2.png
Normal file
BIN
components/esp-wifi-connect/assets/ap_v2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 40 KiB |
BIN
components/esp-wifi-connect/assets/ap_v3.png
Normal file
BIN
components/esp-wifi-connect/assets/ap_v3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 16 KiB |
BIN
components/esp-wifi-connect/assets/ap_v3_advanced.png
Normal file
BIN
components/esp-wifi-connect/assets/ap_v3_advanced.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
126
components/esp-wifi-connect/assets/server_cert.pem
Normal file
126
components/esp-wifi-connect/assets/server_cert.pem
Normal file
@@ -0,0 +1,126 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIGHTCCBQWgAwIBAgIRALRjadnk8DLow4yqTotOSsUwDQYJKoZIhvcNAQELBQAw
|
||||
XDELMAkGA1UEBhMCQ04xGjAYBgNVBAoTEVdvVHJ1cyBDQSBMaW1pdGVkMTEwLwYD
|
||||
VQQDDChXb1RydXMgRFYgU2VydmVyIENBICBbUnVuIGJ5IHRoZSBJc3N1ZXJdMB4X
|
||||
DTI1MDQxNjAwMDAwMFoXDTI2MDQxNjIzNTk1OVowFjEUMBIGA1UEAwwLKi5sa2Fs
|
||||
bC5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC3nurK+e83OkbK
|
||||
VpbQdaMZgVXgmv3driHRRjXDcLBW+qRhIMXEXr+8sZHWdQ4M1a9N2fj6dRKQH3tM
|
||||
1RtHq5fAe6kP/F6y0BQzG67RYGbSb1wC42G2mQrBbuleaBlU3FvSEf5r02hXDBO3
|
||||
IvIgnPaWiEY7zi7IwT1SJaFJDKBeQ13/IHfko/uJZDub4eB/sShe7ehQ6bBUr5WM
|
||||
9VePyMPdFIWi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C3BUrz9vHCv8S5dIa2LX1rzNLz
|
||||
Rt0vxuBqw8M0Ayx9lt1awg6nCpnBBYurDC/zXDrPbDdVCYfeU0BsWO/8tqtlbgT2
|
||||
G9w84FoVxp7Z8VlIMCFlA2zs6SFz7JsDoeA3raAVGI/6ugLOpyypEBMs1OUIJqsi
|
||||
l2D4kF501KKaU73yqWjgom7C12yxow+ev+to51byrvLjKzg6CYG1a4XXvi3tPxq3
|
||||
smPi9WIsgtRqAEFQ8TmDn5XpNpaYbg==
|
||||
-----END CERTIFICATE-----
|
||||
27
components/esp-wifi-connect/assets/server_key.pem
Normal file
27
components/esp-wifi-connect/assets/server_key.pem
Normal file
@@ -0,0 +1,27 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIEowIBAAKCAQEAt57qyvnvNzpGylaW0HWjGYFV4Jr93a4h0UY1w3CwVvqkYSDF
|
||||
xF6/vLGR1nUODNWvTdn4+nUSkB97TNUbR6uXwHupD/xestAUMxuu0WBm0m9cAuNh
|
||||
tpkKwW7pXmgZVNxb0hH+a9NoVwwTtyLyIJz2lohGO84uyME9UiWhSQygXkNd/yB3
|
||||
5KP7iWQ7m+Hgf7EoXu3oUOmwVK+VjPVXj8jD3RSFoklDkprcFOVlkXIuZX3POy+4
|
||||
FgyjWh7t1nnx76EZmYddE6g7cOoIJw1OxI+7kL+N/gjrgWbsZed6AGGp6f9TsTGp
|
||||
kXTMrmPhZQk7IIRatnvKa0/UI9WNC2YsTrNH7QIDAQABAoIBAEk2qSGjhDk+GEnH
|
||||
Or0FmrGpuyciRYTlUICUAQ3bWcRkjbRYj+JbcRH4r1CWPbLqZMqi1b5vfaZPlycs
|
||||
dYMfLRPHesJv+wZLJzK1n//F81mGo2ZXaXeYEAzOf2hwE2Gdt1LMb+hoU9sZaqcD
|
||||
3Q0Z6Mxeq7pe3JKZUiz16hgpW6wZfU5gnUDtIvxx4jN72b9c4pJtyxifH7SGfh1u
|
||||
eBi5xxBpOj6A6Y7OqxYRY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nFQhpc8WEnvIxa4e23VYXXJX
|
||||
-----END RSA PRIVATE KEY-----
|
||||
721
components/esp-wifi-connect/assets/wifi_configuration.html
Normal file
721
components/esp-wifi-connect/assets/wifi_configuration.html
Normal file
@@ -0,0 +1,721 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Network Configuration</title>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="Content-Security-Policy" content="referrer no-referrer">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
|
||||
<style type="text/css">
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background-color: #f0f0f0;
|
||||
}
|
||||
label {
|
||||
display: block;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
input {
|
||||
width: 100%;
|
||||
padding: 5px;
|
||||
box-sizing: border-box;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 3px;
|
||||
}
|
||||
input[type="submit"] {
|
||||
background-color: #007bff;
|
||||
color: #fff;
|
||||
border: none;
|
||||
border-radius: 3px;
|
||||
padding: 10px;
|
||||
cursor: pointer;
|
||||
}
|
||||
input[type="submit"]:hover {
|
||||
background-color: #0056b3;
|
||||
}
|
||||
input[type="submit"]:disabled {
|
||||
background-color: #ccc;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
#ap_list {
|
||||
margin-top: 20px;
|
||||
border-top: 1px solid #ccc;
|
||||
padding-top: 10px;
|
||||
}
|
||||
#ap_list .scanning {
|
||||
font-style: italic;
|
||||
color: #666;
|
||||
}
|
||||
#ap_list a {
|
||||
display: block;
|
||||
margin-top: 5px;
|
||||
color: #007bff;
|
||||
text-decoration: none;
|
||||
}
|
||||
#ap_list a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.language-switch {
|
||||
position: absolute;
|
||||
top: 20px;
|
||||
right: 20px;
|
||||
}
|
||||
.language-switch select {
|
||||
padding: 5px;
|
||||
border-radius: 3px;
|
||||
border: 1px solid #ccc;
|
||||
}
|
||||
|
||||
/* Tab styles */
|
||||
.tabs {
|
||||
display: flex;
|
||||
margin-top: 20px;
|
||||
margin-bottom: 0;
|
||||
padding-left: 20px;
|
||||
border-bottom: 1px solid #ccc;
|
||||
}
|
||||
.tab {
|
||||
padding: 10px 20px;
|
||||
cursor: pointer;
|
||||
border: 1px solid #ccc;
|
||||
border-bottom: none;
|
||||
border-radius: 5px 5px 0 0;
|
||||
margin-right: 5px;
|
||||
background-color: #f0f0f0;
|
||||
position: relative;
|
||||
top: 1px;
|
||||
}
|
||||
.tab.active {
|
||||
background-color: #fff;
|
||||
}
|
||||
.tab-content {
|
||||
display: none;
|
||||
border-top: none;
|
||||
padding: 20px;
|
||||
background-color: #fff;
|
||||
}
|
||||
.tab-content.active {
|
||||
display: block;
|
||||
}
|
||||
.tab[data-lang="advanced_tab"] {
|
||||
display: none;
|
||||
}
|
||||
#advanced-tab {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#remember_bssid, #sleep_mode {
|
||||
width: auto;
|
||||
}
|
||||
|
||||
/* Toast message styles */
|
||||
.toast {
|
||||
position: fixed;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
background-color: rgba(0, 0, 0, 0.8);
|
||||
color: white;
|
||||
padding: 15px 30px;
|
||||
border-radius: 8px;
|
||||
z-index: 1000;
|
||||
opacity: 0;
|
||||
transition: opacity 0.3s ease-in-out;
|
||||
min-width: 200px;
|
||||
text-align: center;
|
||||
}
|
||||
.toast.show {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
/* Loading spinner styles */
|
||||
.loading-overlay {
|
||||
display: none;
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background-color: rgba(0, 0, 0, 0.5);
|
||||
z-index: 2000;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
.loading-overlay.show {
|
||||
display: flex;
|
||||
}
|
||||
.loading-spinner {
|
||||
width: 60px;
|
||||
height: 60px;
|
||||
border: 6px solid rgba(255, 255, 255, 0.3);
|
||||
border-top-color: #ffffff;
|
||||
border-radius: 50%;
|
||||
animation: spin 1s linear infinite;
|
||||
}
|
||||
.loading-content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 20px;
|
||||
}
|
||||
.loading-text {
|
||||
color: white;
|
||||
font-size: 18px;
|
||||
font-weight: bold;
|
||||
}
|
||||
@keyframes spin {
|
||||
to { transform: rotate(360deg); }
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="toast" id="toast"></div>
|
||||
<div class="loading-overlay" id="loadingOverlay">
|
||||
<div class="loading-content">
|
||||
<div class="loading-spinner"></div>
|
||||
<div class="loading-text" data-lang="connecting">Connecting...</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="language-switch">
|
||||
<select id="language" onchange="changeLanguage()">
|
||||
<option value="de-DE">deutsch</option>
|
||||
<option value="en-US">English</option>
|
||||
<option value="zh-CN">简体中文</option>
|
||||
<option value="zh-TW">繁體中文</option>
|
||||
<option value="ja-JP">日本語</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="tabs">
|
||||
<div class="tab active" onclick="switchTab('wifi')" data-lang="wifi_tab">Wi-Fi Config</div>
|
||||
<div class="tab" onclick="switchTab('advanced')" data-lang="advanced_tab">Advanced</div>
|
||||
</div>
|
||||
|
||||
<div id="wifi-tab" class="tab-content active">
|
||||
<div id="saved_list_container" style="display: none;">
|
||||
<h3 data-lang="saved_wifi">Saved Wi-Fi</h3>
|
||||
<ul id="saved_list">
|
||||
<li>
|
||||
<span>SSID</span>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div>
|
||||
<h3 data-lang="new_wifi">New Wi-Fi</h3>
|
||||
<p class="error" style="color: red; text-align: center;" id="error">
|
||||
</p>
|
||||
<form action="/submit" method="post" onsubmit="submitForm(event)">
|
||||
<p>
|
||||
<label for="ssid">SSID:</label>
|
||||
<input type="text" id="ssid" name="ssid" required readonly>
|
||||
</p>
|
||||
<p>
|
||||
<label for="password" data-lang="password">Password:</label>
|
||||
<input type="password" id="password" name="password">
|
||||
</p>
|
||||
<p style="text-align: center;">
|
||||
<input type="submit" value="Connect" id="button" data-lang-value="connect">
|
||||
</p>
|
||||
</form>
|
||||
<p id="ap_list">
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="advanced-tab" class="tab-content">
|
||||
<form action="/advanced/submit" method="post" onsubmit="submitAdvancedForm(event)">
|
||||
<div>
|
||||
<h3 data-lang="advanced_tab">Advanced</h3>
|
||||
<p class="error" style="color: red; text-align: center;" id="advanced_error"></p>
|
||||
|
||||
<p>
|
||||
<label for="ota_url" data-lang="ota_url">Custom OTA URL:</label>
|
||||
<div style="display: flex; align-items: center; gap: 10px;">
|
||||
<input type="text" id="ota_url" name="ota_url" style="flex: 1;">
|
||||
<button type="button" onclick="clearOtaUrl()" style="padding: 5px 10px; border: 1px solid #ccc; border-radius: 3px; background-color: #f0f0f0; cursor: pointer;">❌</button>
|
||||
</div>
|
||||
</p>
|
||||
|
||||
<p style="display: flex; align-items: center; gap: 10px;">
|
||||
<label for="max_tx_power" data-lang="max_tx_power" style="white-space: nowrap;">Wi-Fi Max TX Power:</label>
|
||||
<select id="max_tx_power" name="max_tx_power">
|
||||
<option value="8">2 dBm</option>
|
||||
<option value="20">5 dBm</option>
|
||||
<option value="28">7 dBm</option>
|
||||
<option value="34">8 dBm</option>
|
||||
<option value="44">11 dBm</option>
|
||||
<option value="52">13 dBm</option>
|
||||
<option value="56">14 dBm</option>
|
||||
<option value="60">15 dBm</option>
|
||||
<option value="66">16 dBm</option>
|
||||
<option value="72">18 dBm</option>
|
||||
<option value="80">20 dBm</option>
|
||||
</select>
|
||||
</p>
|
||||
|
||||
<p style="display: flex; align-items: center; gap: 10px;">
|
||||
<label for="remember_bssid" style="margin: 0;">
|
||||
<span data-lang="remember_bssid">Remember BSSID when connecting to Wi-Fi</span>
|
||||
</label>
|
||||
<input type="checkbox" id="remember_bssid" name="remember_bssid">
|
||||
</p>
|
||||
|
||||
<p style="display: flex; align-items: center; gap: 10px;">
|
||||
<label for="sleep_mode" style="margin: 0;">
|
||||
<span data-lang="sleep_mode">Enable Sleep Mode</span>
|
||||
</label>
|
||||
<input type="checkbox" id="sleep_mode" name="sleep_mode">
|
||||
</p>
|
||||
|
||||
<p style="text-align: center;">
|
||||
<input type="submit" value="Save" id="advanced_button" data-lang-value="save">
|
||||
</p>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<script type="text/javascript">
|
||||
const button = document.getElementById('button');
|
||||
const error = document.getElementById('error');
|
||||
const ssid = document.getElementById('ssid');
|
||||
let support5G = false; // Global variable to store 5G support status
|
||||
|
||||
// Add language translations
|
||||
const translations = {
|
||||
'zh-CN': {
|
||||
title: '网络配置',
|
||||
saved_wifi: '已保存的 Wi-Fi',
|
||||
new_wifi: '新的 Wi-Fi',
|
||||
password: '密码:',
|
||||
connect: '连接',
|
||||
connecting: '正在连接...',
|
||||
select_wifi: '从下面列表选择 2.4G Wi-Fi:',
|
||||
select_wifi_5g: '从下面列表选择 Wi-Fi:',
|
||||
scanning: '扫描中...',
|
||||
wifi_tab: 'Wi-Fi 配置',
|
||||
advanced_tab: '高级选项',
|
||||
ota_url: '自定义 OTA 地址:',
|
||||
max_tx_power: 'Wi-Fi 最大发射功率:',
|
||||
remember_bssid: '连接 Wi-Fi 时记住 BSSID',
|
||||
sleep_mode: '启用睡眠模式',
|
||||
save: '保存'
|
||||
},
|
||||
'zh-TW': {
|
||||
title: '網路設定',
|
||||
saved_wifi: '已儲存的 Wi-Fi',
|
||||
new_wifi: '新的 Wi-Fi',
|
||||
password: '密碼:',
|
||||
connect: '連接',
|
||||
connecting: '正在連接...',
|
||||
select_wifi: '從下方列表選擇 2.4G Wi-Fi:',
|
||||
select_wifi_5g: '從下方列表選擇 Wi-Fi:',
|
||||
scanning: '掃描中...',
|
||||
wifi_tab: 'Wi-Fi 設定',
|
||||
advanced_tab: '進階選項',
|
||||
ota_url: '自定義 OTA 位址:',
|
||||
max_tx_power: 'Wi-Fi 最大發射功率:',
|
||||
remember_bssid: '連接 Wi-Fi 時記住 BSSID',
|
||||
sleep_mode: '啟用睡眠模式',
|
||||
save: '儲存'
|
||||
},
|
||||
'de-DE': {
|
||||
title: 'Netzwerk-Konfiguration',
|
||||
saved_wifi: 'Gesicherte Wi-Fi-Verbindungen',
|
||||
new_wifi: 'Neue Wi-Fi-Verbindung',
|
||||
password: 'Passwort:',
|
||||
connect: 'verbinden',
|
||||
connecting: 'Verbindung wird hergestellt...',
|
||||
select_wifi: 'Wähle ein 2.4G Wi-Fi aus der Liste:',
|
||||
select_wifi_5g: 'Wähle ein Wi-Fi aus der Liste:',
|
||||
scanning: 'Scanne...',
|
||||
wifi_tab: 'Wi-Fi-Konfiguration',
|
||||
advanced_tab: 'erweitert',
|
||||
ota_url: 'Custom OTA URL:',
|
||||
max_tx_power: 'Wi-Fi Max TX Power:',
|
||||
remember_bssid: 'Remember BSSID when connecting to Wi-Fi',
|
||||
sleep_mode: 'Enable Sleep Mode',
|
||||
save: 'Save'
|
||||
},
|
||||
'en-US': {
|
||||
title: 'Network Configuration',
|
||||
saved_wifi: 'Saved Wi-Fi',
|
||||
new_wifi: 'New Wi-Fi',
|
||||
password: 'Password:',
|
||||
connect: 'Connect',
|
||||
connecting: 'Connecting...',
|
||||
select_wifi: 'Select an 2.4G Wi-Fi from the list below:',
|
||||
select_wifi_5g: 'Select a Wi-Fi from the list below:',
|
||||
scanning: 'Scanning...',
|
||||
wifi_tab: 'Wi-Fi Config',
|
||||
advanced_tab: 'Advanced',
|
||||
ota_url: 'Custom OTA URL:',
|
||||
max_tx_power: 'Wi-Fi Max TX Power:',
|
||||
remember_bssid: 'Remember BSSID when connecting to Wi-Fi',
|
||||
sleep_mode: 'Enable Sleep Mode',
|
||||
save: 'Save'
|
||||
},
|
||||
'ja-JP': {
|
||||
title: 'ネットワーク設定',
|
||||
saved_wifi: '保存済みのWi-Fi',
|
||||
new_wifi: '新しいWi-Fi',
|
||||
password: 'パスワード:',
|
||||
connect: '接続',
|
||||
connecting: '接続中...',
|
||||
select_wifi: '以下のリストから2.4G Wi-Fiを選択してください:',
|
||||
select_wifi_5g: '以下のリストからWi-Fiを選択してください:',
|
||||
scanning: 'スキャン中...',
|
||||
wifi_tab: 'Wi-Fi設定',
|
||||
advanced_tab: '詳細設定',
|
||||
ota_url: '自定義 OTA 位址:',
|
||||
max_tx_power: 'Wi-Fi最大送信電力:',
|
||||
remember_bssid: 'Wi-Fi接続時にBSSIDを記憶する',
|
||||
sleep_mode: 'スリープモードを有効にする',
|
||||
save: '保存'
|
||||
}
|
||||
};
|
||||
|
||||
function changeLanguage() {
|
||||
const lang = document.getElementById('language').value;
|
||||
// Check if language is valid
|
||||
if (!translations[lang]) {
|
||||
console.warn(`Unsupported language: ${lang}, defaulting to English`);
|
||||
document.getElementById('language').value = 'en-US';
|
||||
return changeLanguage();
|
||||
}
|
||||
// Set page title
|
||||
document.title = translations[lang].title;
|
||||
document.querySelectorAll('[data-lang]').forEach(element => {
|
||||
const key = element.getAttribute('data-lang');
|
||||
element.textContent = translations[lang][key];
|
||||
});
|
||||
document.querySelectorAll('[data-lang-value]').forEach(element => {
|
||||
const key = element.getAttribute('data-lang-value');
|
||||
element.value = translations[lang][key];
|
||||
});
|
||||
// Update AP list text based on 5G support
|
||||
const apList = document.getElementById('ap_list');
|
||||
if (apList.firstChild) {
|
||||
const selectText = support5G ? translations[lang].select_wifi_5g : translations[lang].select_wifi;
|
||||
apList.firstChild.textContent = selectText;
|
||||
}
|
||||
// Save language preference
|
||||
localStorage.setItem('preferred_language', lang);
|
||||
}
|
||||
|
||||
function renderSavedList(data) {
|
||||
const savedListContainer = document.getElementById('saved_list_container');
|
||||
const savedList = document.getElementById('saved_list');
|
||||
savedList.innerHTML = '';
|
||||
data.forEach((ssid, index) => {
|
||||
const li = document.createElement('li');
|
||||
let html = `<span>${ssid}</span>`;
|
||||
// Only add priority and delete buttons after the first item
|
||||
if (index > 0) {
|
||||
html += ` <span>
|
||||
<button type="button" onclick="setDefaultItem(this, ${index})">⬆️</button>
|
||||
<button type="button" onclick="deleteItem(this, ${index})">❌</button>
|
||||
</span>`;
|
||||
} else {
|
||||
html += ` <span><button type="button" onclick="deleteItem(this, ${index})">❌</button></span>`;
|
||||
}
|
||||
li.innerHTML = html;
|
||||
savedList.appendChild(li);
|
||||
});
|
||||
if (data.length > 0) {
|
||||
savedListContainer.style.display = 'block';
|
||||
} else {
|
||||
savedListContainer.style.display = 'none';
|
||||
}
|
||||
}
|
||||
|
||||
// Delete one item from the list
|
||||
function deleteItem(item, index) {
|
||||
// disable the button
|
||||
item.disabled = true;
|
||||
// /saved/delete?index=INDEX
|
||||
fetch('/saved/delete?index=' + index)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
loadSavedList();
|
||||
});
|
||||
}
|
||||
|
||||
function setDefaultItem(item, index) {
|
||||
item.disabled = true;
|
||||
fetch('/saved/set_default?index=' + index)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
loadSavedList();
|
||||
});
|
||||
}
|
||||
|
||||
// Load saved ssid and password list
|
||||
function loadSavedList() {
|
||||
fetch('/saved/list')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
renderSavedList(data);
|
||||
});
|
||||
}
|
||||
|
||||
// Load AP list from /scan
|
||||
function loadAPList() {
|
||||
if (button.disabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
const lang = document.getElementById('language').value;
|
||||
const apList = document.getElementById('ap_list');
|
||||
|
||||
// Check if AP list already has content (not just scanning text)
|
||||
const hasExistingAPs = apList.querySelector('a') !== null;
|
||||
|
||||
// Only show scanning text if there's no existing AP list
|
||||
if (!hasExistingAPs) {
|
||||
apList.innerHTML = '<p class="scanning">' + translations[lang].scanning + '</p>';
|
||||
}
|
||||
|
||||
fetch('/scan')
|
||||
.then(response => response.text())
|
||||
.then(text => {
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(text);
|
||||
} catch (error) {
|
||||
throw new Error('Invalid scan response JSON');
|
||||
}
|
||||
// Update global 5G support status
|
||||
support5G = data.support_5g;
|
||||
// Use select_wifi_5g if 5G is supported, otherwise use select_wifi
|
||||
const selectText = support5G ? translations[lang].select_wifi_5g : translations[lang].select_wifi;
|
||||
apList.innerHTML = '<p>' + selectText + '</p>';
|
||||
// Iterate through aps array
|
||||
if (!Array.isArray(data.aps)) {
|
||||
throw new Error('Scan response missing aps array');
|
||||
}
|
||||
data.aps.forEach(ap => {
|
||||
// Create a link for each AP
|
||||
const link = document.createElement('a');
|
||||
link.href = '#';
|
||||
link.textContent = ap.ssid + ' (' + ap.rssi + ' dBm)';
|
||||
if (ap.authmode === 0) {
|
||||
link.textContent += ' 🌐';
|
||||
} else {
|
||||
link.textContent += ' 🔒';
|
||||
}
|
||||
link.addEventListener('click', () => {
|
||||
ssid.value = ap.ssid;
|
||||
});
|
||||
apList.appendChild(link);
|
||||
});
|
||||
setTimeout(loadAPList, 5000);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
// On error, keep showing scanning text and try again
|
||||
setTimeout(loadAPList, 5000);
|
||||
});
|
||||
}
|
||||
|
||||
// Set initial language
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
// Get language setting from URL parameters
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const langParam = urlParams.get('lang');
|
||||
|
||||
// Get browser language
|
||||
const browserLang = navigator.language || navigator.userLanguage;
|
||||
|
||||
// Language mapping table: maps browser language codes to supported language codes
|
||||
const languageMap = {
|
||||
'zh': 'zh-CN', // Simplified Chinese
|
||||
'zh-CN': 'zh-CN', // Simplified Chinese
|
||||
'zh-TW': 'zh-TW', // Traditional Chinese
|
||||
'zh-HK': 'zh-TW', // Traditional Chinese (Hong Kong)
|
||||
'ja': 'ja-JP', // Japanese
|
||||
'ja-JP': 'ja-JP', // Japanese
|
||||
'en': 'en-US', // English
|
||||
'en-US': 'en-US', // English
|
||||
'en-GB': 'en-US', // English (UK)
|
||||
'en-CA': 'en-US', // English (Canada)
|
||||
'en-AU': 'en-US' // English (Australia)
|
||||
};
|
||||
|
||||
// Get supported language code
|
||||
const getSupportedLanguage = (lang) => {
|
||||
// First try exact match
|
||||
if (languageMap[lang]) {
|
||||
return languageMap[lang];
|
||||
}
|
||||
// Then try to match only the main language code (e.g. 'zh' instead of 'zh-CN')
|
||||
const mainLang = lang.split('-')[0];
|
||||
if (languageMap[mainLang]) {
|
||||
return languageMap[mainLang];
|
||||
}
|
||||
// If no match, return English
|
||||
return 'en-US';
|
||||
};
|
||||
|
||||
// Priority: URL parameter > local storage > browser language setting
|
||||
const savedLang = langParam ||
|
||||
localStorage.getItem('preferred_language') ||
|
||||
getSupportedLanguage(browserLang);
|
||||
|
||||
document.getElementById('language').value = savedLang;
|
||||
changeLanguage();
|
||||
loadSavedList();
|
||||
loadAPList();
|
||||
loadAdvancedConfig();
|
||||
});
|
||||
|
||||
// Listen to pageshow event to handle browser back button
|
||||
window.addEventListener('pageshow', (event) => {
|
||||
if (event.persisted) {
|
||||
loadSavedList();
|
||||
} else {
|
||||
// Already handled during normal load
|
||||
}
|
||||
});
|
||||
|
||||
async function submitForm(event) {
|
||||
event.preventDefault();
|
||||
button.disabled = true;
|
||||
error.textContent = '';
|
||||
|
||||
// Show loading animation
|
||||
const loadingOverlay = document.getElementById('loadingOverlay');
|
||||
loadingOverlay.classList.add('show');
|
||||
|
||||
const ssidValue = ssid.value;
|
||||
const passwordValue = document.getElementById('password').value;
|
||||
|
||||
const payload = {
|
||||
ssid: ssidValue,
|
||||
password: passwordValue
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch('/submit', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
if (!data.success) {
|
||||
throw new Error(data.error || 'Connection failed');
|
||||
}
|
||||
|
||||
// Connection successful, redirect to done page
|
||||
button.disabled = false;
|
||||
window.location.href = '/done.html';
|
||||
} catch (err) {
|
||||
// Hide loading animation on error
|
||||
loadingOverlay.classList.remove('show');
|
||||
error.textContent = err.message;
|
||||
button.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
function switchTab(tabName) {
|
||||
// Hide all tab contents
|
||||
document.querySelectorAll('.tab-content').forEach(content => {
|
||||
content.classList.remove('active');
|
||||
});
|
||||
// Remove active class from all tabs
|
||||
document.querySelectorAll('.tab').forEach(tab => {
|
||||
tab.classList.remove('active');
|
||||
});
|
||||
// Show selected tab content
|
||||
document.getElementById(tabName + '-tab').classList.add('active');
|
||||
// Activate selected tab
|
||||
document.querySelector(`.tab[onclick="switchTab('${tabName}')"]`).classList.add('active');
|
||||
}
|
||||
|
||||
async function submitAdvancedForm(event) {
|
||||
event.preventDefault();
|
||||
const advancedButton = document.getElementById('advanced_button');
|
||||
const advancedError = document.getElementById('advanced_error');
|
||||
advancedButton.disabled = true;
|
||||
advancedError.textContent = '';
|
||||
|
||||
const config = {
|
||||
ota_url: document.getElementById('ota_url').value,
|
||||
max_tx_power: parseInt(document.getElementById('max_tx_power').value),
|
||||
remember_bssid: document.getElementById('remember_bssid').checked,
|
||||
sleep_mode: document.getElementById('sleep_mode').checked
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch('/advanced/submit', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(config)
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
if (!data.success) {
|
||||
throw new Error(data.error || 'Save failed');
|
||||
}
|
||||
|
||||
advancedButton.disabled = false;
|
||||
showToast('Configuration saved');
|
||||
} catch (err) {
|
||||
advancedError.textContent = err.message;
|
||||
advancedButton.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Show toast message
|
||||
function showToast(message) {
|
||||
const toast = document.getElementById('toast');
|
||||
toast.textContent = message;
|
||||
toast.classList.add('show');
|
||||
setTimeout(() => {
|
||||
toast.classList.remove('show');
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
// Load advanced configuration
|
||||
async function loadAdvancedConfig() {
|
||||
try {
|
||||
const response = await fetch('/advanced/config');
|
||||
const data = await response.json();
|
||||
|
||||
if (data.ota_url) {
|
||||
document.getElementById('ota_url').value = data.ota_url;
|
||||
}
|
||||
if (data.max_tx_power) {
|
||||
document.getElementById('max_tx_power').value = data.max_tx_power;
|
||||
}
|
||||
if (data.remember_bssid !== undefined) {
|
||||
document.getElementById('remember_bssid').checked = data.remember_bssid;
|
||||
}
|
||||
if (data.sleep_mode !== undefined) {
|
||||
document.getElementById('sleep_mode').checked = data.sleep_mode;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error loading advanced config:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Clear OTA URL
|
||||
function clearOtaUrl() {
|
||||
document.getElementById('ota_url').value = '';
|
||||
}
|
||||
|
||||
// Enable SSID input (fix iOS bug: triggering focus event when loadAPList)
|
||||
ssid.addEventListener('touchstart', () => {
|
||||
ssid.removeAttribute('readonly');
|
||||
ssid.focus();
|
||||
}, {once: true});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,85 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>WiFi Configuration</title>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
|
||||
<style>
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 100vh;
|
||||
font-family: Arial, sans-serif;
|
||||
/* Prevent content from overflowing */
|
||||
max-height: 100vh;
|
||||
box-sizing: border-box;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.checkmark {
|
||||
width: 150px;
|
||||
height: 150px;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
|
||||
.message {
|
||||
text-align: center;
|
||||
font-size: 20px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<svg class="checkmark" viewBox="0 0 52 52">
|
||||
<circle class="checkmark__circle" cx="26" cy="26" r="25" fill="none" stroke="#4CAF50" stroke-width="2"/>
|
||||
<path class="checkmark__check" fill="none" stroke="#4CAF50" stroke-width="2" d="M14.1 27.2l7.1 7.2 16.7-16.8"/>
|
||||
</svg>
|
||||
|
||||
<div class="message">
|
||||
<p>设备将在 <span id="countdown">3</span> 秒后重启</p>
|
||||
<p>Device will restart in <span id="countdown-en">3</span> seconds</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
window.addEventListener('load', function() {
|
||||
let count = 3;
|
||||
const countdownElement = document.getElementById('countdown');
|
||||
const countdownEnElement = document.getElementById('countdown-en');
|
||||
|
||||
const timer = setInterval(function() {
|
||||
count--;
|
||||
countdownElement.textContent = count;
|
||||
countdownEnElement.textContent = count;
|
||||
|
||||
if (count <= 0) {
|
||||
clearInterval(timer);
|
||||
fetch('/reboot', {
|
||||
method: 'POST'
|
||||
})
|
||||
.then(response => {
|
||||
if (response.ok) {
|
||||
console.log('重启指令已发送');
|
||||
window.close();
|
||||
} else {
|
||||
console.error('发送重启指令失败');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
});
|
||||
}
|
||||
}, 1000);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
165
components/esp-wifi-connect/dns_server.cc
Normal file
165
components/esp-wifi-connect/dns_server.cc
Normal file
@@ -0,0 +1,165 @@
|
||||
#include "dns_server.h"
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <esp_log.h>
|
||||
#include <lwip/sockets.h>
|
||||
#include <lwip/netdb.h>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
|
||||
#define TAG "DnsServer"
|
||||
|
||||
namespace {
|
||||
constexpr const char* kConfigHost = "borlanet-dev.lkall.com";
|
||||
|
||||
bool ParseQName(const uint8_t* buffer, int len, int offset, std::string& out, int& end_offset) {
|
||||
out.clear();
|
||||
while (offset < len) {
|
||||
uint8_t label_len = buffer[offset++];
|
||||
if (label_len == 0) {
|
||||
end_offset = offset;
|
||||
return true;
|
||||
}
|
||||
if ((label_len & 0xC0) == 0xC0) {
|
||||
// Compression pointer not handled for queries here.
|
||||
return false;
|
||||
}
|
||||
if (offset + label_len > len) {
|
||||
return false;
|
||||
}
|
||||
if (!out.empty()) {
|
||||
out.push_back('.');
|
||||
}
|
||||
for (uint8_t i = 0; i < label_len; ++i) {
|
||||
out.push_back(static_cast<char>(std::tolower(buffer[offset + i])));
|
||||
}
|
||||
offset += label_len;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* DnsTypeToString(uint16_t qtype) {
|
||||
switch (qtype) {
|
||||
case 1:
|
||||
return "A";
|
||||
case 28:
|
||||
return "AAAA";
|
||||
case 64:
|
||||
return "SVCB";
|
||||
case 65:
|
||||
return "HTTPS";
|
||||
case 255:
|
||||
return "ANY";
|
||||
default:
|
||||
return "OTHER";
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
DnsServer::DnsServer() {
|
||||
}
|
||||
|
||||
DnsServer::~DnsServer() {
|
||||
}
|
||||
|
||||
void DnsServer::Start(esp_ip4_addr_t gateway) {
|
||||
ESP_LOGI(TAG, "Starting DNS server");
|
||||
gateway_ = gateway;
|
||||
|
||||
fd_ = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (fd_ < 0) {
|
||||
ESP_LOGE(TAG, "Failed to create socket");
|
||||
return;
|
||||
}
|
||||
|
||||
struct sockaddr_in server_addr;
|
||||
memset(&server_addr, 0, sizeof(server_addr));
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
server_addr.sin_port = htons(port_);
|
||||
|
||||
if (bind(fd_, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
||||
ESP_LOGE(TAG, "failed to bind port %d", port_);
|
||||
close(fd_);
|
||||
return;
|
||||
}
|
||||
|
||||
xTaskCreate([](void* arg) {
|
||||
DnsServer* dns_server = static_cast<DnsServer*>(arg);
|
||||
dns_server->Run();
|
||||
}, "DnsServerTask", 4096, this, 5, NULL);
|
||||
}
|
||||
|
||||
void DnsServer::Stop() {
|
||||
ESP_LOGI(TAG, "Stopping DNS server");
|
||||
}
|
||||
|
||||
void DnsServer::Run() {
|
||||
char buffer[512];
|
||||
while (1) {
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t client_addr_len = sizeof(client_addr);
|
||||
int len = recvfrom(fd_, buffer, sizeof(buffer), 0, (struct sockaddr *)&client_addr, &client_addr_len);
|
||||
if (len < 0) {
|
||||
ESP_LOGE(TAG, "recvfrom failed, errno=%d", errno);
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string qname;
|
||||
int qname_end = 0;
|
||||
bool qname_ok = ParseQName(reinterpret_cast<uint8_t*>(buffer), len, 12, qname, qname_end);
|
||||
uint16_t qtype = 0;
|
||||
uint16_t qclass = 0;
|
||||
if (qname_ok && qname_end + 4 <= len) {
|
||||
qtype = (static_cast<uint8_t>(buffer[qname_end]) << 8) |
|
||||
static_cast<uint8_t>(buffer[qname_end + 1]);
|
||||
qclass = (static_cast<uint8_t>(buffer[qname_end + 2]) << 8) |
|
||||
static_cast<uint8_t>(buffer[qname_end + 3]);
|
||||
}
|
||||
|
||||
bool valid_class = qclass == 1 || qclass == 255;
|
||||
bool is_a = qtype == 1 && valid_class;
|
||||
bool is_any = qtype == 255 && valid_class;
|
||||
bool is_target_host = qname_ok && qname == kConfigHost;
|
||||
bool send_local_ip = is_target_host && (is_a || is_any);
|
||||
|
||||
char client_ip[16] = {0};
|
||||
inet_ntoa_r(client_addr.sin_addr, client_ip, sizeof(client_ip));
|
||||
|
||||
// Set response flag and recursion available
|
||||
buffer[2] |= 0x80;
|
||||
buffer[3] |= 0x80;
|
||||
|
||||
if (send_local_ip) {
|
||||
buffer[6] = 0;
|
||||
buffer[7] = 1; // answer count
|
||||
buffer[8] = buffer[9] = 0;
|
||||
buffer[10] = buffer[11] = 0;
|
||||
|
||||
// Add answer section
|
||||
memcpy(&buffer[len], "\xc0\x0c", 2); // Name pointer
|
||||
len += 2;
|
||||
memcpy(&buffer[len], "\x00\x01\x00\x01\x00\x00\x00\x1c\x00\x04", 10); // Type, class, TTL, data length
|
||||
len += 10;
|
||||
memcpy(&buffer[len], &gateway_.addr, 4); // 192.168.4.1
|
||||
len += 4;
|
||||
ESP_LOGI(TAG, "DNS %s %s from %s -> %s", DnsTypeToString(qtype), qname.c_str(), client_ip, inet_ntoa(gateway_.addr));
|
||||
} else if (qname_ok) {
|
||||
// Return NOERROR with no answers for unrelated domains and unsupported types.
|
||||
buffer[3] &= 0xF0; // RCODE=0
|
||||
buffer[6] = buffer[7] = 0; // answer count
|
||||
buffer[8] = buffer[9] = 0; // authority count
|
||||
buffer[10] = buffer[11] = 0; // additional count
|
||||
ESP_LOGD(TAG, "DNS %s %s from %s -> empty", DnsTypeToString(qtype), qname.c_str(), client_ip);
|
||||
} else {
|
||||
// Malformed queries get NXDOMAIN.
|
||||
buffer[3] = (buffer[3] & 0xF0) | 0x03; // RCODE=3
|
||||
buffer[6] = buffer[7] = 0; // answer count
|
||||
buffer[8] = buffer[9] = 0; // authority count
|
||||
buffer[10] = buffer[11] = 0; // additional count
|
||||
ESP_LOGD(TAG, "DNS malformed query from %s", client_ip);
|
||||
}
|
||||
|
||||
sendto(fd_, buffer, len, 0, (struct sockaddr *)&client_addr, client_addr_len);
|
||||
}
|
||||
}
|
||||
11
components/esp-wifi-connect/idf_component.yml
Normal file
11
components/esp-wifi-connect/idf_component.yml
Normal file
@@ -0,0 +1,11 @@
|
||||
dependencies:
|
||||
idf: '>=5.3'
|
||||
description: ESP32 WiFi Configuration
|
||||
files:
|
||||
exclude:
|
||||
- .git
|
||||
- dist
|
||||
license: MIT
|
||||
repository: https://github.com/78/esp-wifi-connect
|
||||
url: https://github.com/78/esp-wifi-connect
|
||||
version: 2.6.0
|
||||
22
components/esp-wifi-connect/include/dns_server.h
Normal file
22
components/esp-wifi-connect/include/dns_server.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef _DNS_SERVER_H_
|
||||
#define _DNS_SERVER_H_
|
||||
|
||||
#include <string>
|
||||
#include <esp_netif_ip_addr.h>
|
||||
|
||||
class DnsServer {
|
||||
public:
|
||||
DnsServer();
|
||||
~DnsServer();
|
||||
|
||||
void Start(esp_ip4_addr_t gateway);
|
||||
void Stop();
|
||||
|
||||
private:
|
||||
int port_ = 53;
|
||||
int fd_ = -1;
|
||||
esp_ip4_addr_t gateway_;
|
||||
void Run();
|
||||
};
|
||||
|
||||
#endif // _DNS_SERVER_H_
|
||||
35
components/esp-wifi-connect/include/ssid_manager.h
Normal file
35
components/esp-wifi-connect/include/ssid_manager.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef SSID_MANAGER_H
|
||||
#define SSID_MANAGER_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct SsidItem {
|
||||
std::string ssid;
|
||||
std::string password;
|
||||
};
|
||||
|
||||
class SsidManager {
|
||||
public:
|
||||
static SsidManager& GetInstance() {
|
||||
static SsidManager instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void AddSsid(const std::string& ssid, const std::string& password);
|
||||
void RemoveSsid(int index);
|
||||
void SetDefaultSsid(int index);
|
||||
void Clear();
|
||||
const std::vector<SsidItem>& GetSsidList() const { return ssid_list_; }
|
||||
|
||||
private:
|
||||
SsidManager();
|
||||
~SsidManager();
|
||||
|
||||
void LoadFromNvs();
|
||||
void SaveToNvs();
|
||||
|
||||
std::vector<SsidItem> ssid_list_;
|
||||
};
|
||||
|
||||
#endif // SSID_MANAGER_H
|
||||
82
components/esp-wifi-connect/include/wifi_configuration_ap.h
Normal file
82
components/esp-wifi-connect/include/wifi_configuration_ap.h
Normal file
@@ -0,0 +1,82 @@
|
||||
#ifndef _WIFI_CONFIGURATION_AP_H_
|
||||
#define _WIFI_CONFIGURATION_AP_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
#include <functional>
|
||||
|
||||
#include <esp_http_server.h>
|
||||
#include <esp_event.h>
|
||||
#include <esp_timer.h>
|
||||
#include <esp_netif.h>
|
||||
#include <esp_wifi_types_generic.h>
|
||||
|
||||
#include "dns_server.h"
|
||||
|
||||
class WifiConfigurationAp {
|
||||
public:
|
||||
static WifiConfigurationAp& GetInstance();
|
||||
void SetSsidPrefix(const std::string &&ssid_prefix);
|
||||
void SetLanguage(const std::string &&language);
|
||||
void Start();
|
||||
void Stop();
|
||||
void StartSmartConfig();
|
||||
void SetConfigSuccessCallback(std::function<void()> callback);
|
||||
bool ConnectToWifi(const std::string &ssid, const std::string &password);
|
||||
void Save(const std::string &ssid, const std::string &password);
|
||||
std::vector<wifi_ap_record_t> GetAccessPoints();
|
||||
std::string GetSsid();
|
||||
std::string GetWebServerUrl();
|
||||
|
||||
// Delete copy constructor and assignment operator
|
||||
WifiConfigurationAp(const WifiConfigurationAp&) = delete;
|
||||
WifiConfigurationAp& operator=(const WifiConfigurationAp&) = delete;
|
||||
|
||||
private:
|
||||
// Private constructor
|
||||
WifiConfigurationAp();
|
||||
~WifiConfigurationAp();
|
||||
|
||||
std::mutex mutex_;
|
||||
DnsServer dns_server_;
|
||||
httpd_handle_t server_ = NULL;
|
||||
httpd_handle_t https_server_ = NULL;
|
||||
EventGroupHandle_t event_group_;
|
||||
std::string ssid_prefix_;
|
||||
std::string language_;
|
||||
esp_event_handler_instance_t instance_any_id_;
|
||||
esp_event_handler_instance_t instance_got_ip_;
|
||||
esp_timer_handle_t scan_timer_ = nullptr;
|
||||
bool is_connecting_ = false;
|
||||
esp_netif_t* ap_netif_ = nullptr;
|
||||
std::vector<wifi_ap_record_t> ap_records_;
|
||||
bool scan_in_progress_ = false;
|
||||
int64_t last_scan_time_us_ = 0;
|
||||
std::function<void()> on_config_success_;
|
||||
esp_timer_handle_t config_success_timer_ = nullptr;
|
||||
bool config_success_notified_ = false;
|
||||
bool wifi_started_ = false;
|
||||
bool wifi_initialized_ = false;
|
||||
|
||||
// 高级配置项
|
||||
std::string ota_url_;
|
||||
int8_t max_tx_power_;
|
||||
bool remember_bssid_;
|
||||
bool sleep_mode_;
|
||||
wifi_scan_config_t scan_config_ = {};
|
||||
|
||||
void StartAccessPoint();
|
||||
void StartWebServer();
|
||||
void StartScan();
|
||||
void NotifyConfigSuccess();
|
||||
|
||||
// Event handlers
|
||||
static void WifiEventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
|
||||
static void IpEventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
|
||||
static void SmartConfigEventHandler(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data);
|
||||
esp_event_handler_instance_t sc_event_instance_ = nullptr;
|
||||
};
|
||||
|
||||
#endif // _WIFI_CONFIGURATION_AP_H_
|
||||
71
components/esp-wifi-connect/include/wifi_station.h
Normal file
71
components/esp-wifi-connect/include/wifi_station.h
Normal file
@@ -0,0 +1,71 @@
|
||||
#ifndef _WIFI_STATION_H_
|
||||
#define _WIFI_STATION_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
#include <esp_event.h>
|
||||
#include <esp_timer.h>
|
||||
#include <esp_netif.h>
|
||||
#include <esp_wifi_types_generic.h>
|
||||
|
||||
struct WifiApRecord {
|
||||
std::string ssid;
|
||||
std::string password;
|
||||
int channel;
|
||||
wifi_auth_mode_t authmode;
|
||||
uint8_t bssid[6];
|
||||
};
|
||||
|
||||
class WifiStation {
|
||||
public:
|
||||
static WifiStation& GetInstance();
|
||||
static constexpr int kMaxReconnectCount = 5;
|
||||
void AddAuth(const std::string &&ssid, const std::string &&password);
|
||||
void Start();
|
||||
void Stop();
|
||||
bool IsConnected();
|
||||
bool WaitForConnected(int timeout_ms = 10000);
|
||||
int GetReconnectCount() const { return reconnect_count_; }
|
||||
int8_t GetRssi();
|
||||
std::string GetSsid() const { return ssid_; }
|
||||
std::string GetIpAddress() const { return ip_address_; }
|
||||
uint8_t GetChannel();
|
||||
void SetPowerSaveMode(bool enabled);
|
||||
|
||||
void OnConnect(std::function<void(const std::string& ssid)> on_connect);
|
||||
void OnConnected(std::function<void(const std::string& ssid)> on_connected);
|
||||
void OnScanBegin(std::function<void()> on_scan_begin);
|
||||
|
||||
private:
|
||||
WifiStation();
|
||||
~WifiStation();
|
||||
WifiStation(const WifiStation&) = delete;
|
||||
WifiStation& operator=(const WifiStation&) = delete;
|
||||
|
||||
EventGroupHandle_t event_group_;
|
||||
esp_timer_handle_t timer_handle_ = nullptr;
|
||||
esp_event_handler_instance_t instance_any_id_ = nullptr;
|
||||
esp_event_handler_instance_t instance_got_ip_ = nullptr;
|
||||
esp_netif_t* station_netif_ = nullptr;
|
||||
std::string ssid_;
|
||||
std::string password_;
|
||||
std::string ip_address_;
|
||||
int8_t max_tx_power_;
|
||||
uint8_t remember_bssid_;
|
||||
int reconnect_count_ = 0;
|
||||
bool wifi_initialized_ = false;
|
||||
bool wifi_started_ = false;
|
||||
std::function<void(const std::string& ssid)> on_connect_;
|
||||
std::function<void(const std::string& ssid)> on_connected_;
|
||||
std::function<void()> on_scan_begin_;
|
||||
std::vector<WifiApRecord> connect_queue_;
|
||||
|
||||
void HandleScanResult();
|
||||
void StartConnect();
|
||||
static void WifiEventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
|
||||
static void IpEventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
|
||||
};
|
||||
|
||||
#endif // _WIFI_STATION_H_
|
||||
125
components/esp-wifi-connect/ssid_manager.cc
Normal file
125
components/esp-wifi-connect/ssid_manager.cc
Normal file
@@ -0,0 +1,125 @@
|
||||
#include "ssid_manager.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <esp_log.h>
|
||||
#include <nvs_flash.h>
|
||||
|
||||
#define TAG "SsidManager"
|
||||
#define NVS_NAMESPACE "wifi"
|
||||
#define MAX_WIFI_SSID_COUNT 10
|
||||
|
||||
SsidManager::SsidManager() {
|
||||
LoadFromNvs();
|
||||
}
|
||||
|
||||
SsidManager::~SsidManager() {
|
||||
}
|
||||
|
||||
void SsidManager::Clear() {
|
||||
ssid_list_.clear();
|
||||
SaveToNvs();
|
||||
}
|
||||
|
||||
void SsidManager::LoadFromNvs() {
|
||||
ssid_list_.clear();
|
||||
|
||||
// Load ssid and password from NVS from namespace "wifi"
|
||||
// ssid, ssid1, ssid2, ... ssid9
|
||||
// password, password1, password2, ... password9
|
||||
nvs_handle_t nvs_handle;
|
||||
auto ret = nvs_open(NVS_NAMESPACE, NVS_READONLY, &nvs_handle);
|
||||
if (ret != ESP_OK) {
|
||||
// The namespace doesn't exist, just return
|
||||
ESP_LOGW(TAG, "NVS namespace %s doesn't exist", NVS_NAMESPACE);
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < MAX_WIFI_SSID_COUNT; i++) {
|
||||
std::string ssid_key = "ssid";
|
||||
if (i > 0) {
|
||||
ssid_key += std::to_string(i);
|
||||
}
|
||||
std::string password_key = "password";
|
||||
if (i > 0) {
|
||||
password_key += std::to_string(i);
|
||||
}
|
||||
|
||||
char ssid[33];
|
||||
char password[65];
|
||||
size_t length = sizeof(ssid);
|
||||
if (nvs_get_str(nvs_handle, ssid_key.c_str(), ssid, &length) != ESP_OK) {
|
||||
continue;
|
||||
}
|
||||
length = sizeof(password);
|
||||
if (nvs_get_str(nvs_handle, password_key.c_str(), password, &length) != ESP_OK) {
|
||||
continue;
|
||||
}
|
||||
ssid_list_.push_back({ssid, password});
|
||||
}
|
||||
nvs_close(nvs_handle);
|
||||
}
|
||||
|
||||
void SsidManager::SaveToNvs() {
|
||||
nvs_handle_t nvs_handle;
|
||||
ESP_ERROR_CHECK(nvs_open(NVS_NAMESPACE, NVS_READWRITE, &nvs_handle));
|
||||
for (int i = 0; i < MAX_WIFI_SSID_COUNT; i++) {
|
||||
std::string ssid_key = "ssid";
|
||||
if (i > 0) {
|
||||
ssid_key += std::to_string(i);
|
||||
}
|
||||
std::string password_key = "password";
|
||||
if (i > 0) {
|
||||
password_key += std::to_string(i);
|
||||
}
|
||||
|
||||
if (i < ssid_list_.size()) {
|
||||
nvs_set_str(nvs_handle, ssid_key.c_str(), ssid_list_[i].ssid.c_str());
|
||||
nvs_set_str(nvs_handle, password_key.c_str(), ssid_list_[i].password.c_str());
|
||||
} else {
|
||||
nvs_erase_key(nvs_handle, ssid_key.c_str());
|
||||
nvs_erase_key(nvs_handle, password_key.c_str());
|
||||
}
|
||||
}
|
||||
nvs_commit(nvs_handle);
|
||||
nvs_close(nvs_handle);
|
||||
}
|
||||
|
||||
void SsidManager::AddSsid(const std::string& ssid, const std::string& password) {
|
||||
for (auto& item : ssid_list_) {
|
||||
ESP_LOGI(TAG, "compare [%s:%d] [%s:%d]", item.ssid.c_str(), item.ssid.size(), ssid.c_str(), ssid.size());
|
||||
if (item.ssid == ssid) {
|
||||
ESP_LOGW(TAG, "SSID %s already exists, overwrite it", ssid.c_str());
|
||||
item.password = password;
|
||||
SaveToNvs();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (ssid_list_.size() >= MAX_WIFI_SSID_COUNT) {
|
||||
ESP_LOGW(TAG, "SSID list is full, pop one");
|
||||
ssid_list_.pop_back();
|
||||
}
|
||||
// Add the new ssid to the front of the list
|
||||
ssid_list_.insert(ssid_list_.begin(), {ssid, password});
|
||||
SaveToNvs();
|
||||
}
|
||||
|
||||
void SsidManager::RemoveSsid(int index) {
|
||||
if (index < 0 || index >= ssid_list_.size()) {
|
||||
ESP_LOGW(TAG, "Invalid index %d", index);
|
||||
return;
|
||||
}
|
||||
ssid_list_.erase(ssid_list_.begin() + index);
|
||||
SaveToNvs();
|
||||
}
|
||||
|
||||
void SsidManager::SetDefaultSsid(int index) {
|
||||
if (index < 0 || index >= ssid_list_.size()) {
|
||||
ESP_LOGW(TAG, "Invalid index %d", index);
|
||||
return;
|
||||
}
|
||||
// Move the ssid at index to the front of the list
|
||||
auto item = ssid_list_[index];
|
||||
ssid_list_.erase(ssid_list_.begin() + index);
|
||||
ssid_list_.insert(ssid_list_.begin(), item);
|
||||
SaveToNvs();
|
||||
}
|
||||
1205
components/esp-wifi-connect/wifi_configuration_ap.cc
Normal file
1205
components/esp-wifi-connect/wifi_configuration_ap.cc
Normal file
File diff suppressed because it is too large
Load Diff
349
components/esp-wifi-connect/wifi_station.cc
Normal file
349
components/esp-wifi-connect/wifi_station.cc
Normal file
@@ -0,0 +1,349 @@
|
||||
#include "wifi_station.h"
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/event_groups.h>
|
||||
#include <esp_err.h>
|
||||
#include <esp_log.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <nvs.h>
|
||||
#include "nvs_flash.h"
|
||||
#include <esp_netif.h>
|
||||
#include <esp_system.h>
|
||||
#include "ssid_manager.h"
|
||||
|
||||
#define TAG "WifiStation"
|
||||
#define WIFI_EVENT_CONNECTED BIT0
|
||||
|
||||
namespace {
|
||||
|
||||
void LogWifiOpResult(const char* op, esp_err_t err) {
|
||||
if (err == ESP_OK) {
|
||||
return;
|
||||
}
|
||||
if (err == ESP_ERR_WIFI_NOT_INIT || err == ESP_ERR_WIFI_NOT_STARTED || err == ESP_ERR_INVALID_STATE) {
|
||||
ESP_LOGI(TAG, "%s skipped: %s", op, esp_err_to_name(err));
|
||||
return;
|
||||
}
|
||||
ESP_LOGW(TAG, "%s failed: %s", op, esp_err_to_name(err));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
WifiStation& WifiStation::GetInstance() {
|
||||
static WifiStation instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
WifiStation::WifiStation() {
|
||||
// Create the event group
|
||||
event_group_ = xEventGroupCreate();
|
||||
|
||||
// 读取配置
|
||||
nvs_handle_t nvs;
|
||||
esp_err_t err = nvs_open("wifi", NVS_READONLY, &nvs);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to open NVS: %d", err);
|
||||
}
|
||||
err = nvs_get_i8(nvs, "max_tx_power", &max_tx_power_);
|
||||
if (err != ESP_OK) {
|
||||
max_tx_power_ = 0;
|
||||
}
|
||||
err = nvs_get_u8(nvs, "remember_bssid", &remember_bssid_);
|
||||
if (err != ESP_OK) {
|
||||
remember_bssid_ = 0;
|
||||
}
|
||||
nvs_close(nvs);
|
||||
}
|
||||
|
||||
WifiStation::~WifiStation() {
|
||||
vEventGroupDelete(event_group_);
|
||||
}
|
||||
|
||||
void WifiStation::AddAuth(const std::string &&ssid, const std::string &&password) {
|
||||
auto& ssid_manager = SsidManager::GetInstance();
|
||||
ssid_manager.AddSsid(ssid, password);
|
||||
}
|
||||
|
||||
void WifiStation::Stop() {
|
||||
if (timer_handle_ != nullptr) {
|
||||
esp_timer_stop(timer_handle_);
|
||||
esp_timer_delete(timer_handle_);
|
||||
timer_handle_ = nullptr;
|
||||
}
|
||||
|
||||
xEventGroupClearBits(event_group_, WIFI_EVENT_CONNECTED);
|
||||
reconnect_count_ = 0;
|
||||
connect_queue_.clear();
|
||||
ip_address_.clear();
|
||||
|
||||
LogWifiOpResult("esp_wifi_scan_stop", esp_wifi_scan_stop());
|
||||
|
||||
// 取消注册事件处理程序
|
||||
if (instance_any_id_ != nullptr) {
|
||||
auto err = esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id_);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Failed to unregister WIFI_EVENT handler: %s", esp_err_to_name(err));
|
||||
}
|
||||
instance_any_id_ = nullptr;
|
||||
}
|
||||
if (instance_got_ip_ != nullptr) {
|
||||
auto err = esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip_);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Failed to unregister IP_EVENT handler: %s", esp_err_to_name(err));
|
||||
}
|
||||
instance_got_ip_ = nullptr;
|
||||
}
|
||||
|
||||
// Reset the WiFi stack
|
||||
if (wifi_started_) {
|
||||
LogWifiOpResult("esp_wifi_stop", esp_wifi_stop());
|
||||
wifi_started_ = false;
|
||||
} else {
|
||||
LogWifiOpResult("esp_wifi_stop", esp_wifi_stop());
|
||||
}
|
||||
|
||||
if (wifi_initialized_) {
|
||||
LogWifiOpResult("esp_wifi_deinit", esp_wifi_deinit());
|
||||
wifi_initialized_ = false;
|
||||
} else {
|
||||
LogWifiOpResult("esp_wifi_deinit", esp_wifi_deinit());
|
||||
}
|
||||
|
||||
if (station_netif_ != nullptr) {
|
||||
// TODO: esp_netif_destroy will cause crash
|
||||
// esp_netif_destroy(station_netif_);
|
||||
station_netif_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void WifiStation::OnScanBegin(std::function<void()> on_scan_begin) {
|
||||
on_scan_begin_ = on_scan_begin;
|
||||
}
|
||||
|
||||
void WifiStation::OnConnect(std::function<void(const std::string& ssid)> on_connect) {
|
||||
on_connect_ = on_connect;
|
||||
}
|
||||
|
||||
void WifiStation::OnConnected(std::function<void(const std::string& ssid)> on_connected) {
|
||||
on_connected_ = on_connected;
|
||||
}
|
||||
|
||||
void WifiStation::Start() {
|
||||
if (wifi_started_) {
|
||||
ESP_LOGI(TAG, "WiFi station already started");
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize the TCP/IP stack
|
||||
auto netif_err = esp_netif_init();
|
||||
if (netif_err != ESP_OK && netif_err != ESP_ERR_INVALID_STATE) {
|
||||
ESP_ERROR_CHECK(netif_err);
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
&WifiStation::WifiEventHandler,
|
||||
this,
|
||||
&instance_any_id_));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
|
||||
IP_EVENT_STA_GOT_IP,
|
||||
&WifiStation::IpEventHandler,
|
||||
this,
|
||||
&instance_got_ip_));
|
||||
|
||||
// Reuse existing default STA netif if present to avoid duplicate-key assert
|
||||
// (e.g. exiting AP config mode and starting station again).
|
||||
station_netif_ = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
|
||||
if (station_netif_ == nullptr) {
|
||||
station_netif_ = esp_netif_create_default_wifi_sta();
|
||||
}
|
||||
|
||||
// Initialize the WiFi stack in station mode
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
cfg.nvs_enable = false;
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
wifi_initialized_ = true;
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
//ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
wifi_started_ = true;
|
||||
|
||||
if (max_tx_power_ != 0) {
|
||||
ESP_ERROR_CHECK(esp_wifi_set_max_tx_power(max_tx_power_));
|
||||
}
|
||||
|
||||
// Setup the timer to scan WiFi
|
||||
esp_timer_create_args_t timer_args = {
|
||||
.callback = [](void* arg) {
|
||||
esp_wifi_scan_start(nullptr, false);
|
||||
},
|
||||
.arg = this,
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
.name = "WiFiScanTimer",
|
||||
.skip_unhandled_events = true
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_timer_create(&timer_args, &timer_handle_));
|
||||
}
|
||||
|
||||
bool WifiStation::WaitForConnected(int timeout_ms) {
|
||||
auto bits = xEventGroupWaitBits(event_group_, WIFI_EVENT_CONNECTED, pdFALSE, pdFALSE, timeout_ms / portTICK_PERIOD_MS);
|
||||
return (bits & WIFI_EVENT_CONNECTED) != 0;
|
||||
}
|
||||
|
||||
void WifiStation::HandleScanResult() {
|
||||
uint16_t ap_num = 0;
|
||||
esp_wifi_scan_get_ap_num(&ap_num);
|
||||
wifi_ap_record_t *ap_records = (wifi_ap_record_t *)malloc(ap_num * sizeof(wifi_ap_record_t));
|
||||
esp_wifi_scan_get_ap_records(&ap_num, ap_records);
|
||||
// sort by rssi descending
|
||||
std::sort(ap_records, ap_records + ap_num, [](const wifi_ap_record_t& a, const wifi_ap_record_t& b) {
|
||||
return a.rssi > b.rssi;
|
||||
});
|
||||
|
||||
auto& ssid_manager = SsidManager::GetInstance();
|
||||
auto ssid_list = ssid_manager.GetSsidList();
|
||||
for (int i = 0; i < ap_num; i++) {
|
||||
auto ap_record = ap_records[i];
|
||||
auto it = std::find_if(ssid_list.begin(), ssid_list.end(), [ap_record](const SsidItem& item) {
|
||||
return strcmp((char *)ap_record.ssid, item.ssid.c_str()) == 0;
|
||||
});
|
||||
if (it != ssid_list.end()) {
|
||||
ESP_LOGI(TAG, "Found AP: %s, BSSID: %02x:%02x:%02x:%02x:%02x:%02x, RSSI: %d, Channel: %d, Authmode: %d",
|
||||
(char *)ap_record.ssid,
|
||||
ap_record.bssid[0], ap_record.bssid[1], ap_record.bssid[2],
|
||||
ap_record.bssid[3], ap_record.bssid[4], ap_record.bssid[5],
|
||||
ap_record.rssi, ap_record.primary, ap_record.authmode);
|
||||
WifiApRecord record = {
|
||||
.ssid = it->ssid,
|
||||
.password = it->password,
|
||||
.channel = ap_record.primary,
|
||||
.authmode = ap_record.authmode
|
||||
};
|
||||
memcpy(record.bssid, ap_record.bssid, 6);
|
||||
connect_queue_.push_back(record);
|
||||
}
|
||||
}
|
||||
free(ap_records);
|
||||
|
||||
if (connect_queue_.empty()) {
|
||||
ESP_LOGI(TAG, "Wait for next scan");
|
||||
esp_timer_start_once(timer_handle_, 10 * 1000);
|
||||
return;
|
||||
}
|
||||
|
||||
StartConnect();
|
||||
}
|
||||
|
||||
void WifiStation::StartConnect() {
|
||||
auto ap_record = connect_queue_.front();
|
||||
connect_queue_.erase(connect_queue_.begin());
|
||||
ssid_ = ap_record.ssid;
|
||||
password_ = ap_record.password;
|
||||
|
||||
if (on_connect_) {
|
||||
on_connect_(ssid_);
|
||||
}
|
||||
|
||||
wifi_config_t wifi_config;
|
||||
bzero(&wifi_config, sizeof(wifi_config));
|
||||
strcpy((char *)wifi_config.sta.ssid, ap_record.ssid.c_str());
|
||||
strcpy((char *)wifi_config.sta.password, ap_record.password.c_str());
|
||||
if (remember_bssid_) {
|
||||
wifi_config.sta.channel = ap_record.channel;
|
||||
memcpy(wifi_config.sta.bssid, ap_record.bssid, 6);
|
||||
wifi_config.sta.bssid_set = true;
|
||||
}
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
||||
|
||||
reconnect_count_ = 0;
|
||||
ESP_ERROR_CHECK(esp_wifi_connect());
|
||||
}
|
||||
|
||||
int8_t WifiStation::GetRssi() {
|
||||
// Get station info
|
||||
wifi_ap_record_t ap_info;
|
||||
esp_err_t err = esp_wifi_sta_get_ap_info(&ap_info);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "GetRssi failed: %s", esp_err_to_name(err));
|
||||
return -127;
|
||||
}
|
||||
return ap_info.rssi;
|
||||
}
|
||||
|
||||
uint8_t WifiStation::GetChannel() {
|
||||
// Get station info
|
||||
wifi_ap_record_t ap_info;
|
||||
esp_err_t err = esp_wifi_sta_get_ap_info(&ap_info);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "GetChannel failed: %s", esp_err_to_name(err));
|
||||
return 0;
|
||||
}
|
||||
return ap_info.primary;
|
||||
}
|
||||
|
||||
bool WifiStation::IsConnected() {
|
||||
return xEventGroupGetBits(event_group_) & WIFI_EVENT_CONNECTED;
|
||||
}
|
||||
|
||||
void WifiStation::SetPowerSaveMode(bool enabled) {
|
||||
if (!wifi_started_) {
|
||||
ESP_LOGI(TAG, "Skip WiFi power-save change because station is not started");
|
||||
return;
|
||||
}
|
||||
ESP_ERROR_CHECK(esp_wifi_set_ps(enabled ? WIFI_PS_MIN_MODEM : WIFI_PS_NONE));
|
||||
}
|
||||
|
||||
// Static event handler functions
|
||||
void WifiStation::WifiEventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
|
||||
auto* this_ = static_cast<WifiStation*>(arg);
|
||||
if (event_id == WIFI_EVENT_STA_START) {
|
||||
esp_wifi_scan_start(nullptr, false);
|
||||
if (this_->on_scan_begin_) {
|
||||
this_->on_scan_begin_();
|
||||
}
|
||||
} else if (event_id == WIFI_EVENT_SCAN_DONE) {
|
||||
this_->HandleScanResult();
|
||||
} else if (event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
xEventGroupClearBits(this_->event_group_, WIFI_EVENT_CONNECTED);
|
||||
if (this_->reconnect_count_ < WifiStation::kMaxReconnectCount) {
|
||||
esp_wifi_connect();
|
||||
this_->reconnect_count_++;
|
||||
ESP_LOGI(
|
||||
TAG,
|
||||
"Reconnecting %s (attempt %d / %d)",
|
||||
this_->ssid_.c_str(),
|
||||
this_->reconnect_count_,
|
||||
WifiStation::kMaxReconnectCount
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this_->connect_queue_.empty()) {
|
||||
this_->StartConnect();
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "No more AP to connect, wait for next scan");
|
||||
esp_timer_start_once(this_->timer_handle_, 10 * 1000);
|
||||
} else if (event_id == WIFI_EVENT_STA_CONNECTED) {
|
||||
}
|
||||
}
|
||||
|
||||
void WifiStation::IpEventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
|
||||
auto* this_ = static_cast<WifiStation*>(arg);
|
||||
auto* event = static_cast<ip_event_got_ip_t*>(event_data);
|
||||
|
||||
char ip_address[16];
|
||||
esp_ip4addr_ntoa(&event->ip_info.ip, ip_address, sizeof(ip_address));
|
||||
this_->ip_address_ = ip_address;
|
||||
ESP_LOGI(TAG, "Got IP: %s", this_->ip_address_.c_str());
|
||||
|
||||
xEventGroupSetBits(this_->event_group_, WIFI_EVENT_CONNECTED);
|
||||
if (this_->on_connected_) {
|
||||
this_->on_connected_(this_->ssid_);
|
||||
}
|
||||
this_->connect_queue_.clear();
|
||||
this_->reconnect_count_ = 0;
|
||||
}
|
||||
Reference in New Issue
Block a user