Add LED web UI with AP, Wi-Fi client, mDNS and patterns
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>
This commit is contained in:
1 parent
df43820acd
commit
89f996096c
20 files changed
+1539
-18
No files matched your search
+121
@@ -0,0 +1,121 @@
|
||||
// LED strip web UI. Talks to the JSON API in main/web.cpp.
|
||||
(() => {
|
||||
const $ = (id) => document.getElementById(id);
|
||||
|
||||
async function api(path, opts) {
|
||||
const r = await fetch(path, opts);
|
||||
const j = await r.json().catch(() => ({}));
|
||||
if (!r.ok) throw new Error(j.error || r.statusText);
|
||||
return j;
|
||||
}
|
||||
|
||||
const post = (path, body) => api(path, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
const toHex = (rgb) => '#' + rgb.map((v) => v.toString(16).padStart(2, '0')).join('');
|
||||
const fromHex = (h) => [1, 3, 5].map((i) => parseInt(h.slice(i, i + 2), 16));
|
||||
|
||||
function updateOutputs() {
|
||||
$('speedOut').textContent = $('speed').value + ' ms';
|
||||
$('brightOut').textContent = $('bright').value;
|
||||
}
|
||||
|
||||
// Coalesce slider drags into one request
|
||||
let sendTimer;
|
||||
function sendPattern() {
|
||||
updateOutputs();
|
||||
clearTimeout(sendTimer);
|
||||
sendTimer = setTimeout(async () => {
|
||||
try {
|
||||
await post('/api/pattern', {
|
||||
mode: $('mode').value,
|
||||
c1: fromHex($('c1').value),
|
||||
c2: fromHex($('c2').value),
|
||||
speed_ms: Number($('speed').value),
|
||||
brightness: Number($('bright').value),
|
||||
});
|
||||
$('status').textContent = 'Applied';
|
||||
} catch (e) {
|
||||
$('status').textContent = e.message;
|
||||
}
|
||||
}, 150);
|
||||
}
|
||||
|
||||
async function loadStatus() {
|
||||
const s = await api('/api/status');
|
||||
$('apName').textContent = s.ap_ssid;
|
||||
$('staState').textContent = s.sta_connected ? 'connected (' + s.sta_ip + ')' : 'not connected';
|
||||
if (!$('mode').options.length) {
|
||||
s.modes.forEach((m) => $('mode').add(new Option(m, m)));
|
||||
}
|
||||
$('mode').value = s.mode;
|
||||
$('c1').value = toHex(s.c1);
|
||||
$('c2').value = toHex(s.c2);
|
||||
$('speed').value = s.speed_ms;
|
||||
$('bright').value = s.brightness;
|
||||
updateOutputs();
|
||||
}
|
||||
|
||||
async function loadWifi() {
|
||||
const w = await api('/api/wifi');
|
||||
if (w.ssid && !$('ssid').value) $('ssid').value = w.ssid;
|
||||
}
|
||||
|
||||
async function scan() {
|
||||
const list = $('networks');
|
||||
list.textContent = 'Scanning…';
|
||||
try {
|
||||
const { networks } = await api('/api/wifi/scan');
|
||||
list.textContent = '';
|
||||
networks.sort((a, b) => b.rssi - a.rssi).forEach((n) => {
|
||||
const li = document.createElement('li');
|
||||
const btn = document.createElement('button');
|
||||
btn.type = 'button';
|
||||
btn.className = 'net';
|
||||
btn.textContent = `${n.ssid || '(hidden)'} ${n.rssi} dBm${n.open ? '' : ' 🔒'}`;
|
||||
btn.addEventListener('click', () => { $('ssid').value = n.ssid; $('pass').focus(); });
|
||||
li.appendChild(btn);
|
||||
list.appendChild(li);
|
||||
});
|
||||
if (!networks.length) list.textContent = 'No networks found.';
|
||||
} catch (e) {
|
||||
list.textContent = e.message;
|
||||
}
|
||||
}
|
||||
|
||||
// Tabs
|
||||
document.querySelectorAll('.tab').forEach((btn) => {
|
||||
btn.addEventListener('click', () => {
|
||||
document.querySelectorAll('.tab').forEach((b) => b.classList.toggle('active', b === btn));
|
||||
$('patterns').hidden = btn.dataset.tab !== 'patterns';
|
||||
$('settings').hidden = btn.dataset.tab !== 'settings';
|
||||
});
|
||||
});
|
||||
|
||||
['mode', 'c1', 'c2', 'speed', 'bright'].forEach((id) => {
|
||||
$(id).addEventListener('input', sendPattern);
|
||||
$(id).addEventListener('change', sendPattern);
|
||||
});
|
||||
|
||||
$('scan').addEventListener('click', scan);
|
||||
|
||||
$('wifiForm').addEventListener('submit', async (ev) => {
|
||||
ev.preventDefault();
|
||||
$('wifiMsg').textContent = 'Saving…';
|
||||
try {
|
||||
const j = await post('/api/wifi', { ssid: $('ssid').value, password: $('pass').value });
|
||||
$('wifiMsg').textContent = j.message || 'Saved';
|
||||
$('pass').value = '';
|
||||
setTimeout(() => loadStatus().catch(() => {}), 4000);
|
||||
} catch (e) {
|
||||
$('wifiMsg').textContent = e.message;
|
||||
}
|
||||
});
|
||||
|
||||
loadStatus().catch((e) => { $('status').textContent = e.message; });
|
||||
loadWifi().catch(() => {});
|
||||
setInterval(() => loadStatus().catch(() => {}), 5000);
|
||||
})();
|
||||
@@ -0,0 +1,64 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>LED strip</title>
|
||||
<link rel="stylesheet" href="/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>LED strip</h1>
|
||||
<nav>
|
||||
<button class="tab active" data-tab="patterns">Patterns</button>
|
||||
<button class="tab" data-tab="settings">Settings</button>
|
||||
</nav>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
<section id="patterns">
|
||||
<label>Pattern
|
||||
<select id="mode"></select>
|
||||
</label>
|
||||
<div class="row">
|
||||
<label>Colour
|
||||
<input type="color" id="c1" value="#ff0000">
|
||||
</label>
|
||||
<label>Colour 2
|
||||
<input type="color" id="c2" value="#000000">
|
||||
</label>
|
||||
</div>
|
||||
<label>Speed <output id="speedOut"></output>
|
||||
<input type="range" id="speed" min="10" max="3000" step="10">
|
||||
</label>
|
||||
<label>Brightness <output id="brightOut"></output>
|
||||
<input type="range" id="bright" min="0" max="255" step="1">
|
||||
</label>
|
||||
<p id="status" class="muted" role="status"></p>
|
||||
</section>
|
||||
|
||||
<section id="settings" hidden>
|
||||
<h2>Access point</h2>
|
||||
<p>Network <strong id="apName">…</strong> (open). Address <strong>192.168.4.1</strong>.</p>
|
||||
|
||||
<h2>Wi-Fi client</h2>
|
||||
<p>Status: <span id="staState">…</span></p>
|
||||
<button id="scan" type="button">Scan for networks</button>
|
||||
<ul id="networks" class="list"></ul>
|
||||
|
||||
<form id="wifiForm">
|
||||
<label>Network name (SSID)
|
||||
<input id="ssid" required maxlength="32" autocomplete="off">
|
||||
</label>
|
||||
<label>Password <span class="muted">(blank for open networks)</span>
|
||||
<input id="pass" type="password" maxlength="64" autocomplete="off">
|
||||
</label>
|
||||
<button type="submit">Save and connect</button>
|
||||
</form>
|
||||
<p id="wifiMsg" class="muted" role="status"></p>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<script src="/app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
:root {
|
||||
--bg: #f6f6f7;
|
||||
--fg: #1d1d1f;
|
||||
--muted: #6e6e73;
|
||||
--card: #ffffff;
|
||||
--accent: #2563eb;
|
||||
--border: #d2d2d7;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root:not([data-theme="light"]) {
|
||||
--bg: #111114;
|
||||
--fg: #f2f2f5;
|
||||
--muted: #9a9aa3;
|
||||
--card: #1c1c21;
|
||||
--accent: #5b8cff;
|
||||
--border: #34343c;
|
||||
}
|
||||
}
|
||||
|
||||
:root[data-theme="dark"] {
|
||||
--bg: #111114;
|
||||
--fg: #f2f2f5;
|
||||
--muted: #9a9aa3;
|
||||
--card: #1c1c21;
|
||||
--accent: #5b8cff;
|
||||
--border: #34343c;
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; }
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 0 16px 32px;
|
||||
background: var(--bg);
|
||||
color: var(--fg);
|
||||
font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
|
||||
}
|
||||
|
||||
header {
|
||||
max-width: 560px;
|
||||
margin: 0 auto;
|
||||
padding: 16px 0 8px;
|
||||
}
|
||||
|
||||
h1 { margin: 0 0 12px; font-size: 1.5rem; }
|
||||
h2 { font-size: 1.1rem; margin: 20px 0 8px; }
|
||||
|
||||
nav { display: flex; gap: 8px; }
|
||||
|
||||
main {
|
||||
max-width: 560px;
|
||||
margin: 0 auto;
|
||||
background: var(--card);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 12px;
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
section > * + * { margin-top: 12px; }
|
||||
|
||||
label {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
output { font-weight: 400; color: var(--muted); }
|
||||
|
||||
.row { display: flex; gap: 16px; flex-wrap: wrap; }
|
||||
.row > label { flex: 1 1 140px; }
|
||||
|
||||
select, input[type="text"], input:not([type]), input[type="password"] {
|
||||
font: inherit;
|
||||
padding: 8px 10px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 8px;
|
||||
background: var(--bg);
|
||||
color: var(--fg);
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
input[type="color"] {
|
||||
width: 100%;
|
||||
height: 44px;
|
||||
padding: 2px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 8px;
|
||||
background: var(--bg);
|
||||
}
|
||||
|
||||
input[type="range"] { width: 100%; accent-color: var(--accent); }
|
||||
|
||||
button {
|
||||
font: inherit;
|
||||
padding: 8px 14px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 8px;
|
||||
background: var(--bg);
|
||||
color: var(--fg);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
button.tab.active,
|
||||
button[type="submit"] {
|
||||
background: var(--accent);
|
||||
border-color: var(--accent);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.list { list-style: none; padding: 0; margin: 0; }
|
||||
.list li + li { margin-top: 6px; }
|
||||
button.net { width: 100%; text-align: left; }
|
||||
|
||||
.muted { color: var(--muted); font-size: 0.9rem; }
|
||||
|
||||
[hidden] { display: none !important; }
|
||||
|
||||
form { display: flex; flex-direction: column; gap: 12px; }
|
||||
form button { align-self: flex-start; }
|
||||
Reference in new issue
Block a user