-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht11.js
205 lines (184 loc) · 5.01 KB
/
dht11.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
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
// Copyright 2017 by the eJS project. All rights reserved.
// The use of this source code is governed by the MIT license.
//
// This program decodes a signal from DHT11 temperture and humidity
// sensor module. Decoding algorithm is shown in:
// https://osoyoo.com/2017/07/06/dht11/
SENSOR_DATA = [[451, 811663104, 1623294968, 813631472, -2096755688,
405823681, -262078964, 1623294727, 101455920, -262094461,
-131039481, -470270077, -1, -1, -1, 7],
[451, 811663104, 406815736, -65027588, 405823681, -1048377844,
-2080886688, 101703873, 811647363, -2096755688, -32761793,
-470273952, -1, -1, -1, 7],
[456, 811663104, 532775928, 266387964, 1623294726, 101455920,
266387843, 405831430, -1048377844, 202911840, 101703934,
2131226751, -4, -1, -1, 255],
[436, 811663104, -524157928, -1048377841, 202911840,
1623294726, 1065613360, 1623325720, 101455920, -262094461,
1623294727, -14352, -1, -1, 1048575],
[436, -262078720, 1623294727, -1048315856, 202911840,
1623294726, -1048377360, -2096754720, 405823681, 813630988,
-524188680, -14833, -1, -1, 1048575],
[446, -262078720, 406815495, -1048377844, 202911840,
1623294726, 1065551856, 1623325720, 101455920, 266387843,
-131047170, -14696061, -1, -1, 1073741823],
[436, 811663104, 532775928, -1048377844, 202911840, 1623294726,
-260110352, 1623294727, 101455920, 266387843, 532775686,
-14836, -1, -1, 1048575],
[441, 811663104, 406815736, 811662860, -2096755688,
405823681, 266403340, 1623294726, 101455920, 266387843,
406815494, -474628, -1, -1, 33554431]]
ANSWER =
[[35, 20],
[55, 18],
[45, 10],
[20, 13],
[65, 17],
[80, 21],
[40, 28],
[50, 24]]
function validate(i, humidity, temperture) {
if (humidity != ANSWER[i][0])
throw "error";
if (temperture != ANSWER[i][1])
throw "error";
}
function decode_sensor_data(sensor_data) {
var data, i, x, len = sensor_data[0];
data = new Array(len);
for (i = 0; i < len; i++) {
if (i % 32 == 0)
x = sensor_data[i / 32 + 1];
data[i] = x & 1;
x >>= 1;
}
return data;
}
function get_lengths_from_signal(data) {
var STATE_INIT_PULL_DOWN = 1;
var STATE_INIT_PULL_UP = 2;
var STATE_DATA_FIRST_PULL_DOWN = 3;
var STATE_DATA_PULL_UP = 4;
var STATE_DATA_PULL_DOWN = 5;
var state = STATE_INIT_PULL_DOWN;
var lengths = [];
var current_length = 0;
for (var i = 0; i < data.length; i++) {
var current = data[i];
current_length += 1;
if (state == STATE_INIT_PULL_DOWN) {
if (current == 0) {
state = STATE_INIT_PULL_UP;
} else {
continue;
}
}
if (state == STATE_INIT_PULL_UP) {
if (current == 1) {
state = STATE_DATA_FIRST_PULL_DOWN;
} else {
continue;
}
}
if (state == STATE_DATA_FIRST_PULL_DOWN) {
if (current == 0) {
state = STATE_DATA_PULL_UP;
} else {
continue;
}
}
if (state == STATE_DATA_PULL_UP) {
if (current == 1) {
current_length = 0;
state = STATE_DATA_PULL_DOWN;
} else {
continue;
}
}
if (state == STATE_DATA_PULL_DOWN) {
if (current == 0) {
lengths.push(current_length);
state = STATE_DATA_PULL_UP;
} else {
continue;
}
}
}
return lengths;
}
function max_len(ary) {
if (ary.length > 0) {
var x = ary[0];
for (var i = 0; i < ary.length; i++) {
if (x < ary[i]) x = ary[i];
}
return x;
} else {
return undefined;
}
}
function min_len(ary) {
if (ary.length > 0) {
var x = ary[0];
for (var i = 0; i < ary.length; i++) {
if (x > ary[i]) x = ary[i];
}
return x;
} else {
return undefined;
}
}
function make_bits_from_lengths(lengths) {
var shortest_pull_up = min_len(lengths);
var longest_pull_up = max_len(lengths);
var halfway = (longest_pull_up + shortest_pull_up) / 2;
var bits = [];
for (var i = 0; i < lengths.length; i++) {
var length = lengths[i];
var bit = 0;
if (length > halfway) {
bit = 1;
}
bits.push(bit);
}
return bits;
}
function bytes_from_bits(bits) {
var the_bytes = [];
var bt = 0;
for (var i = 0; i < bits.length; i++) {
bt = bt << 1;
if (bits[i]) {
bt = bt | 1;
} else {
bt = bt | 0;
}
if ((i+1)%8==0) {
the_bytes.push(bt);
bt = 0;
}
}
return the_bytes;
}
function parse_signal(data) {
var lengths = get_lengths_from_signal(data);
if (lengths.length != 40) {
throw ("Data not good, skip (length : " + lengths.length + ")");
return undefined;
}
var bits = make_bits_from_lengths(lengths);
return bytes_from_bits(bits);
}
function main() {
var i, iteration;
var data = new Array(8);
for (i = 0; i < 8; i++)
data[i] = decode_sensor_data(SENSOR_DATA[i]);
print("start benchmark");
for (iteration = 0; iteration < 1000; iteration++)
for (i = 0; i < 8; i++) {
the_bytes = parse_signal(data[i]);
validate(i, the_bytes[0], the_bytes[2]);
}
}
main();