-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmultimeter.html
More file actions
396 lines (353 loc) · 14 KB
/
multimeter.html
File metadata and controls
396 lines (353 loc) · 14 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Owon B41T+ with web bluetooth example</title>
<script src="./dist/bt-owon-bt.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.6/dist/chart.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns@3.0.0/dist/chartjs-adapter-date-fns.bundle.min.js"></script>
</head>
<body>
<style>
.output {
background-color: #f0f0f0;
border-radius: 0.75em;
display: block;
margin: 0.5em;
padding: 0.5em;
}
.status {
background-color: #b0b0b0;
border-radius: 0.75em;
display: block;
margin: 0.5em;
padding: 0.5em;
}
#status {
margin: .5em 0;
font-style: italic;
}
#log {
margin: .5em 0;
white-space: pre-wrap;
}
#status:empty,
#log:empty,
#content:empty {
display: none;
}
.highlight {
border-radius: 0.75em;
border: 1px solid #f0f0f0;
display: block;
margin: 0.5em;
overflow-x: auto;
padding: 0.5em;
}
code {
font-family: Inconsolata, Consolas, monospace;
}
.lcd-container {
margin: 1rem 0;
padding: 1rem;
background-color: #2a2a2a;
border-radius: 0.5rem;
text-align: center;
}
.status-container {
display: flex;
align-items: center;
gap: 1rem;
margin: 1rem 0;
}
.btn-group {
gap: 0.5rem;
}
.multimeter-image {
max-width: 100%;
height: auto;
}
</style>
<header class="container mt-3">
<h1>Owon B41T+ Multimeter</h1>
</header>
<div class="container">
<div class="row">
<div class="col">
<p>
This webpage uses Web Bluetooth to get real-time readings from your Owon multimeter. A
Bluetooth 4.0+ capable device is required.
</p>
<ol>
<li>Enable Bluetooth on your multimeter first.</li>
<li>Click START and select "BDM" or "LILLIPUT" device when prompted.</li>
<li>Measurements will be displayed below in real-time.</li>
</ol>
<div class="status-container">
<strong>Status:</strong>
<div id="status" class="status">Disconnected</div>
</div>
<div class="btn-group d-flex gap-2 mb-3" role="group" aria-label="Multimeter controls">
<button id="btnStart" class="btn btn-success">START</button>
<button id="btnStop" class="btn btn-danger">STOP</button>
<button id="btnDebugLog" class="btn btn-info">ENABLE DEBUG</button>
</div>
<strong>Latest data:</strong>
<div id="measure" class="output"></div>
<!-- LCD Display Container -->
<div id="lcd-container" class="lcd-container"></div>
<!-- Plugin Script -->
<script src="./dist/lcd.js"></script>
</div>
<div class="col">
<img src="dmm.png" alt="Owon B41T+ Digital Multimeter" class="multimeter-image" />
</div>
<div class="col">
<strong>Statistics:</strong>
<div id="stats" class="output"></div>
<details>
<summary><strong>Developer Information</strong></summary>
<p><small>Check Developer Tools console for detailed logs.</small></p>
<ul>
<li><code>OwonBT.SetLogLevel(0-5)</code> - Adjust logging (0=TRACE, 5=SILENT)</li>
<li><code>OwonBT.SetPacketLog(true)</code> - Enable packet logging to localStorage</li>
</ul>
</details>
<div class="alert alert-warning mt-3" role="alert">
<strong>Note:</strong> With auto-range active, the meter may send inconsistent data during range transitions.
For consistent results, disable auto-range on your multimeter.
</div>
</div>
</div>
</div>
<section class="container mt-4">
<h2>Real-time Chart</h2>
<div class="chart-container" style="position: relative; height: 400px;">
<canvas id="myChart" aria-label="Multimeter measurements over time"></canvas>
</div>
</section>
<script>
// Global variables
let myChart = null;
let lcd = null;
// Initialize LCD display
function initializeLCD() {
try {
const lcdContainer = document.getElementById("lcd-container");
if (lcdContainer && typeof LCD !== 'undefined') {
lcd = new LCD({
elem: lcdContainer,
rows: 1,
columns: 10,
pixelSize: 4,
pixelColor: "#00ff00"
});
lcd.writeString({ string: "READY", offset: 0 });
} else {
console.warn('LCD library not available or container not found');
}
} catch (error) {
console.error('Failed to initialize LCD:', error);
}
}
async function updateGraph(chartData) {
if (!chartData || chartData.length === 0) return;
try {
const data = {
datasets: [{
label: chartData[0]?.Function || 'Measurement',
backgroundColor: 'rgba(54, 162, 235, 0.2)',
borderColor: 'rgb(54, 162, 235)',
data: chartData.map(item => ({
x: item.Timestamp,
y: item.Value
})),
fill: false,
tension: 0.1
}]
};
const config = {
type: 'line',
data: data,
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: {
type: 'time',
time: {
unit: 'second',
displayFormats: {
second: 'HH:mm:ss'
}
},
title: {
display: true,
text: 'Time'
}
},
y: {
beginAtZero: false,
title: {
display: true,
text: 'Value'
}
}
},
plugins: {
legend: {
display: true
}
}
}
};
if (myChart === null) {
const ctx = document.getElementById('myChart');
if (ctx) {
myChart = new Chart(ctx, config);
}
} else {
myChart.data = data;
myChart.update('none');
}
} catch (error) {
console.error('Failed to update chart:', error);
}
}
</script>
<script>
// Application state
let chartData = [];
let lastData = null;
let previousData = null;
let isDebugEnabled = false;
function updateStatus() {
try {
const statusElement = document.getElementById("status");
const statsElement = document.getElementById("stats");
const measureElement = document.getElementById("measure");
if (!OwonBT || !OwonBT.btState) {
statusElement.textContent = "Library not loaded";
return;
}
statusElement.textContent = OwonBT.btState.state || "Unknown";
// Format stats for better readability
const stats = OwonBT.btState.stats || {};
statsElement.innerHTML = Object.entries(stats)
.map(([key, value]) => `<strong>${key}:</strong> ${value}`)
.join('<br/>');
if (OwonBT.btState.formattedResponse) {
const timestamp = OwonBT.btState.parsedResponse?.DateTime || new Date();
measureElement.innerHTML = `
${OwonBT.btState.formattedResponse}
<br/><small class="text-muted">(${timestamp})</small>
`;
updateLCDDisplay();
processNewMeasurement();
} else {
measureElement.innerHTML = '<span class="text-muted">No data available</span>';
lastData = null;
}
} catch (error) {
console.error('Error updating status:', error);
}
}
function updateLCDDisplay() {
if (!lcd || !OwonBT.btState.parsedResponse) return;
try {
lcd.clearScreen();
if (OwonBT.btState.parsedResponse.Overload) {
lcd.writeString({ string: "OL", offset: 0 });
} else {
const measurement = OwonBT.btState.parsedResponse.Measurement;
const displayValue = measurement.toFixed(4).toString().padStart(10, " ");
lcd.writeString({ string: displayValue, offset: 0 });
}
} catch (error) {
console.error('Error updating LCD:', error);
}
}
function processNewMeasurement() {
const currentData = OwonBT.btState.parsedResponse;
if (!currentData) return;
let isNewSample = false;
if (!previousData || currentData.Timestamp.getTime() !== previousData.Timestamp.getTime()) {
if (!previousData || previousData.Function !== currentData.Function) {
chartData = []; // Reset chart on function change
}
previousData = currentData;
isNewSample = true;
}
lastData = currentData;
if (isNewSample) {
chartData.push({
Value: currentData.Overload ? null : currentData.Value,
Timestamp: currentData.Timestamp.getTime(),
Function: currentData["Function description"]
});
// Keep only last 75 data points
if (chartData.length > 75) {
chartData.shift();
}
updateGraph(chartData);
}
}
// Event handlers
async function handleStart() {
try {
document.getElementById("btnStart").disabled = true;
await OwonBT.Start();
} catch (error) {
console.error('Failed to start:', error);
alert('Failed to start connection. Please check console for details.');
} finally {
document.getElementById("btnStart").disabled = false;
}
}
async function handleStop() {
try {
document.getElementById("btnStop").disabled = true;
await OwonBT.Stop();
if (lcd) {
lcd.clearScreen();
lcd.writeString({ string: "STOPPED", offset: 0 });
}
} catch (error) {
console.error('Failed to stop:', error);
} finally {
document.getElementById("btnStop").disabled = false;
}
}
function toggleDebugLog() {
try {
isDebugEnabled = !isDebugEnabled;
OwonBT.SetLogLevel(isDebugEnabled ? 1 : 3);
const btn = document.getElementById("btnDebugLog");
btn.textContent = isDebugEnabled ? "DISABLE DEBUG" : "ENABLE DEBUG";
btn.className = isDebugEnabled ? "btn btn-warning" : "btn btn-info";
} catch (error) {
console.error('Failed to toggle debug:', error);
}
}
// Initialize application
function initializeApp() {
initializeLCD();
// Set up event listeners
document.getElementById("btnStart").addEventListener('click', handleStart);
document.getElementById("btnStop").addEventListener('click', handleStop);
document.getElementById("btnDebugLog").addEventListener('click', toggleDebugLog);
// Start status updates
setInterval(updateStatus, 450);
}
// Start when DOM is loaded
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initializeApp);
} else {
initializeApp();
}
</script>
</body>
</html>