add wifi setup mode
This commit is contained in:
parent
ef4cdc53a2
commit
2869d167a8
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,4 +2,5 @@ build/
|
||||
sdkconfig
|
||||
sdkconfig.old
|
||||
main/frontend.h
|
||||
main/setup_frontend.h
|
||||
managed_components/
|
||||
|
||||
@ -313,7 +313,6 @@
|
||||
</div>
|
||||
<div class="pump-state" id="currentPumpState"></div>
|
||||
|
||||
|
||||
<div class="sliders-panel">
|
||||
<div class="slider-group">
|
||||
<label>
|
||||
|
||||
353
assets/setup.html
Normal file
353
assets/setup.html
Normal file
@ -0,0 +1,353 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
|
||||
<title>Wi-Fi Setup</title>
|
||||
<style>
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
min-height: 100vh;
|
||||
background: radial-gradient(circle at 20% 30%, #121826, #0b0f17);
|
||||
font-family: 'Segoe UI', 'Inter', system-ui, -apple-system, 'Roboto', sans-serif;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.card {
|
||||
max-width: 500px;
|
||||
width: 100%;
|
||||
background: rgba(18, 22, 30, 0.75);
|
||||
backdrop-filter: blur(8px);
|
||||
border-radius: 72px 72px 56px 56px;
|
||||
box-shadow: 0 25px 45px rgba(0, 0, 0, 0.6), inset 0 1px 0 rgba(255, 255, 255, 0.08);
|
||||
padding: 32px 28px 40px;
|
||||
transition: all 0.2s ease;
|
||||
border: 1px solid rgba(71, 85, 105, 0.5);
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.icon-wifi {
|
||||
margin: 0 auto 20px;
|
||||
background: #1e2f3f;
|
||||
width: 80px;
|
||||
height: 80px;
|
||||
border-radius: 50%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
box-shadow: 0 8px 20px rgba(0,0,0,0.5), inset 0 1px 0 rgba(255,255,255,0.1);
|
||||
}
|
||||
.icon-wifi svg {
|
||||
width: 48px;
|
||||
height: 48px;
|
||||
filter: drop-shadow(0 2px 2px rgba(0,0,0,0.3));
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 1.8rem;
|
||||
font-weight: 700;
|
||||
margin: 0 0 8px;
|
||||
background: linear-gradient(135deg, #eef4ff, #b9d0f0);
|
||||
-webkit-background-clip: text;
|
||||
background-clip: text;
|
||||
color: transparent;
|
||||
letter-spacing: -0.3px;
|
||||
}
|
||||
|
||||
.sub {
|
||||
color: #8ba3c2;
|
||||
margin-bottom: 32px;
|
||||
font-size: 0.85rem;
|
||||
border-bottom: 1px dashed #2a3a4a;
|
||||
display: inline-block;
|
||||
padding-bottom: 6px;
|
||||
}
|
||||
|
||||
.input-group {
|
||||
margin-bottom: 24px;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.input-group label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
font-weight: 600;
|
||||
color: #e0eafc;
|
||||
font-size: 1rem;
|
||||
margin-bottom: 10px;
|
||||
letter-spacing: 0.3px;
|
||||
}
|
||||
|
||||
.input-group label svg {
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
opacity: 0.8;
|
||||
}
|
||||
|
||||
.input-group input {
|
||||
width: 100%;
|
||||
background: #0e141f;
|
||||
border: 1.5px solid #2a3a4f;
|
||||
border-radius: 60px;
|
||||
padding: 14px 20px;
|
||||
font-size: 1rem;
|
||||
color: #eef4ff;
|
||||
font-family: 'Segoe UI', monospace;
|
||||
transition: all 0.2s;
|
||||
outline: none;
|
||||
box-shadow: inset 0 2px 5px rgba(0,0,0,0.3);
|
||||
}
|
||||
|
||||
.input-group input:focus {
|
||||
border-color: #3b82f6;
|
||||
box-shadow: 0 0 0 3px rgba(59,130,246,0.3), inset 0 1px 2px #000;
|
||||
}
|
||||
|
||||
.input-group input::placeholder {
|
||||
color: #4c6180;
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
.save-btn {
|
||||
width: 100%;
|
||||
background: linear-gradient(95deg, #1f5a3a, #2b7a55);
|
||||
border: none;
|
||||
padding: 16px;
|
||||
font-size: 1.35rem;
|
||||
font-weight: bold;
|
||||
color: #f0faf5;
|
||||
border-radius: 60px;
|
||||
margin-top: 18px;
|
||||
cursor: pointer;
|
||||
box-shadow: 0 6px 0 #0d3522;
|
||||
transition: transform 0.08s, box-shadow 0.08s, background 0.1s;
|
||||
letter-spacing: 1px;
|
||||
font-family: inherit;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.save-btn:active {
|
||||
transform: translateY(3px);
|
||||
box-shadow: 0 3px 0 #0d3522;
|
||||
background: linear-gradient(95deg, #154d33, #216a48);
|
||||
}
|
||||
|
||||
.toast-msg {
|
||||
visibility: hidden;
|
||||
background: #1f2f3cee;
|
||||
backdrop-filter: blur(20px);
|
||||
color: #c2f0d6;
|
||||
text-align: center;
|
||||
border-radius: 60px;
|
||||
padding: 12px 28px;
|
||||
position: fixed;
|
||||
bottom: 32px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
opacity: 0;
|
||||
transition: opacity 0.2s ease;
|
||||
z-index: 1000;
|
||||
pointer-events: none;
|
||||
white-space: nowrap;
|
||||
font-weight: 600;
|
||||
border: 1px solid #3c936e;
|
||||
box-shadow: 0 6px 16px black;
|
||||
}
|
||||
|
||||
.toast-msg.show {
|
||||
visibility: visible;
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.hint {
|
||||
font-size: 0.7rem;
|
||||
color: #6f8aae;
|
||||
margin-top: 24px;
|
||||
border-top: 1px solid rgba(72, 92, 118, 0.4);
|
||||
padding-top: 20px;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
@media (max-width: 500px) {
|
||||
.card { padding: 24px 20px 32px; }
|
||||
h1 { font-size: 1.5rem; }
|
||||
.save-btn { font-size: 1.2rem; }
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="card">
|
||||
<div class="icon-wifi">
|
||||
<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<path d="M12 18C13.1046 18 14 17.1046 14 16C14 14.8954 13.1046 14 12 14C10.8954 14 10 14.8954 10 16C10 17.1046 10.8954 18 12 18Z" fill="#6bcfb8"/>
|
||||
<path d="M16.95 11.05C15.8 9.9 14.2 9.2 12.5 9.2C10.8 9.2 9.2 9.9 8.05 11.05L9.45 12.45C10.25 11.65 11.35 11.2 12.5 11.2C13.65 11.2 14.75 11.65 15.55 12.45L16.95 11.05Z" fill="#6bcfb8"/>
|
||||
<path d="M20.2 7.8C18.3 5.9 15.5 4.8 12.5 4.8C9.5 4.8 6.7 5.9 4.8 7.8L6.2 9.2C7.7 7.7 9.9 6.8 12.5 6.8C15.1 6.8 17.3 7.7 18.8 9.2L20.2 7.8Z" fill="#6bcfb8"/>
|
||||
<path d="M12 2C7.6 2 3.7 3.9 1 7L12 20L23 7C20.3 3.9 16.4 2 12 2Z" fill="#6bcfb8" fill-opacity="0.4"/>
|
||||
</svg>
|
||||
</div>
|
||||
<h1>Подключение к Wi‑Fi</h1>
|
||||
<div class="sub">настройте доступ к сети</div>
|
||||
|
||||
<div class="input-group">
|
||||
<label>
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
|
||||
<rect x="2" y="3" width="20" height="14" rx="2" ry="2"></rect>
|
||||
<line x1="8" y1="21" x2="16" y2="21"></line>
|
||||
<line x1="12" y1="17" x2="12" y2="21"></line>
|
||||
</svg>
|
||||
SSID (имя сети)
|
||||
</label>
|
||||
<input type="text" id="ssidInput" placeholder="Например: MyHomeWiFi" autocomplete="off">
|
||||
</div>
|
||||
|
||||
<div class="input-group">
|
||||
<label>
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
|
||||
<rect x="5" y="11" width="14" height="10" rx="2" ry="2"></rect>
|
||||
<path d="M8 11V8a4 4 0 0 1 8 0v3"></path>
|
||||
</svg>
|
||||
Пароль
|
||||
</label>
|
||||
<input type="password" id="passwordInput" placeholder="··········" autocomplete="off">
|
||||
</div>
|
||||
|
||||
<button class="save-btn" id="saveBtn">
|
||||
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2">
|
||||
<path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"></path>
|
||||
<polyline points="17 21 17 13 7 13 7 21"></polyline>
|
||||
<polyline points="7 3 7 8 15 8"></polyline>
|
||||
</svg>
|
||||
Save settings
|
||||
</button>
|
||||
<div class="hint">
|
||||
<span>⚡IoT контроллер давления | © Vladislav Kan <thek4n@yandex.ru></span>
|
||||
</div>
|
||||
</div>
|
||||
<div id="toastMsg" class="toast-msg">✓ Wi-Fi settings sent</div>
|
||||
|
||||
<script>
|
||||
(function() {
|
||||
const ssidInput = document.getElementById('ssidInput');
|
||||
const passwordInput = document.getElementById('passwordInput');
|
||||
const saveBtn = document.getElementById('saveBtn');
|
||||
const toast = document.getElementById('toastMsg');
|
||||
|
||||
let saveTimeout = null;
|
||||
|
||||
function showToast(message, isError = false) {
|
||||
toast.innerText = message;
|
||||
if (isError) {
|
||||
toast.style.borderColor = '#c93a3a';
|
||||
toast.style.color = '#ffc2c2';
|
||||
} else {
|
||||
toast.style.borderColor = '#3c936e';
|
||||
toast.style.color = '#c2f0d6';
|
||||
}
|
||||
toast.classList.add('show');
|
||||
setTimeout(() => {
|
||||
toast.classList.remove('show');
|
||||
setTimeout(() => {
|
||||
toast.style.borderColor = '#3c936e';
|
||||
toast.style.color = '#c2f0d6';
|
||||
}, 300);
|
||||
}, 1800);
|
||||
}
|
||||
|
||||
async function sendWiFiSettings() {
|
||||
let ssid = ssidInput.value.trim();
|
||||
let password = passwordInput.value;
|
||||
|
||||
if (ssid === "") {
|
||||
showToast("✗ Укажите SSID", true);
|
||||
ssidInput.style.borderColor = "#e94f32";
|
||||
setTimeout(() => ssidInput.style.borderColor = "", 1000);
|
||||
return;
|
||||
}
|
||||
|
||||
const payload = {
|
||||
ssid: ssid,
|
||||
password: password
|
||||
};
|
||||
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 5000);
|
||||
|
||||
const response = await fetch('/settings', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
signal: controller.signal
|
||||
});
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (!response.ok) {
|
||||
let errorText = `Ошибка ${response.status}`;
|
||||
try {
|
||||
const errData = await response.json();
|
||||
errorText = errData.message || errData.error || errorText;
|
||||
} catch(e) {}
|
||||
throw new Error(errorText);
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
console.log('Wi-Fi settings saved:', result);
|
||||
showToast("✓ SSID and password sent!");
|
||||
|
||||
passwordInput.value = "";
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error sending Wi-Fi settings:', error);
|
||||
let errMsg = "✗ Connection error";
|
||||
if (error.name === 'AbortError') errMsg = "✗ timeout";
|
||||
else if (error.message) errMsg = `✗ ${error.message}`;
|
||||
showToast(errMsg, true);
|
||||
}
|
||||
}
|
||||
|
||||
function debounceSend() {
|
||||
if (saveTimeout) clearTimeout(saveTimeout);
|
||||
saveTimeout = setTimeout(() => {
|
||||
sendWiFiSettings();
|
||||
}, 200);
|
||||
}
|
||||
|
||||
saveBtn.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
debounceSend();
|
||||
});
|
||||
|
||||
const inputs = [ssidInput, passwordInput];
|
||||
inputs.forEach(input => {
|
||||
input.addEventListener('keypress', (e) => {
|
||||
if (e.key === 'Enter') {
|
||||
e.preventDefault();
|
||||
debounceSend();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
window.addEventListener('beforeunload', () => {
|
||||
if (saveTimeout) clearTimeout(saveTimeout);
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
2
justfile
2
justfile
@ -14,6 +14,8 @@ build:
|
||||
. "{{ESP_IDF}}/export.sh"
|
||||
xxd -i assets/index.html > main/frontend.h
|
||||
sed -i '1i\static const ' main/frontend.h
|
||||
xxd -i assets/setup.html > main/setup_frontend.h
|
||||
sed -i '1i\static const ' main/setup_frontend.h
|
||||
idf.py build
|
||||
|
||||
|
||||
|
||||
331
main/main.c
331
main/main.c
@ -21,10 +21,16 @@
|
||||
#include "esp_adc/adc_cali_scheme.h"
|
||||
#include "esp_task_wdt.h"
|
||||
#include "mdns.h"
|
||||
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "lwip/inet.h"
|
||||
|
||||
#include "sdkconfig.h"
|
||||
#include "frontend.h"
|
||||
#include "setup_frontend.h"
|
||||
|
||||
#if !defined(CONFIG_PUMP_PIN)
|
||||
#error "CONFIG_PUMP_PIN must be defined in menuconfig"
|
||||
@ -60,6 +66,19 @@
|
||||
|
||||
#define MDNS_DOMAIN "pumpctl"
|
||||
|
||||
#define RESET_BTN_GPIO GPIO_NUM_15
|
||||
#define LED_GPIO GPIO_NUM_2
|
||||
|
||||
#define WIFI_SSID_MAX_LEN 32
|
||||
#define WIFI_PASS_MAX_LEN 64
|
||||
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
static EventGroupHandle_t wifi_event_group;
|
||||
static int retry_count = 0;
|
||||
static const int MAX_RETRY_COUNT = 5;
|
||||
|
||||
static adc_oneshot_unit_handle_t adc_handle;
|
||||
static adc_cali_handle_t cali_handle;
|
||||
static bool is_calibrated = false;
|
||||
@ -526,6 +545,123 @@ static void vHttpServerTask(void *pvParameters) {
|
||||
}
|
||||
}
|
||||
|
||||
static esp_err_t setup_get_handler(httpd_req_t *req) {
|
||||
httpd_resp_set_type(req, "text/html; charset=utf-8");
|
||||
httpd_resp_send(req, (const char*)assets_setup_html, HTTPD_RESP_USE_STRLEN);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void save_wifi_config(const char *ssid, const char *password) {
|
||||
nvs_handle_t nvs_handle;
|
||||
esp_err_t err = nvs_open(NVS_PARTITION, NVS_READWRITE, &nvs_handle);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Error opening NVS");
|
||||
return;
|
||||
}
|
||||
|
||||
nvs_set_str(nvs_handle, "wifi_ssid", ssid);
|
||||
nvs_set_str(nvs_handle, "wifi_pass", password);
|
||||
nvs_commit(nvs_handle);
|
||||
nvs_close(nvs_handle);
|
||||
|
||||
ESP_LOGI(TAG, "WiFi config saved: SSID=%s", ssid);
|
||||
}
|
||||
|
||||
|
||||
static esp_err_t parse_wifi_settings_json(const char *content, char *ssid, char *password) {
|
||||
cJSON *json = cJSON_Parse(content);
|
||||
if (!json) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
cJSON *ssid_item = cJSON_GetObjectItem(json, "ssid");
|
||||
cJSON *pass_item = cJSON_GetObjectItem(json, "password");
|
||||
|
||||
if (!ssid_item || !pass_item ||
|
||||
ssid_item->type != cJSON_String ||
|
||||
pass_item->type != cJSON_String) {
|
||||
cJSON_Delete(json);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
if (strlen(ssid_item->valuestring) >= WIFI_SSID_MAX_LEN ||
|
||||
strlen(pass_item->valuestring) >= WIFI_PASS_MAX_LEN) {
|
||||
cJSON_Delete(json);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
strlcpy(ssid, ssid_item->valuestring, WIFI_SSID_MAX_LEN);
|
||||
strlcpy(password, pass_item->valuestring, WIFI_PASS_MAX_LEN);
|
||||
|
||||
cJSON_Delete(json);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t setup_set_settings_handler(httpd_req_t *req) {
|
||||
char *content = NULL;
|
||||
if (receive_http_content(req, &content) != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
char ssid[WIFI_SSID_MAX_LEN];
|
||||
char password[WIFI_PASS_MAX_LEN];
|
||||
if (parse_wifi_settings_json(content, ssid, password) != ESP_OK) {
|
||||
free(content);
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Missing or invalid 'ssid' or 'password' parameters");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
free(content);
|
||||
|
||||
|
||||
send_json_response(req, "{\"success\":true}");
|
||||
|
||||
save_wifi_config(ssid, password);
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
esp_restart();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void vSetupHttpServerTask(void *pvParameters) {
|
||||
esp_task_wdt_add(xTaskGetCurrentTaskHandle());
|
||||
|
||||
httpd_handle_t server = NULL;
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.server_port = CONFIG_WEBINTERFACE_PORT;
|
||||
config.max_uri_handlers = 10;
|
||||
config.stack_size = 4096;
|
||||
|
||||
if (httpd_start(&server, &config) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "❌ Error HTTP server running");
|
||||
} else {
|
||||
ESP_LOGI(TAG, "🚀 Setup HTTP server run on port %d", config.server_port);
|
||||
|
||||
httpd_uri_t root = {
|
||||
.uri = "/",
|
||||
.method = HTTP_GET,
|
||||
.handler = setup_get_handler,
|
||||
.user_ctx = NULL
|
||||
};
|
||||
if (httpd_register_uri_handler(server, &root) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to register GET / handler");
|
||||
}
|
||||
|
||||
httpd_uri_t settings = {
|
||||
.uri = "/settings",
|
||||
.method = HTTP_POST,
|
||||
.handler = setup_set_settings_handler,
|
||||
.user_ctx = NULL
|
||||
};
|
||||
if (httpd_register_uri_handler(server, &settings) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to register GET /settings handler");
|
||||
}
|
||||
}
|
||||
|
||||
while (1) {
|
||||
esp_task_wdt_reset();
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
}
|
||||
|
||||
static void load_thresholds_from_nvs(void) {
|
||||
nvs_handle_t my_handle;
|
||||
esp_err_t err = nvs_open(NVS_PARTITION, NVS_READWRITE, &my_handle);
|
||||
@ -557,6 +693,115 @@ static void load_thresholds_from_nvs(void) {
|
||||
nvs_close(my_handle);
|
||||
}
|
||||
|
||||
bool is_wifi_config_exists() {
|
||||
nvs_handle_t nvs_handle;
|
||||
esp_err_t err = nvs_open(NVS_PARTITION, NVS_READONLY, &nvs_handle);
|
||||
if (err != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char ssid[WIFI_SSID_MAX_LEN];
|
||||
size_t ssid_len = sizeof(ssid);
|
||||
err = nvs_get_str(nvs_handle, "wifi_ssid", ssid, &ssid_len);
|
||||
|
||||
nvs_close(nvs_handle);
|
||||
|
||||
return (err == ESP_OK);
|
||||
}
|
||||
|
||||
// Сброс настроек и очистка NVS
|
||||
void reset_settings() {
|
||||
ESP_LOGI(TAG, "Resetting settings...");
|
||||
|
||||
// Очищаем NVS
|
||||
nvs_flash_erase();
|
||||
nvs_flash_init();
|
||||
|
||||
ESP_LOGI(TAG, "Settings reset, restarting...");
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
}
|
||||
|
||||
void indicate_led() {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
gpio_set_level(LED_GPIO, 1);
|
||||
vTaskDelay(pdMS_TO_TICKS(200));
|
||||
gpio_set_level(LED_GPIO, 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(200));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Обработчик событий WiFi
|
||||
static void event_handler(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data) {
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||
esp_wifi_connect();
|
||||
ESP_LOGI(TAG, "Attempting to connect to WiFi...");
|
||||
}
|
||||
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
if (retry_count < MAX_RETRY_COUNT) {
|
||||
esp_wifi_connect();
|
||||
retry_count++;
|
||||
ESP_LOGI(TAG, "Retry connecting (%d/%d)...", retry_count, MAX_RETRY_COUNT);
|
||||
} else {
|
||||
xEventGroupSetBits(wifi_event_group, WIFI_FAIL_BIT);
|
||||
ESP_LOGE(TAG, "Failed to connect after %d retries", MAX_RETRY_COUNT);
|
||||
|
||||
esp_restart();
|
||||
}
|
||||
}
|
||||
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
|
||||
ESP_LOGI(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip));
|
||||
retry_count = 0;
|
||||
xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_connect_sta(const char* ssid, const char* password) {
|
||||
wifi_event_group = xEventGroupCreate();
|
||||
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_create_default_wifi_sta();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL);
|
||||
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL);
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
.ssid = "",
|
||||
.password = "",
|
||||
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
|
||||
.sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
|
||||
},
|
||||
};
|
||||
|
||||
strcpy((char*)wifi_config.sta.ssid, ssid);
|
||||
strcpy((char*)wifi_config.sta.password, password);
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
ESP_LOGI(TAG, "WiFi STA started");
|
||||
|
||||
EventBits_t bits = xEventGroupWaitBits(wifi_event_group,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||
pdFALSE,
|
||||
pdFALSE,
|
||||
portMAX_DELAY);
|
||||
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
ESP_LOGI(TAG, "Successfully connected to WiFi!");
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
ESP_LOGE(TAG, "Failed to connect to WiFi!");
|
||||
}
|
||||
}
|
||||
|
||||
void app_main(void) {
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
@ -565,26 +810,80 @@ void app_main(void) {
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
|
||||
load_thresholds_from_nvs();
|
||||
ESP_ERROR_CHECK(adc_init());
|
||||
ESP_ERROR_CHECK(pump_init());
|
||||
wifi_init_softap();
|
||||
gpio_config_t io_conf = {
|
||||
.pin_bit_mask = (1ULL << LED_GPIO),
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = GPIO_PULLUP_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
gpio_config(&io_conf);
|
||||
gpio_set_level(LED_GPIO, 0);
|
||||
|
||||
// ESP_ERROR_CHECK(start_mdns_service());
|
||||
gpio_config_t btn_conf = {
|
||||
.pin_bit_mask = (1ULL << RESET_BTN_GPIO),
|
||||
.mode = GPIO_MODE_INPUT,
|
||||
.pull_up_en = GPIO_PULLUP_ENABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
gpio_config(&btn_conf);
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
|
||||
xTaskCreate(vReadSensorTask, "read_sensor", 2048, NULL, PRIORITY_SENSOR, NULL);
|
||||
xTaskCreate(vPumpControlTask, "pump_control", 2048, NULL, PRIORITY_CONTROL, NULL);
|
||||
xTaskCreate(vHttpServerTask, "http_server", 8192, NULL, PRIORITY_HTTP, NULL);
|
||||
if (gpio_get_level(RESET_BTN_GPIO) == 0) {
|
||||
ESP_LOGI(TAG, "RESET button pressed, resetting settings...");
|
||||
reset_settings();
|
||||
indicate_led();
|
||||
|
||||
ESP_LOGI(TAG, "=========================================");
|
||||
ESP_LOGI(TAG, "✅ Ready to work");
|
||||
ESP_LOGI(TAG, "📱 Connect to Wi-Fi: %s", CONFIG_AP_WIFI_SSID);
|
||||
ESP_LOGI(TAG, "🔑 Wi-Fi Password: %s", strlen(CONFIG_AP_WIFI_PASS) ? CONFIG_AP_WIFI_PASS : "Open network");
|
||||
ESP_LOGI(TAG, "🌐 Open browser: http://%s:%d", CONFIG_AP_IP, CONFIG_WEBINTERFACE_PORT);
|
||||
ESP_LOGI(TAG, "=========================================");
|
||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||
esp_restart();
|
||||
return;
|
||||
}
|
||||
|
||||
bool should_run_setup_mode = !is_wifi_config_exists();
|
||||
if (should_run_setup_mode) {
|
||||
wifi_init_softap();
|
||||
ESP_LOGI(TAG, "=========================================");
|
||||
ESP_LOGI(TAG, "No WiFi settings found, starting setup mode");
|
||||
ESP_LOGI(TAG, "📱 Connect to Wi-Fi: %s", CONFIG_AP_WIFI_SSID);
|
||||
ESP_LOGI(TAG, "🔑 Wi-Fi Password: %s", strlen(CONFIG_AP_WIFI_PASS) ? CONFIG_AP_WIFI_PASS : "Open network");
|
||||
ESP_LOGI(TAG, "🌐 Open browser: http://%s:%d", CONFIG_AP_IP, CONFIG_WEBINTERFACE_PORT);
|
||||
ESP_LOGI(TAG, "=========================================");
|
||||
|
||||
xTaskCreate(vSetupHttpServerTask, "setup_http_server", 8192, NULL, PRIORITY_HTTP, NULL);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "WiFi settings found, starting normal mode");
|
||||
|
||||
load_thresholds_from_nvs();
|
||||
ESP_ERROR_CHECK(adc_init());
|
||||
ESP_ERROR_CHECK(pump_init());
|
||||
|
||||
nvs_handle_t nvs_handle;
|
||||
nvs_open(NVS_PARTITION, NVS_READONLY, &nvs_handle);
|
||||
|
||||
char ssid[WIFI_SSID_MAX_LEN];
|
||||
char password[WIFI_PASS_MAX_LEN];
|
||||
size_t ssid_len = sizeof(ssid);
|
||||
size_t pass_len = sizeof(password);
|
||||
|
||||
nvs_get_str(nvs_handle, "wifi_ssid", ssid, &ssid_len);
|
||||
nvs_get_str(nvs_handle, "wifi_pass", password, &pass_len);
|
||||
nvs_close(nvs_handle);
|
||||
|
||||
ESP_LOGI(TAG, "Using saved SSID: %s", ssid);
|
||||
wifi_connect_sta(ssid, password);
|
||||
|
||||
ESP_ERROR_CHECK(start_mdns_service());
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
|
||||
xTaskCreate(vReadSensorTask, "read_sensor", 2048, NULL, PRIORITY_SENSOR, NULL);
|
||||
xTaskCreate(vPumpControlTask, "pump_control", 2048, NULL, PRIORITY_CONTROL, NULL);
|
||||
xTaskCreate(vHttpServerTask, "http_server", 8192, NULL, PRIORITY_HTTP, NULL);
|
||||
}
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user