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:
jimmyandClaude Haiku 5.5 committed 2026-11-11 00:02:49 +13:00
1 parent df43820acd
commit 89f996096c
20 files changed
+1539 -18

No files matched your search

+121
View File
@@ -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);
})();