Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeouts reading from sensor #116

Open
brlnr23 opened this issue Oct 26, 2020 · 8 comments
Open

Timeouts reading from sensor #116

brlnr23 opened this issue Oct 26, 2020 · 8 comments

Comments

@brlnr23
Copy link

brlnr23 commented Oct 26, 2020

I have searched a lot in this repository but unfortunately I couldn't find a solution for the problem I am facing.

I constantly get an error reading data from the sensor: Error: failed to read sensor

First off

  1. Using the Adafruit example script returns the values I am expecting - so it seems that its not a wiring issue
  2. I am using a DHT22 sensor (as module) without a pull up resistor
  3. Raspberry Model B V2.0 / npm -v: 6.14.8 / node -v: v9.11.1
  4. I have installed the package with the verbose flag npm install node-dht-sensor --dht_verbose=true

If I run that simple script (taken from the examples) it returns an error. The logfile just says

start sensor read (type=22, pin=14).
*** timeout #2
sensor = require("node-dht-sensor");
var usage =
  "USAGE: node async-explicit.js [sensorType] [gpioPin] <repeats>\n" +
  "    sensorType:\n" +
  "         11: For DHT11 sensor.\n" +
  "         22: For DHT22 or AM2302 sensors.\n\n" +
  "    gpipPin:\n" +
  "         Pin number where the sensor is physically connected to.\n\n" +
  "    repeats:\n" +
  "         How many times the read operation will be performed, default: 10\n";

if (process.argv.length < 4) {
  console.warn(usage);
  process.exit(1);
}

var sensorType = parseInt(process.argv[2], 10);
var gpioPin = parseInt(process.argv[3], 10);
var repeats = parseInt(process.argv[4] || "10", 10);
var count = 0;
var start = 0;
var end = 0;

var iid = setInterval(function() {
  if (++count >= repeats) {
    clearInterval(iid);
  }

  start = new Date().getTime();

  sensor.read(sensorType, gpioPin, function(err, temperature, humidity) {
    end = new Date().getTime();
    if (err) {
      console.warn("" + err);
    } else {
      console.log(
        "temperature: %s°C, humidity: %s%%, time: %dms",
        temperature.toFixed(1),
        humidity.toFixed(1),
        end - start
      );
    }
  });
}, 3000);

Results in:

node index.js 22 14 1
(node:5316) Warning: N-API is an experimental feature and could change at any time.
BCM2835 initialized.
Error: failed to read sensor

If I run the same script but with type 11 it returns completely wrong values, but it returns at least something

node index.js 11 14 1
(node:5326) Warning: N-API is an experimental feature and could change at any time.
BCM2835 initialized.
temperature = 0, humidity = 2
temperature: 0.0°C, humidity: 2.0%, time: 52ms

To me this looks like an issue with the DHT22 type of sensor. Any hints are welcome. I am happy to provide more information if needed

@cainpsycode
Copy link

Hello! I also had the same error and found a solution for myself. I found a difference between the dht-sensor implementation and the protocol, in particular the low trigger value timeout it should be within 1-18ms. I have increased the low voltage latency from 800ms to 1000ms.
changes:
cainpsycode@50811e3

@oscar-c
Copy link

oscar-c commented Jan 11, 2021

Hello! I also had the same error and found a solution for myself. I found a difference between the dht-sensor implementation and the protocol, in particular the low trigger value timeout it should be within 1-18ms. I have increased the low voltage latency from 800ms to 1000ms.
changes:
cainpsycode@50811e3

I too had this problem. The mentioned changes fixed my problem as well.

@brlnr23
Copy link
Author

brlnr23 commented Feb 1, 2021

thanks for this hint. Since I am no c++ guy at all: am I right that I need to complile the cpp files? And if so: do you have any hints how a bloody starter is able to do so ;)

@cainpsycode
Copy link

cainpsycode commented Feb 1, 2021

thanks for this hint. Since I am no c++ guy at all: am I right that I need to complile the cpp files? And if so: do you have any hints how a bloody starter is able to do so ;)

Yes you need rebuild native cpp code for your target platform. Just follow the instruction https://github.com/momenso/node-dht-sensor#reference-for-building-from-source

@brlnr23
Copy link
Author

brlnr23 commented Feb 1, 2021

a big up to you, @cainpsycode! After some back and forth it worked for me!

If someone else wants to build it from source:
I did that on my Raspberry directly. For me, there was to need to run the following command

$ sudo update-alternatives --install /usr/bin/node-gyp node-gyp /opt/node-v10.15.3-linux-armv7l/bin/node-gyp 1

Instead I need to run the following commands as sudo in the node-dht-sensor directory

$ sudo node-gyp configure
$ sudo node-gyp build

I was also missing the package node-addon-api but a simple npm install node-addon-api fixed it.

Once the build process has been completed I could import the self-made build via - in my case with promises -

var sensor = require("./lib").promises;

... do fancy things here

// edit: no I hope that the library does not suffer from the negativ values bug as the Python lib I used to use does ;)

@cainpsycode
Copy link

cainpsycode commented Feb 2, 2021

a big up to you, @cainpsycode! After some back and forth it worked for me!

If someone else wants to build it from source:
I did that on my Raspberry directly. For me, there was to need to run the following command

$ sudo update-alternatives --install /usr/bin/node-gyp node-gyp /opt/node-v10.15.3-linux-armv7l/bin/node-gyp 1

Instead I need to run the following commands as sudo in the node-dht-sensor directory

$ sudo node-gyp configure
$ sudo node-gyp build

I was also missing the package node-addon-api but a simple npm install node-addon-api fixed it.

Once the build process has been completed I could import the self-made build via - in my case with promises -

var sensor = require("./lib").promises;

... do fancy things here

// edit: no I hope that the library does not suffer from the negativ values bug as the Python lib I used to use does ;)

There is no hope. The negative values are wrong.

I fixed it, but I can't be sure if the solution is correct.
@brlnr23 you can try this branch https://github.com/cainpsycode/node-dht-sensor/tree/bugfix/tmp-negative

@KillerJulian
Copy link

I have the same timeout problem when I run the DHT11 sensor on 3.3V. I have changed my setup and use 5V on the sensor. But even there it happens sometimes!

I hope this problem will be solved soon in the LIB

@Ricky1966
Copy link

Hi, sorry for my english, but isn't my first languege.
I've read this discussion, I've try to change latency as @cainpsycode suggest, I've compiled and try, the issue there is still.
Also I, with Python, have the rigth values, so isn't a probe problem.
I've changed voltage to 3v3. Yet same problem.
Someone have fixed this problem?
Ther is a fork of project?
Tks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants