#include "boomcast_web.h" #include "boomcast_audio.h" #include "boomcast_monitor.h" #include "boomcast_config.h" #include "boomcast_network.h" #include "boomcast_queue.h" #include "boomcast_state.h" #include BoomWeb boomWeb; static String webJsonEscape(String value) { value.replace("\\", "\\\\"); value.replace("\"", "\\\""); value.replace("\r", "\\r"); value.replace("\n", "\\n"); return value; } static const char MANAGEMENT_HTML[] PROGMEM = R"BOOMHTML( BoomCast

BoomCast

Firework sensor · local management

Loading…
Today—
Queued—
Microphone—
Storage—

Live microphone shape

A low-resolution level preview. This graph is generated locally from the current sample block; it is not an audio stream.

Detection sensitivity

Higher means easier to trigger. This changes BoomCast's software thresholds, not microphone gain.

55%

Device

Open this page again from the address shown on the e-paper status screen.

Listening for bangs, not conversations.
BoomCast keeps a rolling RAM buffer and writes a short clip to SD only around a detected event.

IP: —

Wi-Fi: —

Firmware: —

Scheduled monitoring

Europe/Amsterdam · adjusts for daylight saving. Overnight hours belong to the day they start. Equal start and end means 24 hours.

Loading monitoring status…

Idle stops the microphone and keeps this page reachable. Hardware sleep is unavailable in this release pending V2 power-hold testing. BOOT wakes listening from idle; PWR keeps its existing function.

Upload queue

Files stay on the SD card until the server returns a successful response. Discard permanently removes recordings from this device and adjusts today's count.

Loading queue…

Wi-Fi setup

Scan nearby networks, save credentials, and reboot into station mode.

)BOOMHTML"; bool BoomWeb::authorise() { String password = boomState.prefs().getString("mgmt_pass", ""); if (password.length() && server_.authenticate(MANAGEMENT_USER, password.c_str())) return true; server_.requestAuthentication(BASIC_AUTH, "BoomCast"); return false; } void BoomWeb::addNoCache() { server_.sendHeader("Cache-Control", "no-store, max-age=0"); server_.sendHeader("Pragma", "no-cache"); } void BoomWeb::sendJson(int code, const String& json) { addNoCache(); server_.send(code, "application/json; charset=utf-8", json); } void BoomWeb::begin() { const char* actionHeaders[] = {"X-BoomCast-Action"}; server_.collectHeaders(actionHeaders, 1); server_.on("/", HTTP_GET, [this]() { handleRoot(); }); server_.on("/api/status", HTTP_GET, [this]() { handleStatus(); }); server_.on("/api/monitoring", HTTP_POST, [this]() { if (!authorise()) return; if (server_.header("X-BoomCast-Action") != "monitoring") { sendJson(403, "{\"error\":\"Missing monitoring action header\"}"); return; } BoomScheduleConfig c; auto parseTime = [this](const char* key, uint16_t& value) { String v = server_.arg(key); if (v.length() != 5 || v[2] != ':' || !isDigit(v[0]) || !isDigit(v[1]) || !isDigit(v[3]) || !isDigit(v[4])) return false; int h = v.substring(0,2).toInt(), m = v.substring(3).toInt(); if (h > 23 || m > 59) return false; value = h * 60 + m; return true; }; String minutes = server_.arg("overrideMinutes"); bool numeric = minutes.length() > 0 && minutes.length() <= 4; for (size_t i = 0; i < minutes.length(); ++i) numeric = numeric && isDigit(minutes[i]); String enabled = server_.arg("enabled"), modem = server_.arg("modemSleep"); c.enabled = enabled == "1"; c.modemSleep = modem == "1"; c.overrideMinutes = minutes.toInt(); if (!numeric || (enabled != "0" && enabled != "1") || (modem != "0" && modem != "1") || !parseTime("weekdayStart", c.weekdayStart) || !parseTime("weekdayEnd", c.weekdayEnd) || !parseTime("weekendStart", c.weekendStart) || !parseTime("weekendEnd", c.weekendEnd) || !c.valid()) { sendJson(400, "{\"error\":\"Use valid 24-hour times and 1–1440 override minutes\"}"); return; } if (!boomMonitor.save(c)) { sendJson(500, "{\"error\":\"Could not persist schedule\"}"); return; } sendJson(200, "{\"ok\":true}"); }); server_.on("/api/monitoring/override", HTTP_POST, [this]() { if (!authorise()) return; if (server_.header("X-BoomCast-Action") != "monitoring") { sendJson(403, "{\"error\":\"Missing monitoring action header\"}"); return; } boomMonitor.overrideNow(); sendJson(200, "{\"ok\":true}"); }); server_.on("/api/monitoring/return", HTTP_POST, [this]() { if (!authorise()) return; if (server_.header("X-BoomCast-Action") != "monitoring") { sendJson(403, "{\"error\":\"Missing monitoring action header\"}"); return; } boomMonitor.returnToSchedule(); sendJson(200, "{\"ok\":true}"); }); server_.on("/api/events", HTTP_GET, [this]() { handleEvents(); }); server_.on("/api/events/audio", HTTP_GET, [this]() { handleEventAudio(); }); server_.on("/api/wifi/scan", HTTP_GET, [this]() { handleWifiScan(); }); server_.on("/api/wifi", HTTP_POST, [this]() { handleWifiSave(); }); server_.on("/api/wifi/forget", HTTP_POST, [this]() { handleWifiForget(); }); server_.on("/api/sensitivity", HTTP_POST, [this]() { handleSensitivity(); }); server_.on("/api/sync", HTTP_POST, [this]() { handleSync(); }); server_.on("/api/events/discard", HTTP_POST, [this]() { handleDiscard(); }); server_.on("/api/today/reset", HTTP_POST, [this]() { handleTodayReset(); }); server_.on("/api/reboot", HTTP_POST, [this]() { handleReboot(); }); server_.onNotFound([this]() { if (!authorise()) return; sendJson(404, "{\"error\":\"Not found\"}"); }); server_.begin(); Serial.println("[Web] management interface listening on port 80"); } void BoomWeb::handle() { server_.handleClient(); } void BoomWeb::handleRoot() { if (!authorise()) return; addNoCache(); server_.send_P(200, "text/html; charset=utf-8", MANAGEMENT_HTML); } void BoomWeb::handleStatus() { if (!authorise()) return; BoomStateSnapshot s = boomState.snapshot(); String json; json.reserve(900); json += "{\"version\":\"" + String(BOOMCAST_FIRMWARE_VERSION) + "\""; json += ",\"monitoring\":" + boomMonitor.json(); json += ",\"wifi\":" + String(s.wifiConnected ? "true" : "false"); json += ",\"ap\":" + String(s.apMode ? "true" : "false"); json += ",\"sd\":" + String(s.sdReady ? "true" : "false"); json += ",\"capturing\":" + String(s.capturing ? "true" : "false"); json += ",\"syncing\":" + String(s.syncing ? "true" : "false"); json += ",\"ip\":\"" + webJsonEscape(s.ip) + "\""; json += ",\"ssid\":\"" + webJsonEscape(s.ssid) + "\""; json += ",\"queue\":" + String(s.queueCount); json += ",\"today\":" + String(s.todayEvents); json += ",\"sensitivity\":" + String(s.sensitivity); json += ",\"syncMessage\":\"" + webJsonEscape(s.lastSyncMessage) + "\""; json += ",\"waveform\":["; for (size_t i = 0; i < BOOMCAST_WAVEFORM_POINTS; ++i) { if (i) json += ','; json += String((int)s.waveform[i]); } json += "]}"; sendJson(200, json); } void BoomWeb::handleEvents() { if (!authorise()) return; sendJson(200, boomQueue.listJson(MAX_QUEUE_ITEMS_SHOWN)); } void BoomWeb::handleEventAudio() { if (!authorise()) return; String file = server_.arg("file"); if (!file.length() || file.indexOf('/') >= 0 || file.indexOf('\\') >= 0 || !file.endsWith(".wav") || !boomQueue.exists(file)) { sendJson(404, "{\"error\":\"Queued recording not found\"}"); return; } FILE* recording = fopen(boomQueue.absolutePath(file).c_str(), "rb"); if (!recording) { sendJson(500, "{\"error\":\"Could not open recording\"}"); return; } size_t remaining = boomQueue.fileSize(file); addNoCache(); server_.setContentLength(remaining); server_.send(200, "audio/wav", ""); NetworkClient& output = server_.client(); uint8_t chunk[1024]; while (remaining && output.connected()) { size_t read = fread(chunk, 1, min(sizeof(chunk), remaining), recording); if (!read) break; size_t written = 0; while (written < read && output.connected()) { size_t n = output.write(chunk + written, read - written); if (!n) break; written += n; } if (written != read) break; remaining -= read; } fclose(recording); if (remaining) Serial.printf("[Web] audio preview interrupted: %s (%u bytes left)\n", file.c_str(), unsigned(remaining)); } void BoomWeb::handleWifiScan() { if (!authorise()) return; sendJson(200, boomNetwork.scanJson()); } void BoomWeb::handleWifiSave() { if (!authorise()) return; String ssid = server_.arg("ssid"); String password = server_.arg("password"); ssid.trim(); if (!ssid.length()) { sendJson(400, "{\"error\":\"SSID is required\"}"); return; } if (!boomNetwork.saveCredentials(ssid, password)) { sendJson(500, "{\"error\":\"Could not save Wi-Fi credentials\"}"); return; } sendJson(200, "{\"ok\":true,\"rebooting\":true}"); boomState.requestReboot(1200); } void BoomWeb::handleWifiForget() { if (!authorise()) return; boomNetwork.clearCredentials(); sendJson(200, "{\"ok\":true,\"rebooting\":true}"); boomState.requestReboot(1200); } void BoomWeb::handleSensitivity() { if (!authorise()) return; if (!server_.hasArg("value")) { sendJson(400, "{\"error\":\"value is required\"}"); return; } int value = constrain(server_.arg("value").toInt(), 1, 100); boomState.setSensitivity((uint8_t)value, true); boomAudio.setSensitivity((uint8_t)value); sendJson(200, "{\"ok\":true,\"value\":" + String(value) + "}"); } void BoomWeb::handleSync() { if (!authorise()) return; if (!boomNetwork.connected()) { sendJson(409, "{\"error\":\"No station Wi-Fi connection; upload unavailable in setup-AP-only mode\"}"); return; } String file = server_.arg("file"); if (file.length() && !boomQueue.exists(file)) { sendJson(404, "{\"error\":\"Queued file not found\"}"); return; } boomState.requestSync(file); sendJson(202, "{\"ok\":true,\"queued\":true}"); } void BoomWeb::handleDiscard() { if (!authorise()) return; // Require a non-simple request header so another site cannot submit a // cross-origin form using cached HTTP Basic credentials. if (server_.header("X-BoomCast-Action") != "discard") { sendJson(403, "{\"error\":\"Discard action header required\"}"); return; } if (!boomQueue.ready()) { sendJson(503, "{\"error\":\"SD card is unavailable\"}"); return; } if (server_.arg("all") == "1") { int removed = boomQueue.clearAll(); if (removed < 0) { sendJson(500, "{\"error\":\"Could not remove every queued recording\"}"); return; } boomState.setSyncing(false, "Discarded " + String(removed) + " queued recording(s)"); sendJson(200, "{\"ok\":true,\"removed\":" + String(removed) + "}"); return; } String file = server_.arg("file"); if (!file.length() || file.indexOf('/') >= 0 || file.indexOf('\\') >= 0 || !file.endsWith(".wav")) { sendJson(400, "{\"error\":\"Valid queued filename required\"}"); return; } if (!boomQueue.exists(file)) { sendJson(404, "{\"error\":\"Queued recording not found\"}"); return; } if (!boomQueue.discard(file)) { sendJson(500, "{\"error\":\"Could not discard recording\"}"); return; } boomState.setSyncing(false, "Discarded " + file); sendJson(200, "{\"ok\":true,\"removed\":1}"); } void BoomWeb::handleTodayReset() { if (!authorise()) return; if (server_.header("X-BoomCast-Action") != "discard") { sendJson(403, "{\"error\":\"Reset action header required\"}"); return; } boomState.resetTodayEvents(); boomState.setSyncing(false, "Today count reset"); sendJson(200, "{\"ok\":true,\"today\":0}"); } void BoomWeb::handleReboot() { if (!authorise()) return; sendJson(200, "{\"ok\":true,\"rebooting\":true}"); boomState.requestReboot(900); }