test
This commit is contained in:
parent
16e0709a53
commit
8b679be22a
50
main/main.c
50
main/main.c
@ -824,26 +824,64 @@ static esp_err_t parse_wifi_settings_json(const char *content, char *ssid, char
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool test_wifi_credentials(const char* ssid, const char* password, int timeout_ms) {
|
||||||
|
wifi_config_t wifi_config = {0};
|
||||||
|
strncpy((char*)wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid) - 1);
|
||||||
|
strncpy((char*)wifi_config.sta.password, password, sizeof(wifi_config.sta.password) - 1);
|
||||||
|
|
||||||
|
esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
|
||||||
|
esp_wifi_start();
|
||||||
|
|
||||||
|
// Ожидание результата
|
||||||
|
EventBits_t bits = xEventGroupWaitBits(wifi_event_group,
|
||||||
|
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||||
|
pdFALSE,
|
||||||
|
pdFALSE,
|
||||||
|
timeout_ms / portTICK_PERIOD_MS);
|
||||||
|
|
||||||
|
bool success = (bits & WIFI_CONNECTED_BIT) != 0;
|
||||||
|
|
||||||
|
esp_wifi_stop();
|
||||||
|
vEventGroupDelete(wifi_event_group);
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
static esp_err_t setup_set_settings_handler(httpd_req_t *req) {
|
static esp_err_t setup_set_settings_handler(httpd_req_t *req) {
|
||||||
char *content = NULL;
|
char *content = NULL;
|
||||||
|
|
||||||
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_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "{\"message\": \"failed to receive content\"}");
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
char ssid[WIFI_SSID_MAX_LEN];
|
char ssid[WIFI_SSID_MAX_LEN];
|
||||||
char password[WIFI_PASS_MAX_LEN];
|
char password[WIFI_PASS_MAX_LEN];
|
||||||
|
|
||||||
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_send_err(req, HTTPD_400_BAD_REQUEST, "Missing or invalid 'ssid' or 'password' parameters");
|
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\"}");
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
free(content);
|
free(content);
|
||||||
|
|
||||||
send_json_response(req, "{\"success\":true}");
|
ESP_LOGI(TAG, "Testing WiFi connection to SSID: %s", ssid);
|
||||||
save_wifi_config(ssid, password);
|
bool wifi_ok = test_wifi_credentials(ssid, password, 10000);
|
||||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
||||||
esp_restart();
|
if (wifi_ok) {
|
||||||
return ESP_OK;
|
save_wifi_config(ssid, password);
|
||||||
|
|
||||||
|
send_json_response(req, "{\"success\":true,\"error\":\"Wi-Fi connected successfully. Rebooting...\"}");
|
||||||
|
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||||
|
esp_restart();
|
||||||
|
} else {
|
||||||
|
send_json_response(req, "{\"success\":false,\"error\":\"Failed to connect to WiFi mode\"}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return wifi_ok ? ESP_OK : ESP_FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user