-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDht.js
174 lines (149 loc) Β· 4.22 KB
/
Dht.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
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
+(function (factory) {
if (typeof exports === 'undefined') {
factory(webduino || {});
} else {
module.exports = factory;
}
}(function (scope) {
'use strict';
var Module = scope.Module,
BoardEvent = scope.BoardEvent,
proto;
var DHT_MESSAGE = [0x04, 0x04],
MIN_READ_INTERVAL = 1000,
MIN_RESPONSE_TIME = 30,
RETRY_INTERVAL = 6000;
var DhtEvent = {
READ: 'read',
READ_ERROR: 'readError'
};
function Dht(board, pin) {
Module.call(this);
this._type = 'DHT11';
this._board = board;
this._pin = pin;
this._humidity = null;
this._temperature = null;
this._lastRecv = null;
this._readTimer = null;
this._readCallback = function () {};
this._board.on(BoardEvent.BEFOREDISCONNECT, this.stopRead.bind(this));
this._messageHandler = onMessage.bind(this);
this._board.on(BoardEvent.ERROR, this.stopRead.bind(this));
}
function onMessage(event) {
var message = event.message;
if (message[0] !== DHT_MESSAGE[0] || message[1] !== DHT_MESSAGE[1]) {
return;
} else {
processDhtData(this, message);
}
}
function processDhtData(self, data) {
var str = '',
i = 3,
MAX = 4,
dd = [],
d1, d2;
if (data[2] === self._pin.number) {
while (i < data.length) {
d1 = data[i];
d2 = data[i + 1];
str += (d1 - 48);
d2 && (str += (d2 - 48));
i += 2;
if ((i - 3) % MAX === 0) {
dd.push(parseInt(str) / 100);
str = '';
}
}
self._lastRecv = Date.now();
self.emit(DhtEvent.READ, dd[0], dd[1]);
}
}
Dht.prototype = proto = Object.create(Module.prototype, {
constructor: {
value: Dht
},
humidity: {
get: function () {
return this._humidity;
}
},
temperature: {
get: function () {
return this._temperature;
}
}
});
proto.trigger = function (state, delaySec, repeatTime) {
var self = this;
var recvPin = this._pin._number;
var delayHigh6bit = (delaySec & 0x0FC0) >> 6;
var delayLow6bit = (delaySec & 0x3F);
var repeatHigh6bit = (repeatTime & 0x0FC0) >> 6;
var repeatLow6bit = (repeatTime & 0x3F);
if (state) {
self._board.sendSysex(DHT_MESSAGE[0], [DHT_MESSAGE[1],
0x40, delayHigh6bit + 1, delayLow6bit + 1,
repeatHigh6bit + 1, repeatLow6bit + 1,
DHT_MESSAGE[1], recvPin, 0x00
]);
} else {
self._board.sendSysex(DHT_MESSAGE[0], [DHT_MESSAGE[1], 0x41, 0]);
}
}
proto.read = function (callback, interval) {
var self = this,
timer;
self.stopRead();
if (typeof callback === 'function') {
self._readCallback = function (humidity, temperature) {
self._humidity = humidity;
self._temperature = temperature;
callback({
humidity: humidity,
temperature: temperature
});
};
self._board.on(BoardEvent.SYSEX_MESSAGE, self._messageHandler);
self.on(DhtEvent.READ, self._readCallback);
timer = function () {
self._board.sendSysex(DHT_MESSAGE[0], [DHT_MESSAGE[1], self._pin.number]);
if (interval) {
interval = Math.max(interval, MIN_READ_INTERVAL);
if (self._lastRecv === null || Date.now() - self._lastRecv < 5 * interval) {
self._readTimer = setTimeout(timer, interval);
} else {
self.stopRead();
setTimeout(function () {
self.read(callback, interval);
}, RETRY_INTERVAL);
}
}
};
timer();
} else {
return new Promise(function (resolve, reject) {
self.read(function (data) {
self._humidity = data.humidity;
self._temperature = data.temperature;
setTimeout(function () {
resolve(data);
}, MIN_RESPONSE_TIME);
});
});
}
};
proto.stopRead = function () {
this.removeListener(DhtEvent.READ, this._readCallback);
this._board.removeListener(BoardEvent.SYSEX_MESSAGE, this._messageHandler);
this._lastRecv = null;
if (this._readTimer) {
clearTimeout(this._readTimer);
delete this._readTimer;
}
};
scope.module.DhtEvent = DhtEvent;
scope.module.Dht = Dht;
}));