Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
'target_name': 'node-dht-sensor',
'sources': [ 'node-dht-sensor.cpp' ],
'libraries': [ '-lbcm2835' ],
'libraries': [ '-lwiringPi' ],
'conditions': [
['OS=="linux"', {
'include_dirs+': '/usr/local/lib/libbcm2835.a',
Expand Down
48 changes: 19 additions & 29 deletions node-dht-sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <bcm2835.h>
#include <wiringPi.h>
#include <unistd.h>
#include <sched.h>

Expand Down Expand Up @@ -69,38 +69,39 @@ long readDHT(int type, int pin, float &temperature, float &humidity)
}

// 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);
pinMode (pin, OUTPUT);

digitalWrite (pin, HIGH);
delay(500);

digitalWrite (pin, LOW);
delay(20);

pinMode (pin, INPUT) ;

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) {
while (digitalRead (pin) == 1) {
if (--timeout < 0) {
#ifdef VERBOSE
printf("Sensor timeout.\n");
#endif
return -3;
}
bcm2835_delayMicroseconds(1); //usleep(1);
usleep(1);
}

// read data!
for (int i = 0; i < MAXTIMINGS; i++) {
counter = 0;
while (bcm2835_gpio_lev(pin) == laststate) {
while (digitalRead (pin) == laststate) {
counter++;
if (counter == 1000)
break;
}
laststate = bcm2835_gpio_lev(pin);
laststate = digitalRead (pin);
if (counter == 1000) break;
#ifdef VERBOSE
if (bitidx < 1000) {
Expand Down Expand Up @@ -178,23 +179,12 @@ int initialize()
struct sched_param schedp;
schedp.sched_priority = 1;
sched_setscheduler(0, SCHED_FIFO, &schedp);

wiringPiSetup ();

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;
}
initialized = 1;
memset(last_read, 0, sizeof(unsigned long long)*32);
return 0;
}

using namespace v8;
Expand Down