Brings the led_strip example into the cpp app, targeting the ESP32-C3: - Access point led-XXXXXX (open) plus a Wi-Fi client configured from the web Settings tab, with credentials stored in NVS and reconnect backoff - mDNS as led.local with an HTTP service on port 80 - HTTP server with an embedded web UI and a JSON API for patterns and Wi-Fi - Pattern runner for solid, rainbow, chase, breathe, blink, pulse, transition and circle on a 119-LED WS2812 strip (GPIO 10) Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
277 lines
9.0 KiB
C++
277 lines
9.0 KiB
C++
/*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
#include <cstring>
|
|
|
|
#include "cJSON.h"
|
|
#include "esp_http_server.h"
|
|
#include "esp_log.h"
|
|
#include "esp_wifi.h"
|
|
#include "net.h"
|
|
#include "patterns.h"
|
|
#include "settings.h"
|
|
#include "web.h"
|
|
|
|
static const char *TAG = "web";
|
|
|
|
// Files embedded from web/ (see main/CMakeLists.txt EMBED_TXTFILES)
|
|
extern const char index_html_start[] asm("_binary_index_html_start");
|
|
extern const char index_html_end[] asm("_binary_index_html_end");
|
|
extern const char app_js_start[] asm("_binary_app_js_start");
|
|
extern const char app_js_end[] asm("_binary_app_js_end");
|
|
extern const char style_css_start[] asm("_binary_style_css_start");
|
|
extern const char style_css_end[] asm("_binary_style_css_end");
|
|
|
|
static httpd_handle_t s_server;
|
|
|
|
#define MAX_BODY 512
|
|
#define MAX_SCAN 20
|
|
|
|
static esp_err_t send_embedded(httpd_req_t *req, const char *type, const char *start, const char *end)
|
|
{
|
|
httpd_resp_set_type(req, type);
|
|
httpd_resp_set_hdr(req, "Cache-Control", "no-cache");
|
|
return httpd_resp_send(req, start, end - start);
|
|
}
|
|
|
|
static esp_err_t send_json(httpd_req_t *req, cJSON *obj)
|
|
{
|
|
char *s = cJSON_PrintUnformatted(obj);
|
|
cJSON_Delete(obj);
|
|
if (!s) {
|
|
return httpd_resp_send_500(req);
|
|
}
|
|
httpd_resp_set_type(req, "application/json");
|
|
esp_err_t err = httpd_resp_sendstr(req, s);
|
|
cJSON_free(s);
|
|
return err;
|
|
}
|
|
|
|
static esp_err_t send_error(httpd_req_t *req, const char *status, const char *msg)
|
|
{
|
|
httpd_resp_set_status(req, status);
|
|
cJSON *o = cJSON_CreateObject();
|
|
cJSON_AddStringToObject(o, "error", msg);
|
|
return send_json(req, o);
|
|
}
|
|
|
|
// Read the request body into buf (NUL-terminated). False if missing, too big or unreadable.
|
|
static bool read_body(httpd_req_t *req, char *buf, size_t cap)
|
|
{
|
|
int len = req->content_len;
|
|
if (len <= 0 || (size_t)len >= cap) {
|
|
return false;
|
|
}
|
|
int got = 0;
|
|
while (got < len) {
|
|
int r = httpd_req_recv(req, buf + got, len - got);
|
|
if (r <= 0) {
|
|
return false;
|
|
}
|
|
got += r;
|
|
}
|
|
buf[got] = '\0';
|
|
return true;
|
|
}
|
|
|
|
static void read_rgb(const cJSON *arr, uint8_t *out)
|
|
{
|
|
if (!cJSON_IsArray(arr) || cJSON_GetArraySize(arr) != 3) {
|
|
return;
|
|
}
|
|
for (int i = 0; i < 3; i++) {
|
|
cJSON *v = cJSON_GetArrayItem(arr, i);
|
|
if (cJSON_IsNumber(v)) {
|
|
int x = v->valueint;
|
|
out[i] = (uint8_t)(x < 0 ? 0 : x > 255 ? 255 : x);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void put_rgb(cJSON *obj, const char *key, const uint8_t *c)
|
|
{
|
|
cJSON *a = cJSON_AddArrayToObject(obj, key);
|
|
for (int i = 0; i < 3; i++) {
|
|
cJSON_AddItemToArray(a, cJSON_CreateNumber(c[i]));
|
|
}
|
|
}
|
|
|
|
// GET / , /app.js , /style.css
|
|
static esp_err_t index_get(httpd_req_t *req)
|
|
{
|
|
return send_embedded(req, "text/html", index_html_start, index_html_end);
|
|
}
|
|
|
|
static esp_err_t app_js_get(httpd_req_t *req)
|
|
{
|
|
return send_embedded(req, "application/javascript", app_js_start, app_js_end);
|
|
}
|
|
|
|
static esp_err_t style_css_get(httpd_req_t *req)
|
|
{
|
|
return send_embedded(req, "text/css", style_css_start, style_css_end);
|
|
}
|
|
|
|
// GET /api/status
|
|
static esp_err_t status_get(httpd_req_t *req)
|
|
{
|
|
pattern_params_t p = patterns_get();
|
|
char ap[16], ip[16];
|
|
net_ap_ssid(ap, sizeof(ap));
|
|
bool up = net_sta_status(ip, sizeof(ip));
|
|
|
|
cJSON *o = cJSON_CreateObject();
|
|
cJSON_AddStringToObject(o, "ap_ssid", ap);
|
|
cJSON_AddBoolToObject(o, "sta_connected", up);
|
|
cJSON_AddStringToObject(o, "sta_ip", ip);
|
|
cJSON_AddStringToObject(o, "mode", patterns_mode_name(p.mode));
|
|
cJSON *modes = cJSON_AddArrayToObject(o, "modes");
|
|
for (int i = 0; i < PM_COUNT; i++) {
|
|
cJSON_AddItemToArray(modes, cJSON_CreateString(patterns_mode_name((pattern_mode_t)i)));
|
|
}
|
|
put_rgb(o, "c1", p.c1);
|
|
put_rgb(o, "c2", p.c2);
|
|
cJSON_AddNumberToObject(o, "speed_ms", p.speed_ms);
|
|
cJSON_AddNumberToObject(o, "brightness", p.brightness);
|
|
return send_json(req, o);
|
|
}
|
|
|
|
// POST /api/pattern {"mode","c1":[r,g,b],"c2":[r,g,b],"speed_ms","brightness"}
|
|
static esp_err_t pattern_post(httpd_req_t *req)
|
|
{
|
|
char body[MAX_BODY];
|
|
if (!read_body(req, body, sizeof(body))) {
|
|
return send_error(req, "400 Bad Request", "empty or too large body");
|
|
}
|
|
cJSON *in = cJSON_Parse(body);
|
|
if (!in) {
|
|
return send_error(req, "400 Bad Request", "invalid JSON");
|
|
}
|
|
pattern_params_t p = patterns_get();
|
|
cJSON *mode = cJSON_GetObjectItem(in, "mode");
|
|
if (cJSON_IsString(mode)) {
|
|
pattern_mode_t m = patterns_mode_from_name(mode->valuestring);
|
|
if (m == PM_COUNT) {
|
|
cJSON_Delete(in);
|
|
return send_error(req, "400 Bad Request", "unknown mode");
|
|
}
|
|
p.mode = m;
|
|
}
|
|
read_rgb(cJSON_GetObjectItem(in, "c1"), p.c1);
|
|
read_rgb(cJSON_GetObjectItem(in, "c2"), p.c2);
|
|
cJSON *speed = cJSON_GetObjectItem(in, "speed_ms");
|
|
if (cJSON_IsNumber(speed)) p.speed_ms = (uint32_t)speed->valueint;
|
|
cJSON *bright = cJSON_GetObjectItem(in, "brightness");
|
|
if (cJSON_IsNumber(bright)) {
|
|
int b = bright->valueint;
|
|
p.brightness = (uint8_t)(b < 0 ? 0 : b > 255 ? 255 : b);
|
|
}
|
|
cJSON_Delete(in);
|
|
patterns_set(p);
|
|
return send_json(req, cJSON_CreateObject());
|
|
}
|
|
|
|
// GET /api/wifi : saved client SSID and connection state
|
|
static esp_err_t wifi_get(httpd_req_t *req)
|
|
{
|
|
wifi_creds_t c = {};
|
|
settings_load_wifi(&c);
|
|
char ip[16];
|
|
bool up = net_sta_status(ip, sizeof(ip));
|
|
cJSON *o = cJSON_CreateObject();
|
|
cJSON_AddStringToObject(o, "ssid", c.ssid);
|
|
cJSON_AddBoolToObject(o, "connected", up);
|
|
cJSON_AddStringToObject(o, "ip", ip);
|
|
return send_json(req, o);
|
|
}
|
|
|
|
// POST /api/wifi {"ssid","password"} : save and connect
|
|
static esp_err_t wifi_post(httpd_req_t *req)
|
|
{
|
|
char body[MAX_BODY];
|
|
if (!read_body(req, body, sizeof(body))) {
|
|
return send_error(req, "400 Bad Request", "empty or too large body");
|
|
}
|
|
cJSON *in = cJSON_Parse(body);
|
|
if (!in) {
|
|
return send_error(req, "400 Bad Request", "invalid JSON");
|
|
}
|
|
cJSON *ssid = cJSON_GetObjectItem(in, "ssid");
|
|
cJSON *pass = cJSON_GetObjectItem(in, "password");
|
|
const char *pw = cJSON_IsString(pass) ? pass->valuestring : "";
|
|
if (!cJSON_IsString(ssid) || !ssid->valuestring[0] || strlen(ssid->valuestring) > 32) {
|
|
cJSON_Delete(in);
|
|
return send_error(req, "400 Bad Request", "SSID must be 1-32 characters");
|
|
}
|
|
size_t plen = strlen(pw);
|
|
if (plen > 0 && (plen < 8 || plen > 64)) {
|
|
cJSON_Delete(in);
|
|
return send_error(req, "400 Bad Request", "password must be empty or 8-64 characters");
|
|
}
|
|
wifi_creds_t c = {};
|
|
strncpy(c.ssid, ssid->valuestring, sizeof(c.ssid) - 1);
|
|
strncpy(c.pass, pw, sizeof(c.pass) - 1);
|
|
cJSON_Delete(in);
|
|
|
|
esp_err_t err = net_sta_connect(&c);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGW(TAG, "Connect to %s failed: %s", c.ssid, esp_err_to_name(err));
|
|
return send_error(req, "500 Internal Server Error", esp_err_to_name(err));
|
|
}
|
|
cJSON *o = cJSON_CreateObject();
|
|
cJSON_AddStringToObject(o, "message", "saved; connecting");
|
|
return send_json(req, o);
|
|
}
|
|
|
|
// GET /api/wifi/scan : visible networks
|
|
static esp_err_t wifi_scan_get(httpd_req_t *req)
|
|
{
|
|
static wifi_ap_record_t recs[MAX_SCAN];
|
|
uint16_t count = MAX_SCAN;
|
|
esp_err_t err = net_scan(recs, &count);
|
|
if (err != ESP_OK) {
|
|
return send_error(req, "500 Internal Server Error", esp_err_to_name(err));
|
|
}
|
|
cJSON *arr = cJSON_CreateArray();
|
|
for (int i = 0; i < count; i++) {
|
|
cJSON *o = cJSON_CreateObject();
|
|
cJSON_AddStringToObject(o, "ssid", (const char *)recs[i].ssid);
|
|
cJSON_AddNumberToObject(o, "rssi", recs[i].rssi);
|
|
cJSON_AddBoolToObject(o, "open", recs[i].authmode == WIFI_AUTH_OPEN);
|
|
cJSON_AddItemToArray(arr, o);
|
|
}
|
|
cJSON *root = cJSON_CreateObject();
|
|
cJSON_AddItemToObject(root, "networks", arr);
|
|
return send_json(req, root);
|
|
}
|
|
|
|
static void add_uri(httpd_handle_t srv, const char *uri, httpd_method_t method, esp_err_t (*handler)(httpd_req_t *))
|
|
{
|
|
httpd_uri_t u = { .uri = uri, .method = method, .handler = handler, .user_ctx = NULL };
|
|
httpd_register_uri_handler(srv, &u);
|
|
}
|
|
|
|
esp_err_t web_start(void)
|
|
{
|
|
httpd_config_t cfg = HTTPD_DEFAULT_CONFIG();
|
|
cfg.max_uri_handlers = 12;
|
|
cfg.lru_purge_enable = true;
|
|
cfg.max_open_sockets = 7;
|
|
cfg.backlog_conn = 10;
|
|
esp_err_t err = httpd_start(&s_server, &cfg);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "HTTP server start failed: %s", esp_err_to_name(err));
|
|
return err;
|
|
}
|
|
add_uri(s_server, "/", HTTP_GET, index_get);
|
|
add_uri(s_server, "/app.js", HTTP_GET, app_js_get);
|
|
add_uri(s_server, "/style.css", HTTP_GET, style_css_get);
|
|
add_uri(s_server, "/api/status", HTTP_GET, status_get);
|
|
add_uri(s_server, "/api/pattern", HTTP_POST, pattern_post);
|
|
add_uri(s_server, "/api/wifi", HTTP_GET, wifi_get);
|
|
add_uri(s_server, "/api/wifi", HTTP_POST, wifi_post);
|
|
add_uri(s_server, "/api/wifi/scan", HTTP_GET, wifi_scan_get);
|
|
ESP_LOGI(TAG, "Web UI on port 80");
|
|
return ESP_OK;
|
|
}
|