feat(v2): move web UI into v2, remove legacy MicroPython app

Move the web assets and templates under v2/web, drop the old src/, lib/
and scripts, and update firmware, tools, docs and db files. Add the audio
page, LED test scripts and VPN autoconnect script.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Sonnet 5.5 committed 2026-11-02 20:30:02 +13:00
1 parent 6c16998691
commit 71bd320661
193 files changed
+5605 -18860

No files matched your search

+5 -4
View File
@@ -2,9 +2,10 @@ idf_component_register(SRCS "main.c" "mic.c" "beat.c" "beat_dsp.c" "espnow_tx.c"
INCLUDE_DIRS "."
REQUIRES esp_eth esp_netif esp_event esp_driver_spi esp_driver_gpio nvs_flash app_update esp_app_format
esp_http_server esp_timer esp_driver_i2s esp_wifi esp_rom json esp_driver_gpio
PRIV_REQUIRES littlefs)
PRIV_REQUIRES littlefs mdns)
# Flash the gzipped web UI into the www partition (run tools/build_web.sh first)
if(EXISTS ${PROJECT_DIR}/www_dist)
littlefs_create_partition_image(www ${PROJECT_DIR}/www_dist FLASH_IN_PROJECT)
# The gzipped web UI is embedded in the app image (run tools/build_web.sh first)
if(NOT EXISTS ${PROJECT_DIR}/www_dist/www.pack)
message(FATAL_ERROR "www_dist/www.pack is missing: run v2/tools/build_web.sh (make build does)")
endif()
target_add_binary_data(${COMPONENT_LIB} ${PROJECT_DIR}/www_dist/www.pack BINARY)
+45
View File
@@ -198,6 +198,47 @@ static esp_err_t handle_profile_action(httpd_req_t *req, const char *id, const c
return not_implemented(req, action);
}
// Settings a driver keeps in its own settings.json and takes from a device_config message
static const char *const DEVICE_CONFIG_KEYS[] = { "max_brightness", "num_leds", "color_order", "startup_mode", "reversed" };
// The driver settings present in a device update, or NULL when it has none. Caller deletes the result.
static cJSON *device_config_from(const cJSON *body)
{
cJSON *dc = NULL;
for (size_t i = 0; i < sizeof(DEVICE_CONFIG_KEYS) / sizeof(DEVICE_CONFIG_KEYS[0]); i++) {
const cJSON *v = cJSON_GetObjectItem(body, DEVICE_CONFIG_KEYS[i]);
if (!v || cJSON_IsNull(v)) {
continue;
}
if (!dc) {
dc = cJSON_CreateObject();
}
cJSON_AddItemToObject(dc, DEVICE_CONFIG_KEYS[i], cJSON_Duplicate(v, true));
}
return dc;
}
// Send changed settings to an ESP-NOW driver; takes ownership of dc
static esp_err_t push_device_config(const cJSON *dev, cJSON *dc)
{
const cJSON *tr = cJSON_GetObjectItem(dev, "transport"), *addr = cJSON_GetObjectItem(dev, "address");
if (!cJSON_IsString(tr) || strcmp(tr->valuestring, "espnow") || !cJSON_IsString(addr)) {
cJSON_Delete(dc);
return ESP_ERR_INVALID_ARG;
}
cJSON *msg = cJSON_CreateObject();
cJSON_AddStringToObject(msg, "v", "1");
cJSON_AddItemToObject(msg, "device_config", dc);
cJSON_AddTrueToObject(msg, "save");
cJSON *messages = cJSON_CreateArray(), *targets = cJSON_CreateArray();
cJSON_AddItemToArray(messages, msg);
cJSON_AddItemToArray(targets, cJSON_CreateString(addr->valuestring));
esp_err_t err = engine_push(messages, targets);
cJSON_Delete(messages);
cJSON_Delete(targets);
return err;
}
static esp_err_t collection_handler_inner(httpd_req_t *req)
{
char seg[3][64];
@@ -425,9 +466,13 @@ static esp_err_t collection_handler_inner(httpd_req_t *req)
if (!body) {
return ESP_OK;
}
cJSON *dev_cfg = !strcmp(coll, "device") ? device_config_from(body) : NULL;
merge(rec, body);
cJSON_Delete(body);
store_save(coll);
if (dev_cfg) {
push_device_config(rec, dev_cfg); // before the group resend: a second message right behind another may not be delivered
}
if (!strcmp(coll, "zone") || !strcmp(coll, "device")) {
engine_groups_changed();
}
+1
View File
@@ -1,2 +1,3 @@
dependencies:
joltwallet/littlefs: "^1.19.0"
espressif/mdns: "^1.4.0"
+138
View File
@@ -17,6 +17,13 @@
#include "esp_http_server.h"
#include "esp_timer.h"
#include "esp_system.h"
#include "mdns.h"
#include "lwip/ip4_addr.h"
#include "lwip/etharp.h"
#include "lwip/netif.h"
#include "lwip/sockets.h"
#include "esp_netif_net_stack.h"
#include "cJSON.h"
#include "mic.h"
#include "beat.h"
#include "espnow_tx.h"
@@ -71,6 +78,126 @@ static esp_err_t status_get_handler(httpd_req_t *req)
return httpd_resp_sendstr(req, buf);
}
#define DEFAULT_HOSTNAME "led-controller"
static const char *settings_str(const char *key)
{
cJSON *v = cJSON_GetObjectItem(store_settings(), key);
return cJSON_IsString(v) && v->valuestring[0] ? v->valuestring : NULL;
}
// "static_ips" setting: a list of {ip, netmask, gw, dns} tried in order on every link-up. The first whose
// gateway answers ARP is kept; if none does, the controller falls back to DHCP.
static bool parse_static(cJSON *e, esp_netif_ip_info_t *info, esp_netif_dns_info_t *dns)
{
const char *ip = NULL, *mask = "255.255.255.0", *gw = NULL, *d = NULL;
cJSON *v;
if (!cJSON_IsObject(e)) return false;
if (cJSON_IsString(v = cJSON_GetObjectItem(e, "ip"))) ip = v->valuestring;
if (cJSON_IsString(v = cJSON_GetObjectItem(e, "netmask"))) mask = v->valuestring;
if (cJSON_IsString(v = cJSON_GetObjectItem(e, "gw"))) gw = v->valuestring;
if (cJSON_IsString(v = cJSON_GetObjectItem(e, "dns"))) d = v->valuestring;
memset(info, 0, sizeof(*info));
memset(dns, 0, sizeof(*dns));
if (!ip || esp_netif_str_to_ip4(ip, &info->ip) != ESP_OK || esp_netif_str_to_ip4(mask, &info->netmask) != ESP_OK ||
(gw && esp_netif_str_to_ip4(gw, &info->gw) != ESP_OK)) {
return false;
}
if (d && esp_netif_str_to_ip4(d, &dns->ip.u_addr.ip4) == ESP_OK) {
dns->ip.type = ESP_IPADDR_TYPE_V4;
} else if (gw) {
dns->ip.u_addr.ip4 = info->gw;
dns->ip.type = ESP_IPADDR_TYPE_V4;
}
return true;
}
// Poke the gateway so lwIP ARPs for it, then wait for the entry to show up
static bool gateway_answers(esp_netif_t *netif, const esp_ip4_addr_t *gw)
{
struct netif *nif = esp_netif_get_netif_impl(netif);
ip4_addr_t g = { .addr = gw->addr };
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (!nif || sock < 0) {
if (sock >= 0) close(sock);
return false;
}
struct sockaddr_in to = { .sin_family = AF_INET, .sin_port = htons(9), .sin_addr.s_addr = gw->addr };
bool ok = false;
for (int i = 0; i < 15 && !ok; i++) {
sendto(sock, "", 0, 0, (struct sockaddr *)&to, sizeof(to));
vTaskDelay(pdMS_TO_TICKS(100));
struct eth_addr *eth = NULL;
const ip4_addr_t *ipr = NULL;
ok = etharp_find_addr(nif, &g, &eth, &ipr) >= 0;
}
close(sock);
return ok;
}
static void static_ip_task(void *arg)
{
static bool running;
if (running) {
vTaskDelete(NULL);
}
running = true;
cJSON *list = cJSON_GetObjectItem(store_settings(), "static_ips");
esp_netif_dhcpc_stop(s_eth_netif);
bool done = false;
cJSON *e;
cJSON_ArrayForEach(e, list) {
esp_netif_ip_info_t info;
esp_netif_dns_info_t dns;
if (!parse_static(e, &info, &dns)) {
ESP_LOGW(TAG, "static_ips: skipping an invalid entry");
continue;
}
if (esp_netif_set_ip_info(s_eth_netif, &info) != ESP_OK) {
continue;
}
if (dns.ip.type == ESP_IPADDR_TYPE_V4) {
esp_netif_set_dns_info(s_eth_netif, ESP_NETIF_DNS_MAIN, &dns);
}
if (info.gw.addr == 0 || gateway_answers(s_eth_netif, &info.gw)) {
ESP_LOGI(TAG, "Static IP " IPSTR " (gw " IPSTR ")", IP2STR(&info.ip), IP2STR(&info.gw));
done = true;
break;
}
ESP_LOGI(TAG, "Static IP " IPSTR ": gateway " IPSTR " did not answer", IP2STR(&info.ip), IP2STR(&info.gw));
}
if (!done) {
ESP_LOGW(TAG, "No static IP matched this network, using DHCP");
esp_netif_ip_info_t none = { 0 };
esp_netif_set_ip_info(s_eth_netif, &none);
esp_netif_dhcpc_start(s_eth_netif);
}
running = false;
vTaskDelete(NULL);
}
static bool have_static_ips(void)
{
cJSON *l = cJSON_GetObjectItem(store_settings(), "static_ips");
return cJSON_IsArray(l) && cJSON_GetArraySize(l) > 0;
}
static void start_mdns(void)
{
const char *host = settings_str("hostname");
if (!host) {
host = DEFAULT_HOSTNAME;
}
if (mdns_init() != ESP_OK) {
ESP_LOGE(TAG, "mDNS init failed");
return;
}
mdns_hostname_set(host);
mdns_instance_name_set("LED controller");
mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0);
ESP_LOGI(TAG, "mDNS: %s.local", host);
}
static void start_webserver(void)
{
if (s_server) {
@@ -108,6 +235,9 @@ static void eth_event_handler(void *arg, esp_event_base_t event_base,
esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &speed);
esp_eth_ioctl(eth_handle, ETH_CMD_G_DUPLEX_MODE, &duplex);
ESP_LOGI(TAG, "Ethernet Link Up, MAC " MACSTR, MAC2STR(mac));
if (have_static_ips()) {
xTaskCreate(static_ip_task, "static_ip", 4096, NULL, 5, NULL);
}
ESP_LOGI(TAG, "%s Mbps %s Duplex",
speed == ETH_SPEED_100M ? "100" : "10",
duplex == ETH_DUPLEX_FULL ? "Full" : "Half");
@@ -139,6 +269,11 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
// Server binds to all interfaces, so it keeps working across lease changes
start_webserver();
static bool mdns_started;
if (!mdns_started) {
mdns_started = true;
start_mdns();
}
}
static esp_err_t w5500_init(esp_eth_handle_t *eth_handle_out)
@@ -229,6 +364,9 @@ void app_main(void)
s_eth_netif = esp_netif_new(&netif_cfg);
ESP_ERROR_CHECK(esp_netif_attach(s_eth_netif, esp_eth_new_netif_glue(eth_handle)));
const char *host = settings_str("hostname");
esp_netif_set_hostname(s_eth_netif, host ? host : DEFAULT_HOSTNAME);
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
+29 -3
View File
@@ -6,6 +6,7 @@
#include "esp_ota_ops.h"
#include "esp_app_format.h"
#include "esp_system.h"
#include <string.h>
#include "esp_timer.h"
static const char *TAG = "ota";
@@ -140,14 +141,35 @@ static esp_err_t ota_get_handler(httpd_req_t *req)
{
const esp_partition_t *run = esp_ota_get_running_partition();
const esp_app_desc_t *app = esp_app_get_description();
char buf[256];
// The build date is only recompiled with its own file, so the ELF hash is what tells two builds apart
char sha[17];
for (int i = 0; i < 8; i++) {
snprintf(sha + 2 * i, 3, "%02x", app->app_elf_sha256[i]);
}
char buf[288];
snprintf(buf, sizeof(buf),
"{\"partition\":\"%s\",\"version\":\"%s\",\"built\":\"%s %s\",\"idf\":\"%s\"}",
run ? run->label : "?", app->version, app->date, app->time, app->idf_ver);
"{\"partition\":\"%s\",\"version\":\"%s\",\"built\":\"%s %s\",\"idf\":\"%s\",\"sha\":\"%s\"}",
run ? run->label : "?", app->version, app->date, app->time, app->idf_ver, sha);
httpd_resp_set_type(req, "application/json");
return httpd_resp_sendstr(req, buf);
}
static esp_err_t reboot_post_handler(httpd_req_t *req)
{
ESP_LOGI(TAG, "reboot requested");
httpd_resp_set_type(req, "application/json");
httpd_resp_sendstr(req, "{\"message\":\"rebooting\"}");
static esp_timer_handle_t t;
const esp_timer_create_args_t args = { .callback = reboot_cb, .name = "reboot" };
if (esp_timer_create(&args, &t) == ESP_OK) {
esp_timer_start_once(t, 500 * 1000);
} else {
esp_restart();
}
return ESP_OK;
}
esp_err_t ota_register(httpd_handle_t server)
{
const httpd_uri_t post = { .uri = "/ota", .method = HTTP_POST, .handler = ota_post_handler };
@@ -156,5 +178,9 @@ esp_err_t ota_register(httpd_handle_t server)
if (err == ESP_OK) {
err = httpd_register_uri_handler(server, &get);
}
const httpd_uri_t reboot = { .uri = "/reboot", .method = HTTP_POST, .handler = reboot_post_handler };
if (err == ESP_OK) {
err = httpd_register_uri_handler(server, &reboot);
}
return err;
}
+2
View File
@@ -4,6 +4,8 @@
#include "esp_http_server.h"
// Register POST /ota (raw firmware .bin as the body) and GET /ota (running image info).
// The web UI is part of the image, so one upload updates both.
// POST /reboot restarts the board (replies first, reboots after 0.5 s).
// A good upload is written to the idle app slot, becomes the boot image and the board reboots.
esp_err_t ota_register(httpd_handle_t server);
+1 -1
View File
@@ -36,6 +36,7 @@ static esp_err_t mount(const char *label, const char *base)
.base_path = base,
.partition_label = label,
.format_if_mount_failed = true,
.grow_on_mount = true, // the db partition spans the rest of the flash; an older, smaller filesystem is extended in place
};
return esp_vfs_littlefs_register(&conf);
}
@@ -71,7 +72,6 @@ esp_err_t store_init(void)
{
s_lock = xSemaphoreCreateRecursiveMutex();
ESP_RETURN_ON_FALSE(s_lock, ESP_ERR_NO_MEM, TAG, "mutex");
ESP_RETURN_ON_ERROR(mount("www", "/www"), TAG, "mount www");
ESP_RETURN_ON_ERROR(mount("db", "/db"), TAG, "mount db");
for (size_t i = 0; i < N_COLL; i++) {
s_coll[i] = load_file(NAMES[i]);
+1 -1
View File
@@ -8,7 +8,7 @@
// id -> record, mirroring the Pi's db/*.json. Collections stay in memory and
// are written back (via a temp file and rename) whenever they change.
// Mount the www and db partitions and load every collection
// Mount the db partition and load every collection
esp_err_t store_init(void);
// Borrowed pointer to the collection object (id -> record); never NULL for a known name
+44 -26
View File
@@ -1,14 +1,12 @@
#include "web.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "esp_log.h"
static const char *TAG = "web";
#define CHUNK 2048
static const struct {
const char *ext;
const char *type;
@@ -34,37 +32,57 @@ static const char *mime_for(const char *path)
return "application/octet-stream";
}
// path is the URL-relative name, e.g. "static/zones.js"; the file on flash is "/www/<path>.gz"
// The web UI is one blob linked into the app image (www_dist/www.pack, see tools/build_web.sh):
// "WWW1", then per file u16 name length, u32 data length, the name, the gzipped data; a zero name
// length ends it.
extern const uint8_t www_pack_start[] asm("_binary_www_pack_start");
extern const uint8_t www_pack_end[] asm("_binary_www_pack_end");
static bool find_file(const char *path, const uint8_t **data, uint32_t *len)
{
const uint8_t *p = www_pack_start, *end = www_pack_end;
if (end - p < 6 || memcmp(p, "WWW1", 4) != 0) {
return false;
}
p += 4;
size_t want = strlen(path);
while (end - p >= 2) {
uint16_t nlen;
memcpy(&nlen, p, 2);
if (nlen == 0 || end - p < 6 + nlen) {
return false;
}
uint32_t dlen;
memcpy(&dlen, p + 2, 4);
const uint8_t *name = p + 6, *body = name + nlen;
if (end - body < (ptrdiff_t)dlen) {
return false;
}
if (nlen == want && memcmp(name, path, want) == 0) {
*data = body;
*len = dlen;
return true;
}
p = body + dlen;
}
return false;
}
// path is the URL-relative name, e.g. "static/zones.js"
static esp_err_t send_gz(httpd_req_t *req, const char *path, bool immutable)
{
char fs[128];
if (strstr(path, "..") || snprintf(fs, sizeof(fs), "/www/%s.gz", path) >= (int)sizeof(fs)) {
const uint8_t *data;
uint32_t len;
if (strstr(path, "..")) {
return httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "bad path");
}
FILE *f = fopen(fs, "rb");
if (!f) {
if (!find_file(path, &data, &len)) {
return httpd_resp_send_404(req);
}
httpd_resp_set_type(req, mime_for(path));
httpd_resp_set_hdr(req, "Content-Encoding", "gzip");
httpd_resp_set_hdr(req, "Cache-Control", immutable ? "public, max-age=86400" : "no-cache");
char *buf = malloc(CHUNK);
if (!buf) {
fclose(f);
return httpd_resp_send_500(req);
}
esp_err_t ret = ESP_OK;
size_t n;
while (ret == ESP_OK && (n = fread(buf, 1, CHUNK, f)) > 0) {
ret = httpd_resp_send_chunk(req, buf, n);
}
if (ret == ESP_OK) {
ret = httpd_resp_send_chunk(req, NULL, 0);
}
free(buf);
fclose(f);
return ret;
return httpd_resp_send(req, (const char *)data, len);
}
esp_err_t web_send_page(httpd_req_t *req, const char *name)
+2 -2
View File
@@ -3,8 +3,8 @@
#include "esp_err.h"
#include "esp_http_server.h"
// Register /, /settings/page, /static/* and /favicon.ico. Files come from the
// www partition, stored as <path>.gz and sent with Content-Encoding: gzip.
// Register /, /audio, /static/* and /favicon.ico. Files come from the web UI blob embedded in the
// firmware (gzipped, sent with Content-Encoding: gzip).
esp_err_t web_register(httpd_handle_t server);
// Send one of the top-level pages (e.g. "settings.html")