-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
186 lines (168 loc) · 7.22 KB
/
Copy pathindex.html
File metadata and controls
186 lines (168 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<!DOCTYPE html>
<!--
ble-config.html — Web Bluetooth field-configuration page.
Works in Chrome / Edge on Android and desktop. (iOS Safari has no Web
Bluetooth; use the Bluefy browser app or nRF Connect there.)
Serve over HTTPS or open as a local file with Chrome flag allowances;
easiest: host anywhere (GitHub Pages works) or run: python3 -m http.server
and open via http://localhost (localhost is exempt from the HTTPS rule).
-->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Field Config</title>
<style>
:root { --accent:#0a7cff; --bg:#111418; --card:#1b2027; --text:#e8eaed; --mut:#9aa0a6; }
* { box-sizing:border-box; font-family:system-ui,sans-serif; }
body { margin:0; background:var(--bg); color:var(--text); }
.wrap { max-width:480px; margin:0 auto; padding:16px; }
h1 { font-size:1.2rem; margin:8px 0 16px; }
.card { background:var(--card); border-radius:12px; padding:16px; margin-bottom:16px; }
label { display:block; font-size:.8rem; color:var(--mut); margin:12px 0 4px; }
input { width:100%; padding:10px; border-radius:8px; border:1px solid #333a44;
background:#0d1013; color:var(--text); font-size:1rem; }
button { width:100%; padding:12px; margin-top:12px; border:0; border-radius:8px;
background:var(--accent); color:#fff; font-size:1rem; font-weight:600; }
button.secondary { background:#2a313b; }
button.danger { background:#b3261e; }
button:disabled { opacity:.4; }
#log { font-family:monospace; font-size:.75rem; color:var(--mut);
white-space:pre-wrap; max-height:160px; overflow-y:auto; }
.pill { display:inline-block; padding:2px 10px; border-radius:99px; font-size:.75rem; }
.ok { background:#0f5132; } .off { background:#4a1b1a; }
</style>
</head>
<body>
<div class="wrap">
<h1>Field Config <span id="state" class="pill off">disconnected</span></h1>
<div class="card">
<label>Device PIN (printed on the unit)</label>
<input id="pin" type="tel" inputmode="numeric" maxlength="6" placeholder="6-digit PIN">
<button id="btnConnect">Connect to device</button>
</div>
<div class="card" id="form" style="display:none">
<label>Device name</label>
<input id="dev_name" placeholder="north-gate">
<label>Wi-Fi SSID</label>
<input id="wifi_ssid">
<label>Wi-Fi password</label>
<input id="wifi_pass" type="password" placeholder="(unchanged if blank)">
<label>MQTT host</label>
<input id="mqtt_host" placeholder="mqtt.example.com">
<label>MQTT port</label>
<input id="mqtt_port" type="number" placeholder="8883">
<label>MQTT username</label>
<input id="mqtt_user">
<label>MQTT password</label>
<input id="mqtt_pass" type="password" placeholder="(unchanged if blank)">
<label>Topic prefix</label>
<input id="topic_prefix" placeholder="nodes">
<button id="btnSave">Save & reboot device</button>
<button id="btnRefresh" class="secondary">Re-read device status</button>
<button id="btnFactory" class="danger">Factory reset</button>
</div>
<div class="card"><div id="log"></div></div>
</div>
<script>
const SVC = 'f1e0c001-2f1a-4b6e-9d3a-464331000001';
const CONFIG = 'f1e0c001-2f1a-4b6e-9d3a-464331000002';
const STATUS = 'f1e0c001-2f1a-4b6e-9d3a-464331000003';
const CONTROL = 'f1e0c001-2f1a-4b6e-9d3a-464331000004';
let dev = null, chConfig = null, chStatus = null, chControl = null;
const enc = new TextEncoder(), dec = new TextDecoder();
const $ = id => document.getElementById(id);
const log = m => { $('log').textContent += m + '\n'; $('log').scrollTop = 1e9; };
function setState(ok) {
const el = $('state');
el.textContent = ok ? 'connected' : 'disconnected';
el.className = 'pill ' + (ok ? 'ok' : 'off');
$('form').style.display = ok ? '' : 'none';
}
async function connect() {
try {
dev = await navigator.bluetooth.requestDevice({
filters: [{ namePrefix: 'FC-' }],
optionalServices: [SVC]
});
dev.addEventListener('gattserverdisconnected', () => { setState(false); log('Disconnected.'); });
log('Connecting to ' + dev.name + '…');
const server = await dev.gatt.connect();
const svc = await server.getPrimaryService(SVC);
chConfig = await svc.getCharacteristic(CONFIG);
chStatus = await svc.getCharacteristic(STATUS);
chControl = await svc.getCharacteristic(CONTROL);
// Unlock with the device PIN before anything else.
const pin = $('pin').value.trim();
try {
await chControl.writeValueWithResponse(enc.encode('pin:' + pin));
} catch (_) {
log('PIN rejected. Check the 6 digits on the unit label and retry.');
dev.gatt.disconnect();
return;
}
setState(true);
log('Connected and unlocked.');
await refresh();
} catch (e) { log('Error: ' + e.message); }
}
async function refresh() {
const raw = await chStatus.readValue();
const st = JSON.parse(dec.decode(raw));
if (st.locked) { log('Device is locked — wrong PIN.'); return; }
log('Status: ' + JSON.stringify(st));
$('dev_name').value = st.dev_name ?? '';
$('wifi_ssid').value = st.wifi_ssid ?? '';
$('mqtt_host').value = st.mqtt_host ?? '';
$('mqtt_port').value = st.mqtt_port ?? 8883;
$('mqtt_user').value = st.mqtt_user ?? '';
$('topic_prefix').value= st.topic_prefix ?? '';
$('wifi_pass').placeholder = st.wifi_pass_set ? '(set — leave blank to keep)' : '';
$('mqtt_pass').placeholder = st.mqtt_pass_set ? '(set — leave blank to keep)' : '';
}
// Write possibly-long payloads in MTU-safe chunks. Firmware treats a chunk
// starting with '{' as the start of a fresh document.
async function writeChunked(ch, str) {
const bytes = enc.encode(str);
const CHUNK = 180;
for (let i = 0; i < bytes.length; i += CHUNK) {
await ch.writeValueWithResponse(bytes.slice(i, i + CHUNK));
}
}
async function save() {
const cfg = {
dev_name: $('dev_name').value.trim(),
wifi_ssid: $('wifi_ssid').value.trim(),
mqtt_host: $('mqtt_host').value.trim(),
mqtt_port: parseInt($('mqtt_port').value) || 8883,
mqtt_user: $('mqtt_user').value.trim(),
topic_prefix: $('topic_prefix').value.trim(),
};
// Blank password fields mean "keep existing" — omit the key entirely.
if ($('wifi_pass').value) cfg.wifi_pass = $('wifi_pass').value;
if ($('mqtt_pass').value) cfg.mqtt_pass = $('mqtt_pass').value;
try {
log('Writing config…');
await writeChunked(chConfig, JSON.stringify(cfg));
await chControl.writeValueWithResponse(enc.encode('save'));
log('Saved. Rebooting device…');
await chControl.writeValueWithResponse(enc.encode('reboot'));
} catch (e) { log('Error: ' + e.message); }
}
async function factoryReset() {
if (!confirm('Erase ALL stored settings on this device?')) return;
try { await chControl.writeValueWithResponse(enc.encode('reset')); }
catch (e) { /* device reboots mid-write; expected */ }
log('Factory reset sent.');
}
$('btnConnect').onclick = connect;
$('btnSave').onclick = save;
$('btnRefresh').onclick = () => refresh().catch(e => log('Error: ' + e.message));
$('btnFactory').onclick = factoryReset;
if (!navigator.bluetooth) {
log('This browser has no Web Bluetooth. Use Chrome on Android/desktop, or Bluefy on iOS.');
$('btnConnect').disabled = true;
}
</script>
</body>
</html>