forked from momenso/node-dht-sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-dht-sensor.cpp
298 lines (255 loc) · 7.65 KB
/
node-dht-sensor.cpp
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
#include <node.h>
#include <cstdlib>
#include <ctime>
// Access from ARM running linux
#define BCM2708_PERI_BASE 0x20000000
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <bcm2835.h>
#include <unistd.h>
#include <sched.h>
#define MAXTIMINGS 100
#define DHT11 11
#define DHT22 22
#define AM2302 22
#ifdef VERBOSE
int bits[1000];
int bitidx = 0;
#endif
int initialized = 0;
int data[100];
unsigned long long last_read[32];
float last_temperature[32] = {};
float last_humidity[32] = {};
unsigned long long getTime()
{
struct timeval tv;
gettimeofday(&tv, NULL);
unsigned long long time = (unsigned long long)(tv.tv_sec)*1000 +
(unsigned long long)(tv.tv_usec)/1000;
return time;
}
long readDHT(int type, int pin, float &temperature, float &humidity)
{
int counter = 0;
int laststate = HIGH;
int j=0;
#ifdef VERBOSE
bitidx = 0;
#endif
unsigned long long now = getTime();
if (now - last_read[pin] < 2000) {
#ifdef VERBOSE
printf("Too early to read again pin %d: %llu\n", pin, now - last_read[pin]);
#endif
temperature = last_temperature[pin];
humidity = last_humidity[pin];
return 0;
} else {
last_read[pin] = now + 420;
}
// Set GPIO pin to output
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_write(pin, HIGH);
bcm2835_delay(400);
bcm2835_gpio_write(pin, LOW);
bcm2835_delay(20);
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
// wait for pin to drop?
int timeout = 100000;
while (bcm2835_gpio_lev(pin) == 1) {
if (--timeout < 0) {
#ifdef VERBOSE
printf("Sensor timeout.\n");
#endif
return -3;
}
bcm2835_delayMicroseconds(1); //usleep(1);
}
// read data!
for (int i = 0; i < MAXTIMINGS; i++) {
counter = 0;
while (bcm2835_gpio_lev(pin) == laststate) {
counter++;
if (counter == 1000)
break;
}
laststate = bcm2835_gpio_lev(pin);
if (counter == 1000) break;
#ifdef VERBOSE
if (bitidx < 1000) {
bits[bitidx++] = counter;
} else {
printf("WARNING: bits buffer blew up!\n");
}
#endif
if ((i>3) && (i%2 == 0)) {
// shove each bit into the storage bytes
data[j/8] <<= 1;
if (counter > 200)
data[j/8] |= 1;
j++;
}
}
if ((j >= 39) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xff)))
{
#ifdef VERBOSE
printf("[Sensor type = %d] ", type);
#endif
if (type == DHT11) {
#ifdef VERBOSE
printf("Temp = %d C, Hum = %d %%\n", data[2], data[0]);
#endif
temperature = data[2];
humidity = data[0];
}
else if (type == DHT22)
{
float f, h;
h = data[0] * 256 + data[1];
h /= 10;
f = (data[2] & 0x7F) * 256 + data[3];
f /= 10.0;
if (data[2] & 0x80) f *= -1;
#ifdef VERBOSE
printf("Temp = %.1f C, Hum = %.1f %%\n", f, h);
#endif
temperature = f;
humidity = h;
}
else
{
return -2;
}
}
else
{
#ifdef VERBOSE
printf("Unexpected data: j=%d: %d != %d + %d + %d + %d\n",
j, data[4], data[0], data[1], data[2], data[3]);
#endif
return -1;
}
#ifdef VERBOSE
printf("Obtained readout successfully.\n");
#endif
// update last readout
last_temperature[pin] = temperature;
last_humidity[pin] = humidity;
return 0;
}
int initialize()
{
// set up real-time scheduling
struct sched_param schedp;
schedp.sched_priority = 1;
sched_setscheduler(0, SCHED_FIFO, &schedp);
if (!bcm2835_init())
{
#ifdef VERBOSE
printf("BCM2835 initialization failed.\n");
#endif
return 1;
}
else
{
#ifdef VERBOSE
printf("BCM2835 initialized.\n");
#endif
initialized = 1;
memset(last_read, 0, sizeof(unsigned long long)*32);
return 0;
}
}
using namespace v8;
int GPIOPort = 4;
int SensorType = 11;
Handle<Value> Read(const Arguments& args) {
HandleScope scope;
float temperature = 0, humidity = 0;
int retry = 3;
int result = 0;
do {
result = readDHT(SensorType, GPIOPort, temperature, humidity);
if (--retry < 0) break;
} while (result != 0);
Local<Object> readout = Object::New();
readout->Set(String::NewSymbol("humidity"), Number::New(humidity));
readout->Set(String::NewSymbol("temperature"), Number::New(temperature));
readout->Set(String::NewSymbol("isValid"), Boolean::New(result == 0));
readout->Set(String::NewSymbol("errors"), Number::New(2 - retry));
return scope.Close(readout);
}
Handle<Value> ReadSpec(const Arguments& args) {
HandleScope scope;
if (args.Length() < 2) {
ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
return scope.Close(Undefined());
}
int sensorType = args[0]->Uint32Value();
if (sensorType != 11 && sensorType != 22) {
ThrowException(Exception::TypeError(String::New("Specified sensor type is invalid.")));
return scope.Close(Undefined());
}
if (!initialized) {
initialized = initialize() == 0;
if (!initialized) {
ThrowException(Exception::TypeError(String::New("Failed to initialize.")));
return scope.Close(Undefined());
}
}
int gpio = args[1]->Uint32Value();
float temperature = 0, humidity = 0;
int retry = 3;
int result = 0;
do {
result = readDHT(sensorType, gpio, temperature, humidity);
if (--retry < 0) break;
} while (result != 0);
Local<Object> readout = Object::New();
readout->Set(String::NewSymbol("humidity"), Number::New(humidity));
readout->Set(String::NewSymbol("temperature"), Number::New(temperature));
readout->Set(String::NewSymbol("isValid"), Boolean::New(result == 0));
readout->Set(String::NewSymbol("errors"), Number::New(2 - retry));
return scope.Close(readout);
}
Handle<Value> Initialize(const Arguments& args) {
HandleScope scope;
if (args.Length() < 2) {
ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
return scope.Close(Undefined());
}
if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
ThrowException(Exception::TypeError(String::New("Wrong arguments")));
return scope.Close(Undefined());
}
int sensorType = args[0]->Uint32Value();
if (sensorType != 11 && sensorType != 22) {
ThrowException(Exception::TypeError(String::New("Specified sensor type is not supported")));
return scope.Close(Undefined());
}
// update parameters
SensorType = sensorType;
GPIOPort = args[1]->Uint32Value();
return scope.Close(v8::Boolean::New(initialize() == 0));
}
void RegisterModule(v8::Handle<v8::Object> target) {
target->Set(String::NewSymbol("read"),
FunctionTemplate::New(Read)->GetFunction());
target->Set(String::NewSymbol("readSpec"),
FunctionTemplate::New(ReadSpec)->GetFunction());
target->Set(String::NewSymbol("initialize"),
FunctionTemplate::New(Initialize)->GetFunction());
}
NODE_MODULE(node_dht_sensor, RegisterModule);