diff --git a/README.md b/README.md index 8424260..b1b48ca 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ make test-browser HOST= # Selenium UI tests (needs selenium and chromedriv - `led-driver/` (submodule): MicroPython firmware for the ESP32 LED drivers. - `led-tool/` (submodule): `led-cli`, configures drivers over serial and updates the controller over Ethernet (`led-cli --ota HOST`). - `led-simulator/` (submodule), `espnow-sender/`: test helpers. -- `db/`: the records a controller starts with (the `default` profile the firmware seeds at first boot, plus pattern metadata), importable with `PUT /db/`. +- `db/`: the records a controller starts with (the `default` profile and group the firmware seeds at first boot, plus pattern metadata), importable with `PUT /db/`. - `test/api/`: the API tests (`test/api/README.md`); `test/browser/`: the UI tests. ## Docs @@ -39,6 +39,8 @@ The controller advertises `.local` over mDNS (default `led-controller` `static_ips` is a list of networks to try, in order, each time the Ethernet link comes up. The first entry whose gateway answers ARP is used; if none does (or the list is empty) the controller uses DHCP. `netmask` defaults to `255.255.255.0`, `dns` to the gateway. +If DHCP gets no answer within 15 seconds, the controller takes `192.168.4.1/24`. For a cable straight from a PC, give the PC a static IP on that subnet, such as `192.168.4.2/24`, and open `http://192.168.4.1/`. The controller tries DHCP again only when the link next comes up. + ```bash curl -X PUT http://led-controller.local/settings/settings -H 'Content-Type: application/json' -d '{ "hostname": "stage", diff --git a/db/group.json b/db/group.json index 9e26dfe..f195b59 100644 --- a/db/group.json +++ b/db/group.json @@ -1 +1 @@ -{} \ No newline at end of file +{"1": {"name": "default", "devices": [], "wifi_driver_display_name": null, "wifi_driver_num_leds": null, "wifi_color_order": null, "wifi_startup_mode": null, "output_brightness": 255, "bridge_id": null, "pattern": "on", "colors": ["000000", "FF0000"], "brightness": 100, "delay": 100, "step_offset": 0, "step_increment": 1, "n1": 0, "n2": 0, "n3": 0, "n4": 0, "n5": 0, "n6": 0, "n7": 0, "n8": 0, "id": "1"}} \ No newline at end of file diff --git a/db/zone.json b/db/zone.json index f079fb9..b98aba2 100644 --- a/db/zone.json +++ b/db/zone.json @@ -1 +1 @@ -{"1": {"name": "default", "names": ["1"], "presets": [["1", "2", "3", "4", "5", "6", "7", "8"]], "presets_flat": ["1", "2", "3", "4", "5", "6", "7", "8"], "default_preset": "1", "id": "1", "group_ids": [], "preset_group_ids": {}, "sequence_ids": [], "content_kind": "presets"}} \ No newline at end of file +{"1": {"name": "default", "names": ["1"], "presets": [["1", "2", "3", "4", "5", "6", "7", "8"]], "presets_flat": ["1", "2", "3", "4", "5", "6", "7", "8"], "default_preset": "1", "id": "1", "group_ids": ["1"], "preset_group_ids": {}, "sequence_ids": [], "content_kind": "presets"}} \ No newline at end of file diff --git a/docs/API.md b/docs/API.md index e9889e4..47f0aa3 100644 --- a/docs/API.md +++ b/docs/API.md @@ -61,7 +61,7 @@ Keys with meaning on the controller (others are stored as they are): | Key | Checked | Meaning | |-----|---------|---------| | `hostname` | | mDNS name (`.local`, default `led-controller`); applies after a restart | -| `static_ips` | | List of `{ip, netmask?, gw, dns?}` tried in order when the Ethernet link comes up; the first whose gateway answers ARP is used, else DHCP. Applies after a restart | +| `static_ips` | | List of `{ip, netmask?, gw, dns?}` tried in order when the Ethernet link comes up; the first whose gateway answers ARP is used, else DHCP, else `192.168.4.1/24` for a direct PC cable. Applies after a restart | | `global_brightness` | integer 0–255 | Global factor of each driver's brightness (see Groups). Stored only: the UI pushes each driver its combined value | | `wifi_channel` | integer 1–11 | Stored for reference. The controller's ESP-NOW channel is set at build time (`make menuconfig`) | | `audio_simulated_bpm` | clamped to 60–200 | Tempo of the beat clock (default 120) | @@ -90,7 +90,7 @@ A profile is `{name, type: "zones", zones: [zone ids], scenes, palette_id}`. | GET | `/profiles//export` | Profile bundle (see Import and export) | | POST | `/profiles/import` | Body: `{"bundle": {...}, "name"?, "apply"? (default true)}` or the bundle itself. Reply `{"": profile, "id": ""}`, 201 | -A store with no profile gets one at boot, `default`, with the same seed and a starter palette. A new profile gets an empty palette, eight presets (`on`, `off`, `rainbow` (`colour_cycle`), `Colour Cycle`, `transition`, `flicker`, `flame`, `twinkle`) and a zone `default` that lists them and names driver `1`. With `seed_dj_zone` it also gets three `DJ …` presets in a zone `dj` naming driver `dj`. +A store with no profile gets one at boot, `default`, with the same seed, a starter palette and an empty `default` group that its zone plays to. A new profile gets an empty palette, eight presets (`on`, `off`, `rainbow` (`colour_cycle`), `Colour Cycle`, `transition`, `flicker`, `flame`, `twinkle`) and a zone `default` that lists them and names driver `1`. With `seed_dj_zone` it also gets three `DJ …` presets in a zone `dj` naming driver `dj`. ## Presets — `/presets` diff --git a/docs/BENCH_TEST.md b/docs/BENCH_TEST.md index f2671f5..5f957d4 100644 --- a/docs/BENCH_TEST.md +++ b/docs/BENCH_TEST.md @@ -50,7 +50,7 @@ export S3=192.168.1.50 curl -s http://$S3/status ``` -Expect JSON with `uptime_s`, `bpm`, an `espnow` block and a `link` block (`ver` nonzero, `digests` increasing by one about every 1.5 s). If the mic hears music, `bpm` and `beats_sent` move too, which means beats are already being broadcast. Open `http://$S3/` in a browser: the UI should load, and the dev tools network tab should show `Content-Encoding: gzip` on `/static/*.js`. +Expect JSON with `uptime_s`, `reset_reason` (why the board last restarted, such as `panic`, `task_watchdog` or `brownout`; opening the USB serial port resets the board and replaces it with `usb`, so read it first), `bpm`, an `espnow` block and a `link` block (`ver` nonzero, `digests` increasing by one about every 1.5 s). If the mic hears music, `bpm` and `beats_sent` move too, which means beats are already being broadcast. Open `http://$S3/` in a browser: the UI should load, and the dev tools network tab should show `Content-Encoding: gzip` on `/static/*.js`. ```bash curl -s -D - -o /dev/null -H 'Accept-Encoding: gzip' http://$S3/static/presets.js | grep -i -E 'content-encoding|content-type' diff --git a/docs/help.md b/docs/help.md index 88719bd..455dd51 100644 --- a/docs/help.md +++ b/docs/help.md @@ -106,7 +106,7 @@ The preset editor: ## Settings -**Settings** (Edit mode) holds the controller's name, the ESP-NOW channel (for reference: the controller's channel is set at build time), its mDNS **hostname** and the **static IPs** it tries on the Ethernet link (DHCP when none answers). Network changes apply after a restart. Drivers are configured over USB with `led-cli` on a computer. +**Settings** (Edit mode) holds the controller's name, the ESP-NOW channel (for reference: the controller's channel is set at build time), its mDNS **hostname** and the **static IPs** it tries on the Ethernet link (DHCP when none answers). With no DHCP server, as on a cable straight to a PC, the controller is at `192.168.4.1`; give the PC a static IP such as `192.168.4.2`. Network changes apply after a restart. Drivers are configured over USB with `led-cli` on a computer. ![Schematic: narrow layout with Menu and the same header actions in a dropdown.](images/help/mobile-menu.svg) diff --git a/main/api_util.h b/main/api_util.h index e8e035a..474cddd 100644 --- a/main/api_util.h +++ b/main/api_util.h @@ -55,6 +55,8 @@ bool api_preset_in_profile(const cJSON *rec); // Bring records written by older versions into the current shape (at boot) void api_migrate_sequences(void); void api_seed_defaults(void); +// A new group with the defaults, saved; its id in id +void api_add_group(const char *name, char *id, size_t len); void api_migrate_zones(void); void api_migrate_groups(void); diff --git a/main/main.c b/main/main.c index aa4978c..794047b 100644 --- a/main/main.c +++ b/main/main.c @@ -45,6 +45,23 @@ static const char *TAG = "eth"; static httpd_handle_t s_server = NULL; static esp_netif_t *s_eth_netif = NULL; +static const char *reset_reason(void) +{ + switch (esp_reset_reason()) { + case ESP_RST_POWERON: return "power_on"; + case ESP_RST_EXT: return "external"; + case ESP_RST_SW: return "software"; + case ESP_RST_PANIC: return "panic"; + case ESP_RST_INT_WDT: return "interrupt_watchdog"; + case ESP_RST_TASK_WDT: return "task_watchdog"; + case ESP_RST_WDT: return "watchdog"; + case ESP_RST_BROWNOUT: return "brownout"; + case ESP_RST_USB: return "usb"; + case ESP_RST_PWR_GLITCH: return "power_glitch"; + default: return "unknown"; + } +} + static esp_err_t status_get_handler(httpd_req_t *req) { beat_info_t bi; @@ -63,14 +80,14 @@ static esp_err_t status_get_handler(httpd_req_t *req) snprintf(ip, sizeof(ip), IPSTR, IP2STR(&ip_info.ip)); snprintf(macs, sizeof(macs), MACSTR, MAC2STR(mac)); snprintf(buf, sizeof(buf), - "{\"ip\":\"%s\",\"mac\":\"%s\",\"uptime_s\":%lld,\"free_heap\":%lu,\"free_internal\":%lu," + "{\"ip\":\"%s\",\"mac\":\"%s\",\"uptime_s\":%lld,\"reset_reason\":\"%s\",\"free_heap\":%lu,\"free_internal\":%lu," "\"mic_rms_dbfs\":%.1f,\"mic_peak_dbfs\":%.1f,\"mic_spl_db\":%.1f," "\"bpm\":%.1f,\"beat_confidence\":%.2f,\"beat_locked\":%s,\"beats\":%lu,\"onsets\":%lu," "\"meter\":%d,\"bar_beat\":%d,\"bar_sure\":%s,\"downbeats\":%lu," "\"espnow\":{\"beats_sent\":%lu,\"beat_send_fail\":%lu,\"bulk_sent\":%lu," "\"bulk_retries\":%lu,\"bulk_failed\":%lu,\"sim_dropped\":%lu}," "\"link\":{\"ver\":%u,\"acked\":%lu,\"ack_timeouts\":%lu,\"retransmits\":%lu,\"resyncs\":%lu,\"digests\":%lu}}", - ip, macs, (long long)(esp_timer_get_time() / 1000000), (unsigned long)esp_get_free_heap_size(), + ip, macs, (long long)(esp_timer_get_time() / 1000000), reset_reason(), (unsigned long)esp_get_free_heap_size(), (unsigned long)heap_caps_get_free_size(MALLOC_CAP_INTERNAL), level.rms_dbfs, level.peak_dbfs, level.spl_db, bi.bpm, bi.confidence, bi.locked ? "true" : "false", (unsigned long)bi.beat_count, (unsigned long)bi.onset_count, bi.meter, bi.bar_beat, bi.bar_sure ? "true" : "false", @@ -85,6 +102,11 @@ static esp_err_t status_get_handler(httpd_req_t *req) #define DEFAULT_HOSTNAME "led-controller" +// Address when no DHCP server answers, as on a cable straight to a PC. The PC needs a static IP on this subnet. +#define DEFAULT_IP ESP_IP4TOADDR(192, 168, 4, 1) +#define DEFAULT_NETMASK ESP_IP4TOADDR(255, 255, 255, 0) +#define DHCP_WAIT_S 15 + static const char *settings_str(const char *key) { cJSON *v = cJSON_GetObjectItem(store_settings(), key); @@ -92,7 +114,7 @@ static const char *settings_str(const char *key) } // "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. +// gateway answers ARP is kept; if none does, the controller falls back to DHCP, then to DEFAULT_IP. 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; @@ -140,16 +162,19 @@ static bool gateway_answers(esp_netif_t *netif, const esp_ip4_addr_t *gw) return ok; } -static void static_ip_task(void *arg) +static bool have_static_ips(void) { - static bool running; - if (running) { - vTaskDelete(NULL); + cJSON *l = cJSON_GetObjectItem(store_settings(), "static_ips"); + return cJSON_IsArray(l) && cJSON_GetArraySize(l) > 0; +} + +static bool try_static_ips(void) +{ + if (!have_static_ips()) { + return false; } - 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; @@ -166,27 +191,46 @@ static void static_ip_task(void *arg) } 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; + return true; } 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); + ESP_LOGW(TAG, "No static IP matched this network, using DHCP"); + return false; +} + +// Start DHCP and wait for a lease. A lost link also ends the wait: the next link-up starts over. +static bool dhcp_settled(void) +{ + esp_netif_dhcpc_start(s_eth_netif); + for (int i = 0; i < DHCP_WAIT_S * 10; i++) { + esp_netif_ip_info_t ip; + vTaskDelay(pdMS_TO_TICKS(100)); + esp_netif_get_ip_info(s_eth_netif, &ip); + if (ip.ip.addr != 0 || !esp_netif_is_netif_up(s_eth_netif)) { + return true; + } + } + return false; +} + +static void link_up_task(void *arg) +{ + static bool running; + if (running) { + vTaskDelete(NULL); + } + running = true; + if (!try_static_ips() && !dhcp_settled()) { + ESP_LOGW(TAG, "No DHCP server, using the default IP"); + esp_netif_ip_info_t info = { .ip.addr = DEFAULT_IP, .netmask.addr = DEFAULT_NETMASK }; + esp_netif_dhcpc_stop(s_eth_netif); + esp_netif_set_ip_info(s_eth_netif, &info); } 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"); @@ -243,9 +287,7 @@ 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); - } + xTaskCreate(link_up_task, "link_up", 4096, NULL, 5, NULL); ESP_LOGI(TAG, "%s Mbps %s Duplex", speed == ETH_SPEED_100M ? "100" : "10", duplex == ETH_DUPLEX_FULL ? "Full" : "Half"); diff --git a/main/res_group.c b/main/res_group.c index fdf67b5..8034f1a 100644 --- a/main/res_group.c +++ b/main/res_group.c @@ -16,6 +16,14 @@ static const char *const DEFAULTS = "\"pattern\":\"on\",\"colors\":[\"000000\",\"FF0000\"],\"brightness\":100,\"delay\":100,\"step_offset\":0," "\"step_increment\":1,\"n1\":0,\"n2\":0,\"n3\":0,\"n4\":0,\"n5\":0,\"n6\":0,\"n7\":0,\"n8\":0}"; +void api_add_group(const char *name, char *id, size_t len) +{ + cJSON *g = cJSON_Parse(DEFAULTS); + api_set(g, "name", cJSON_CreateString(name)); + api_add("group", g, id, len); + store_save("group"); +} + bool api_mac(const cJSON *v, char out[13]) { if (!cJSON_IsString(v)) { diff --git a/main/res_profile.c b/main/res_profile.c index 8a5a076..7940419 100644 --- a/main/res_profile.c +++ b/main/res_profile.c @@ -277,11 +277,17 @@ void api_seed_defaults(void) } cJSON *fields = cJSON_CreateObject(); cJSON_AddStringToObject(fields, "name", "default"); - char pid[16]; - seed_profile(fields, + char pid[16], gid[16]; + cJSON *prof = seed_profile(fields, cJSON_Parse("[\"#FF0000\",\"#00FF00\",\"#0000FF\",\"#FFFF00\",\"#FF00FF\",\"#00FFFF\",\"#FFFFFF\",\"#000000\"]"), false, pid, sizeof(pid)); cJSON_Delete(fields); + api_add_group("default", gid, sizeof(gid)); + cJSON *gids = cJSON_CreateArray(); + cJSON_AddItemToArray(gids, cJSON_CreateString(gid)); + const cJSON *zid = cJSON_GetArrayItem(cJSON_GetObjectItem(prof, "zones"), 0); + api_set(cJSON_GetObjectItem(store_coll("zone"), zid->valuestring), "group_ids", gids); + store_save("zone"); } static const cJSON *profile_zones(const cJSON *prof) diff --git a/test/api/test_routes.py b/test/api/test_routes.py index 1e0b4ff..013d9ed 100644 --- a/test/api/test_routes.py +++ b/test/api/test_routes.py @@ -16,6 +16,10 @@ def test_static(c, path): assert c.get(f"/static/{path}").status_code == 200 +def test_status_reset_reason(c): + assert c.get("/status").json()["reset_reason"] + + def test_settings_page(c): resp = c.get("/settings/page") assert resp.status_code == 200 diff --git a/web/templates/index.html b/web/templates/index.html index 0983b63..44070cc 100644 --- a/web/templates/index.html +++ b/web/templates/index.html @@ -1178,7 +1178,7 @@
- Tried in order each time the Ethernet link comes up; the first whose gateway answers is used. If none does, or the list is empty, DHCP is used. + Tried in order each time the Ethernet link comes up; the first whose gateway answers is used. If none does, or the list is empty, DHCP is used. With no DHCP server, as on a cable straight to a PC, the controller is at 192.168.4.1; give the PC a static IP such as 192.168.4.2.