feat(check-wifi): check wifi in setup mode before reboot

This commit is contained in:
thek4n 2026-06-09 16:24:54 +03:00
parent 802035d38b
commit 1b5b3cd8ca
3 changed files with 18 additions and 17 deletions

View File

@ -50,11 +50,6 @@ On **first boot**, the device creates a captive Wi-Fi access point:
That IP becomes your **pump control dashboard** That IP becomes your **pump control dashboard**
> [!WARNING]
>
> If you enter wrong credentials, the device will **not** connect.
> Youll need to [**factory reset**](#factory_reset) and try again.
--- ---

View File

@ -652,7 +652,7 @@
const payload = { ssid: ssid, password: password }; const payload = { ssid: ssid, password: password };
try { try {
const controller = new AbortController(); const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000); const timeoutId = setTimeout(() => controller.abort(), 15000);
const response = await fetch('/settings', { const response = await fetch('/settings', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
@ -660,6 +660,8 @@
signal: controller.signal signal: controller.signal
}); });
clearTimeout(timeoutId); clearTimeout(timeoutId);
showToast(`Wi-Fi credentials sent to device`, false);
if (!response.ok) { if (!response.ok) {
let errorText = `Error ${response.status}`; let errorText = `Error ${response.status}`;
try { try {
@ -669,12 +671,16 @@
throw new Error(errorText); throw new Error(errorText);
} }
const result = await response.json(); const result = await response.json();
console.log('Wi-Fi settings saved:', result);
showToast(`✓ Wi-Fi credentials sent to device`, false); if (!result.success) {
throw new Error(result.message);
}
showToast(result.message, false);
passwordInput.value = ""; passwordInput.value = "";
} catch (error) { } catch (error) {
let errMsg = "✗ Connection failed"; let errMsg = "✗ Connection failed";
if (error.name === 'AbortError') errMsg = "✗ Request timeout (5s)"; if (error.name === 'AbortError') errMsg = "✗ Request timeout (15s)";
else if (error.message) errMsg = `✗ ${error.message}`; else if (error.message) errMsg = `✗ ${error.message}`;
showToast(errMsg, true); showToast(errMsg, true);
} }

View File

@ -137,7 +137,7 @@ static const error_info_t error_table[] = {
{APP_ERR_ADC_CALIB_FAIL, "ADC calibration failed", false}, {APP_ERR_ADC_CALIB_FAIL, "ADC calibration failed", false},
{APP_ERR_PUMP_INIT_FAIL, "Pump GPIO initialization failed", true}, {APP_ERR_PUMP_INIT_FAIL, "Pump GPIO initialization failed", true},
{APP_ERR_WIFI_INIT_FAIL, "WiFi initialization failed", true}, {APP_ERR_WIFI_INIT_FAIL, "WiFi initialization failed", true},
{APP_ERR_WIFI_CONNECT_FAIL, "WiFi connection failed", false}, {APP_ERR_WIFI_CONNECT_FAIL, "WiFi connection failed", true},
{APP_ERR_MDNS_INIT_FAIL, "mDNS service initialization failed", false}, {APP_ERR_MDNS_INIT_FAIL, "mDNS service initialization failed", false},
{APP_ERR_HTTP_SERVER_START_FAIL, "HTTP server start failed", false}, {APP_ERR_HTTP_SERVER_START_FAIL, "HTTP server start failed", false},
{APP_ERR_TASK_CREATE_FAIL, "Task creation failed", true}, {APP_ERR_TASK_CREATE_FAIL, "Task creation failed", true},
@ -572,7 +572,7 @@ bool test_wifi_credentials(const char* ssid, const char* password, int timeout_m
vTaskDelay(pdMS_TO_TICKS(500)); vTaskDelay(pdMS_TO_TICKS(500));
} else { } else {
ESP_LOGE(TAG, "✗ Failed to connect to %s", ssid); ESP_LOGW(TAG, "✗ Failed to connect to %s", ssid);
} }
g_wifi_test_in_progress = false; g_wifi_test_in_progress = false;
@ -907,7 +907,7 @@ static esp_err_t setup_set_settings_handler(httpd_req_t *req) {
if (receive_http_content(req, &content) != ESP_OK) { if (receive_http_content(req, &content) != ESP_OK) {
httpd_resp_set_type(req, "application/json"); httpd_resp_set_type(req, "application/json");
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "{\"status\":\"error\",\"message\":\"Failed to receive content\"}"); httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "{\"success\":false,\"message\":\"Failed to receive content\"}");
return ESP_FAIL; return ESP_FAIL;
} }
@ -917,7 +917,7 @@ static esp_err_t setup_set_settings_handler(httpd_req_t *req) {
if (parse_wifi_settings_json(content, ssid, password) != ESP_OK) { if (parse_wifi_settings_json(content, ssid, password) != ESP_OK) {
free(content); free(content);
httpd_resp_set_type(req, "application/json"); httpd_resp_set_type(req, "application/json");
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "{\"status\":\"error\",\"message\":\"Missing or invalid 'ssid' or 'password' parameters\"}"); httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "{\"success\":false,\"message\":\"Missing or invalid 'ssid' or 'password' parameters\"}");
return ESP_FAIL; return ESP_FAIL;
} }
free(content); free(content);
@ -925,7 +925,7 @@ static esp_err_t setup_set_settings_handler(httpd_req_t *req) {
// Проверяем, что пароль не пустой для защищенных сетей // Проверяем, что пароль не пустой для защищенных сетей
if (strlen(ssid) == 0) { if (strlen(ssid) == 0) {
httpd_resp_set_type(req, "application/json"); httpd_resp_set_type(req, "application/json");
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "{\"status\":\"error\",\"message\":\"SSID cannot be empty\"}"); httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "{\"success\":false,\"message\":\"SSID cannot be empty\"}");
return ESP_FAIL; return ESP_FAIL;
} }
@ -948,7 +948,7 @@ static esp_err_t setup_set_settings_handler(httpd_req_t *req) {
httpd_resp_set_type(req, "application/json"); httpd_resp_set_type(req, "application/json");
httpd_resp_set_status(req, HTTPD_200); httpd_resp_set_status(req, HTTPD_200);
const char *success_response = "{\"status\":\"success\",\"message\":\"WiFi connected successfully! Rebooting in 2 seconds...\"}"; const char *success_response = "{\"success\":true,\"message\":\"WiFi connected successfully! Rebooting in 2 seconds...\"}";
httpd_resp_send(req, success_response, strlen(success_response)); httpd_resp_send(req, success_response, strlen(success_response));
ESP_LOGI(TAG, "WiFi test SUCCESSFUL, rebooting..."); ESP_LOGI(TAG, "WiFi test SUCCESSFUL, rebooting...");
@ -956,13 +956,13 @@ static esp_err_t setup_set_settings_handler(httpd_req_t *req) {
esp_restart(); esp_restart();
} else { } else {
httpd_resp_set_type(req, "application/json"); httpd_resp_set_type(req, "application/json");
const char *error_response = "{\"status\":\"error\",\"message\":\"Failed to connect to WiFi. Please check SSID and password.\"}"; const char *error_response = "{\"success\":false,\"message\":\"Failed to connect to WiFi. Please check SSID and password.\"}";
httpd_resp_send(req, error_response, strlen(error_response)); httpd_resp_send(req, error_response, strlen(error_response));
ESP_LOGW(TAG, "WiFi test FAILED for SSID: %s", ssid); ESP_LOGW(TAG, "WiFi test FAILED for SSID: %s", ssid);
} }
return wifi_ok ? ESP_OK : ESP_FAIL; return ESP_OK;
} }