-
Notifications
You must be signed in to change notification settings - Fork 2
/
cypressWSN.js
83 lines (70 loc) · 3.36 KB
/
cypressWSN.js
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
(function() {
var gattip = null;
var count = 0;
class CypressWSN {
constructor() {
this.temperature = '';
this.humidity = '';
this.time = '';
this.tempData = [];
this.humidityData = [];
this.peripherals = {};
gattip = navigator.bluetooth.gattip;
gattip.once('ready', function(gateway) {
console.log('ready');
function onScan(peripheral) {
var mfrData;
mfrData = peripheral.getMfrData('0131');
if (mfrData) {
mfrData = mfrData.toUpperCase();
if (mfrData.indexOf("0005000100001000800000805F9B0131") > -1) {
if (!cypressWSN.peripherals[peripheral.uuid]) {
peripheral.tempGraphData = [{
values: [],
key: 'Temperature'
}];
peripheral.humidityGraphData = [{
values: [],
key: 'Humidity'
}];
}
cypressWSN.peripherals[peripheral.uuid] = peripheral;
var data = mfrData.substr(mfrData.lastIndexOf("C3") - 4, 4);
var hex_humidity = data[0] + data[1] + '0' + '1';
var hex_temperature = data[2] + data[3] + '0' + '0';
var temperature = Math.round((parseInt(hex_temperature, 16) * 175.72 / 65536 - 46.85) * 100) / 100;
var humidity = Math.round((parseInt(hex_humidity, 16) * 125 / 65536 - 6) * 100) / 100;
var d = new Date();
var currDate = d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
if (!isNaN(Number(temperature))) {
cypressWSN.peripherals[peripheral.uuid].tempData = {
timeNum: count,
date: currDate,
temp: Number(temperature)
}
}
if (!isNaN(Number(humidity))) {
cypressWSN.peripherals[peripheral.uuid].humidityData = {
timeNum: count,
date: currDate,
temp: Number(humidity)
}
}
count++;
cypressWSN.time = currDate;
cypressWSN.peripherals[peripheral.uuid].temperature = temperature + ' °C';
cypressWSN.peripherals[peripheral.uuid].humidity = humidity + ' %rH';
cypressWSN.updateUI();
}
}
}
gateway.scan();
gateway.on('scan', onScan);
});
gattip.on('error', function(err) {
console.log(err);
});
}
}
window.cypressWSN = new CypressWSN();
})();