-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_serial_TMPHMD_SHT31_with_Graph.html
More file actions
303 lines (274 loc) · 7.46 KB
/
web_serial_TMPHMD_SHT31_with_Graph.html
File metadata and controls
303 lines (274 loc) · 7.46 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
<!DOCTYPE html>
<html lang="en">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.9.1/chart.min.js"></script>
<head>
<meta charset="UTF-8" />
<title>TMP/HMD SENSOR SHT31 with Graph</title>
<style>
h1 { font-size:20pt; background-color:#4F4F4F; color:#FFFFFF; font-weight : normal;}
h2 { font-size:16pt; }
h3 { font-size:8pt; }
body { font-size:12pt; color:#444444; }
footer{ font-size:4pt; }
</style>
</head>
<body>
<h1><span>温度/湿度センサ SHT31 グラフツール </span></h1>
<style>
#tmp_chart {max-width:800px;max-height:300px;}
#hmd_chart {max-width:800px;max-height:300px;}
</style>
<canvas id="tmp_chart"></canvas>
<br/>
<canvas id="hmd_chart"></canvas>
<br/>
<button id="connect-button" type="button" disabled>接続</button>
<input id="start-button" type="button" value="開始">
<button id="save-button" type="button">保存</button>
<button id="clear-button" type="button">クリア</button>
<h3>
<span>
本ツールは<a href="https://www.switch-science.com/catalog/6214/">USBシリアルI2C変換基板</a>とブラウザでI2Cデバイスのテストをするツールです</span>
</h3>
<h3><span>オフラインやローカル環境でも動作可能です</span></h3>
<h3><span>ブラウザはEdgeもしくはChromeのみ対応しています</span></h3>
<footer>
<p>Copyright (c) 2022 Crescent All Rights Reserved.</p>
</footer>
<script>
/* TMP/HMD SENSOR SHT31 with Graph Tool
* Copyright (c) 2022
* K.Watanabe,Crescent
* Released under the MIT license
* http://opensource.org/licenses/mit-license.php
*/
const connectButton = document.getElementById ('connect-button');
const startButton = document.getElementById ('start-button');
const saveButton = document.getElementById ('save-button');
const clearButton = document.getElementById ('clear-button');
let tChart = document.getElementById('tmp_chart');
let hChart = document.getElementById('hmd_chart');
let tdata = {
labels: [],
datasets: [{
label: '温度',
data: [],
borderColor: 'rgba(100, 100, 255, 1)'
}]
};
let hdata = {
labels: [],
datasets: [{
label: '湿度',
data: [],
borderColor: 'rgba(100, 100, 255, 1)'
}]
};
let toptions = {
scales: {
x:{title:{display: true, text: '時刻'}},
y:{title:{display: true, text: '温度℃'}}
},
plugins:{legend:{display: false}}
};
let hoptions = {
scales: {
x:{title:{display: true, text: '時刻'}},
y:{title:{display: true, text: '湿度%'}}
},
plugins:{legend:{display: false}}
};
let tmpChart = new Chart(tChart, {
type: 'line',
data: tdata,
options: toptions
});
let hmdChart = new Chart(hChart, {
type: 'line',
data: hdata,
options: hoptions
});
let port;
let keepReading = true;
let reader;
let dataIndex = 0;
let str = "";
let closed;
let dataArry = Array(6);
let chartIndex=0;
let IsRunning = false;
let IsDataReady = false;
let nowTmp = 0.0;
let nowHmd = 0.0;
//Wait Function
function Sleep( milli_second )
{
var start = new Date();
while( new Date() - start < milli_second );
}
//Graph Clear
clearButton.addEventListener('click', (event) =>
{
chartIndex=0;
tdata.labels=[];
tdata.datasets[0].data=[];
hdata.labels=[];
hdata.datasets[0].data=[];
tmpChart.update();
hmdChart.update();
});
//Save Data
saveButton.addEventListener('click', (event) =>
{
var strCsv = "time,temp,humd,\n";
for(var i=0; i<chartIndex; i++)
{
strCsv = strCsv + tmpChart.data.labels[i] + ","
+ tmpChart.data.datasets[0].data[i] + ","
+ hmdChart.data.datasets[0].data[i] + ",\n";
}
var doc = document.createElement("a");
doc.href = 'data:text/csv,' + encodeURIComponent(strCsv);
doc.download = "SHT31.csv";
doc.click();
});
//Com Open & Recieve Data
async function getReader()
{
port = await navigator.serial.requestPort({});
await port.open({ baudRate: 9600 });
//Set Init
if (port && port.writable)
{
const bytes1 = new Uint8Array([0x53, 0x88, 0x02, 0x30, 0xA2, 0x50]);
const bytes2 = new Uint8Array([0x53, 0x88, 0x02, 0x30, 0x41, 0x50]);
const bytes3 = new Uint8Array([0x53, 0x88, 0x02, 0x30, 0x66, 0x50]);
const writer = port.writable.getWriter();
dataIndex=0;
writer.write(bytes1);
Sleep(30);
writer.write(bytes2);
Sleep(30);
writer.write(bytes3);
writer.releaseLock();
}
connectButton.innerText = '切断';
startButton.disabled = false;
//Start Stop Control
startButton.addEventListener('click', (event) =>
{
IsRunning=IsRunning?false:true;
startButton.value=IsRunning?"停止":"開始";
});
//Interval Timer 2000msec=2sec
setInterval(function()
{
//console.log("Interval");
if(IsDataReady==false)
getTmpHmdData();
if(IsRunning && IsDataReady)
{
const dt = new Date();
const d = new Intl.DateTimeFormat("ja-jp", {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
}).format(dt);
tmpChart.data.datasets[0].data[chartIndex] = nowTmp;
hmdChart.data.datasets[0].data[chartIndex] = nowHmd;
tmpChart.data.labels[chartIndex] = d;
hmdChart.data.labels[chartIndex] = d;
tmpChart.update();
hmdChart.update();
chartIndex=chartIndex+1;
IsDataReady=false;
}
},2000);
//Get TEMP & HUMD
function getTmpHmdData()
{
if (port && port.writable)
{
const bytes1 = new Uint8Array([0x53, 0x88, 0x02, 0x24, 0x00, 0x50]);
const bytes2 = new Uint8Array([0x53, 0x89, 0x06, 0x50]);
const writer = port.writable.getWriter();
dataIndex=0;
writer.write(bytes1);
Sleep(30);
writer.write(bytes2);
writer.releaseLock();
//console.log("Send");
}
}
while (port.readable && keepReading)
{
reader = port.readable.getReader();
try {
while (true)
{
const { value, done } = await reader.read();
if (done)
{
reader.releaseLock();
break;
}
if (value)
{
//console.log(value);
//console.log(dataIndex);
dataArry[dataIndex] = value;
if(dataIndex==5)
{
var t = -45.0+((dataArry[0]*256.0+dataArry[1]*1.0)*175.0/65535.0);
var h = 100.0*(dataArry[3]*256+dataArry[4]*1.0)/65535.0;
nowTmp = Math.round(10*t)/10;
nowHmd = Math.round(10*h)/10;
var str=" " + nowTmp + " ℃ " + nowHmd + " %";
console.log(str);
dataIndex=0;
IsDataReady=true;
}
else
{
dataIndex = dataIndex+1;
}
}
}
}
catch (error)
{
}
finally
{
reader.releaseLock();
await port.close();
}
}
}
if ('serial' in navigator)
{
connectButton.addEventListener('click', async ()=>
{
if (port)
{
keepReading = false;
connectButton.innerText = '接続';
startButton.disabled = true;
reader.cancel();
await closed;
port = undefined;
}
else
{
closed = getReader();
}
});
connectButton.disabled = false;
}
else
{
alert('お使いのブラウザは対応していません Edgeもしくはchromeを使用してください');
}
</script>
</body>
</html>