-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathco2meter.html
257 lines (222 loc) · 6.51 KB
/
co2meter.html
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
<html>
<head>
<title>CO₂ Meter</title>
<meta charset="UTF-8">
<script src="https://cdn.jsdelivr.net/npm/chart.js@^3"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@^2"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-moment@^1"></script>
<style>
table {
border-collapse: collapse;
}
table td, table th {
border: 1px solid black;
padding: 3px;
}
table tr:first-child td, table tr:first-child th {
border-top: 0;
}
table tr td:first-child, table tr th:first-child {
border-left: 0;
}
table tr:last-child td, table tr:last-child th {
border-bottom: 0;
}
table tr td:last-child, table tr th:last-child {
border-right: 0;
}
</style>
</head>
<body>
<p>
<button id="connect">Connect</button>
</p>
<p>
Some devices encrypt the data sent to the host. Check this box if no data
is recorded or it appears garbled:
</p>
<p>
<input type="checkbox" id="encrypted">
<label for="encrypted">Encrypted data</label>
</p>
<p>
Units:
<input type="radio" name="units" id="kelvin">
<label for="kelvin">Kelvin</label>
<input type="radio" name="units" id="celsius" checked>
<label for="celsius">Celsius</label>
<input type="radio" name="units" id="fahrenheit">
<label for="fahrenheit">Fahrenheit</label>
</p>
<div>
<canvas id="chart"></canvas>
</div>
<table id="table">
<tr>
<th>Timestamp</th>
<th>CO₂</th>
<th>Temperature</th>
</tr>
</table>
<script>
const connectButton = document.getElementById('connect');
connectButton.addEventListener('click', connect);
const table = document.getElementById('table');
const chartConfig = {
type: 'line',
data: {
datasets: [
{
label: "Temperature",
data: [],
borderColor: 'rgba(255, 0, 0, 1)',
backgroundColor: 'rgba(255, 0, 0, 0.5)',
yAxisID: 'y'
},
{
label: "CO₂",
data: [],
borderColor: 'rgba(0, 0, 255, 1)',
backgroundColor: 'rgba(0, 0, 255, 0.5)',
yAxisID: 'y1'
}
]
},
options: {
scales: {
x: {
type: 'timeseries'
},
y: {
type: 'linear',
display: true,
position: 'left',
grid: {
drawOnChartArea: false
}
},
y1: {
type: 'linear',
display: true,
position: 'right'
}
}
}
};
const chart = new Chart(document.getElementById('chart'), chartConfig);
const celsiusButton = document.getElementById('celsius');
const fahrenheitButton = document.getElementById('fahrenheit');
const encrypted = document.getElementById('encrypted');
const key = new Uint8Array([0xc4, 0xc6, 0xc0, 0x92, 0x40, 0x23, 0xdc, 0x96]);
let currentCO2 = '';
let currentTemperature = '';
// This decryption routine is a JavaScript translation of the algorithm from
// https://hackaday.io/project/5301/logs.
function decrypt(key, data) {
const cstate = [0x48, 0x74, 0x65, 0x6D, 0x70, 0x39, 0x39, 0x65];
const shuffle = [2, 4, 0, 7, 1, 6, 5, 3];
const phase1 = new Uint8Array(8);
shuffle.forEach((value, index) => {
phase1[value] = data[index];
});
const phase2 = new Uint8Array(8);
for (let i = 0; i < 8; ++i) {
phase2[i] = phase1[i] ^ key[i];
}
const phase3 = new Uint8Array(8);
for (let i = 0; i < 8; ++i) {
phase3[i] = ((phase2[i] >> 3) | (phase2[(i-1+8)%8] << 5)) & 0xff;
}
const ctmp = new Uint8Array(8);
for (let i = 0; i < 8; ++i) {
ctmp[i] = ((cstate[i] >> 4) | (cstate[i]<<4)) & 0xff;
}
const out = new Uint8Array(8);
for (let i = 0; i < 8; ++i) {
out[i] = (0x100 + phase3[i] - ctmp[i]) & 0xff;
}
return out;
}
function kelvinToCelsius(value) {
return value - 273.15;
}
function kelvinToFahrenheit(value) {
return 1.8 * (value - 273.15) + 32;
}
function onInputReport(report) {
const date = new Date();
let data = new Uint8Array(report.data.buffer,
report.data.byteOffset,
report.data.byteLength);
data = encrypted.checked ? decrypt(key, data) : data;
const op = data[0];
let val = data[1] << 8 | data[2];
let newData = false;
if (op == 0x42) {
let unit = 'K';
val = val / 16;
if (celsiusButton.checked) {
unit = 'C';
val = kelvinToCelsius(val);
}
if (fahrenheitButton.checked) {
unit = 'F';
val = kelvinToFahrenheit(val);
}
currentTemperature = `${val.toFixed(2)}° ${unit}`;
newData = true;
chart.data.datasets[0].data.push({ x: date, y: val });
} else if (op == 0x50) {
currentCO2 = `${val} ppm`;
newData = true;
chart.data.datasets[1].data.push({ x: date, y: val });
} else {
console.log(`Unknown operation: ${op}`, data);
}
if (newData) {
const row = table.insertRow(1);
row.innerHTML = `
<td>${date}</td>
<td>${currentCO2}</td>
<td>${currentTemperature}</td>
`;
chart.update();
}
}
let currentDevice = null;
async function connectToDevice(device) {
currentDevice = device;
try {
currentDevice.addEventListener('inputreport', onInputReport);
await currentDevice.open();
await currentDevice.sendFeatureReport(0, key);
connectButton.disabled = true;
} catch {
currentDevice.close().then(() => {});
currentDevice.removeEventListener('inputreport', onInputReport);
currentDevice = null;
connectButton.disabled = false;
}
}
async function connect() {
const devices = await navigator.hid.requestDevice({
filters: [{vendorId: 0x04D9, productId: 0xA052}]
});
await connectToDevice(devices[0]);
}
navigator.hid.addEventListener('connect', async (e) => {
if (currentDevice)
return;
connectToDevice(e.device);
});
navigator.hid.addEventListener('disconnect', async (e) => {
if (e.device === currentDevice) {
currentDevice.close().then(() => {});
currentDevice.removeEventListener('inputreport', onInputReport);
currentDevice = null;
connectButton.disabled = false;
}
});
</script>
</body>
</html>