feat(check-wifi): check wifi in setup mode before reboot
This commit is contained in:
parent
802035d38b
commit
1b5b3cd8ca
@ -50,11 +50,6 @@ On **first boot**, the device creates a captive Wi-Fi access point:
|
||||
|
||||
That IP becomes your **pump control dashboard**
|
||||
|
||||
> [!WARNING]
|
||||
>
|
||||
> If you enter wrong credentials, the device will **not** connect.
|
||||
> You’ll need to [**factory reset**](#factory_reset) and try again.
|
||||
|
||||
---
|
||||
|
||||
|
||||
|
||||
@ -652,7 +652,7 @@
|
||||
const payload = { ssid: ssid, password: password };
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 5000);
|
||||
const timeoutId = setTimeout(() => controller.abort(), 15000);
|
||||
const response = await fetch('/settings', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
@ -660,6 +660,8 @@
|
||||
signal: controller.signal
|
||||
});
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
showToast(`Wi-Fi credentials sent to device`, false);
|
||||
if (!response.ok) {
|
||||
let errorText = `Error ${response.status}`;
|
||||
try {
|
||||
@ -669,12 +671,16 @@
|
||||
throw new Error(errorText);
|
||||
}
|
||||
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 = "";
|
||||
} catch (error) {
|
||||
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}`;
|
||||
showToast(errMsg, true);
|
||||
}
|
||||
|
||||
16
main/main.c
16
main/main.c
@ -137,7 +137,7 @@ static const error_info_t error_table[] = {
|
||||
{APP_ERR_ADC_CALIB_FAIL, "ADC calibration failed", false},
|
||||
{APP_ERR_PUMP_INIT_FAIL, "Pump GPIO 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_HTTP_SERVER_START_FAIL, "HTTP server start failed", false},
|
||||
{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));
|
||||
} 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;
|
||||
@ -907,7 +907,7 @@ static esp_err_t setup_set_settings_handler(httpd_req_t *req) {
|
||||
|
||||
if (receive_http_content(req, &content) != ESP_OK) {
|
||||
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;
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
free(content);
|
||||
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;
|
||||
}
|
||||
free(content);
|
||||
@ -925,7 +925,7 @@ static esp_err_t setup_set_settings_handler(httpd_req_t *req) {
|
||||
// Проверяем, что пароль не пустой для защищенных сетей
|
||||
if (strlen(ssid) == 0) {
|
||||
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;
|
||||
}
|
||||
|
||||
@ -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_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));
|
||||
|
||||
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();
|
||||
} else {
|
||||
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));
|
||||
|
||||
ESP_LOGW(TAG, "WiFi test FAILED for SSID: %s", ssid);
|
||||
}
|
||||
|
||||
return wifi_ok ? ESP_OK : ESP_FAIL;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user