optimize AP mode
Some checks failed
ESP-IDF Build / build (esp32c6, release-v5.4) (push) Failing after 6m44s
ESP-IDF Build / build (esp32c6, release-v5.5) (push) Failing after 3m59s
ESP-IDF Build / build (esp32s3, release-v5.4) (push) Failing after 3m51s
ESP-IDF Build / build (esp32s3, release-v5.5) (push) Failing after 3m52s

- save wifi data
- show status led

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2026-01-15 00:36:19 +01:00
parent bccfb80791
commit 1fbc28a628
14 changed files with 213 additions and 257 deletions

View File

@@ -12,4 +12,5 @@ idf_component_register(SRCS
esp_event
json
simulator
persistence-manager
)

View File

@@ -5,6 +5,7 @@
#include <esp_http_server.h>
#include <esp_log.h>
#include <esp_wifi.h>
#include <persistence_manager.h>
#include <string.h>
#include <sys/stat.h>
@@ -116,6 +117,17 @@ esp_err_t api_wifi_scan_handler(httpd_req_t *req)
return res;
}
static void reboot_task(void *param)
{
vTaskDelay(pdMS_TO_TICKS(100));
esp_restart();
}
static bool is_valid(const cJSON *string)
{
return string && cJSON_IsString(string) && string->valuestring && strlen(string->valuestring) > 0;
}
esp_err_t api_wifi_config_handler(httpd_req_t *req)
{
ESP_LOGI(TAG, "POST /api/wifi/config");
@@ -128,12 +140,22 @@ esp_err_t api_wifi_config_handler(httpd_req_t *req)
}
buf[ret] = '\0';
// Passwort maskieren
cJSON *json = cJSON_Parse(buf);
if (json)
{
cJSON *ssid = cJSON_GetObjectItem(json, "ssid");
cJSON *pw = cJSON_GetObjectItem(json, "password");
if (pw && cJSON_IsString(pw) && pw->valuestring)
if (is_valid(ssid) && is_valid(pw))
{
persistence_manager_t pm;
if (persistence_manager_init(&pm, "wifi_config") == ESP_OK)
{
persistence_manager_set_string(&pm, "ssid", ssid->valuestring);
persistence_manager_set_string(&pm, "password", pw->valuestring);
persistence_manager_deinit(&pm);
}
}
if (is_valid(pw))
{
size_t pwlen = strlen(pw->valuestring);
char *masked = malloc(pwlen + 1);
@@ -163,7 +185,9 @@ esp_err_t api_wifi_config_handler(httpd_req_t *req)
ESP_LOGI(TAG, "Received WiFi config: %s", buf);
}
// TODO: Parse JSON and connect to WiFi
// Define a reboot task function
xTaskCreate(reboot_task, "reboot_task", 2048, NULL, 5, NULL);
set_cors_headers(req);
httpd_resp_set_status(req, "200 OK");
return httpd_resp_sendstr(req, "{\"status\":\"ok\"}");

View File

@@ -18,7 +18,7 @@ static void ws_clients_init(void)
}
// Add a client to the list
static void add_client(int fd)
static bool add_client(int fd)
{
for (int i = 0; i < WS_MAX_CLIENTS; i++)
{
@@ -27,10 +27,11 @@ static void add_client(int fd)
ws_clients[i] = fd;
ws_client_count++;
ESP_LOGI(TAG, "WebSocket client connected: fd=%d (total: %d)", fd, ws_client_count);
return;
return true;
}
}
ESP_LOGW(TAG, "Max WebSocket clients reached, cannot add fd=%d", fd);
return false;
}
// Remove a client from the list
@@ -83,6 +84,13 @@ esp_err_t websocket_handler(httpd_req_t *req)
{
// This is the handshake
ESP_LOGI(TAG, "WebSocket handshake");
int fd = httpd_req_to_sockfd(req);
if (!add_client(fd))
{
// Zu viele Clients, Verbindung schließen
httpd_sess_trigger_close(req->handle, fd);
return ESP_FAIL;
}
return ESP_OK;
}