You are reading a single comment by @ClearMemory041063 and its replies. Click here to read the full conversation.
  • Let me restate the problem.
    I pulled the DHT22.js module and inserted a console.log for illustration.
    Here is where the console.log was inserted

        delete ht.watch;
        var cks = 
    
            parseInt(d.substr(2,8),2)+
            parseInt(d.substr(10,8),2)+
            parseInt(d.substr(18,8),2)+
            parseInt(d.substr(26,8),2);
        console.log('n=',n,'len=',d.length,' cks=',cks&0xFF,' chsum=',parseInt(d.substr(34,8),2));
        if (cks&&((cks&0xFF)==parseInt(d.substr(34,­8),2))) {
    
    

    The example code

    var dht=new DHT22(dpin);
    
    setInterval(function () {
      dht.read(function (a) {
     F=a.temp;
     H=a.rh;
      console.log("Temp is "+F.toString()+" and RH is "+H.toString());
    });
    }, 2000);
    

    Produces the following output:

    n= 3 len= 42  cks= 201  chsum= 201
    Temp is 23.2 and RH is 48
    n= 10 len= 0  cks= 0  chsum= NaN
    n= 6 len= 0  cks= 0  chsum= NaN
    n= 9 len= 42  cks= 201  chsum= 201
    Temp is 23.2 and RH is 48
    n= 5 len= 0  cks= 0  chsum= NaN
    n= 4 len= 42  cks= 202  chsum= 202
    Temp is 23.2 and RH is 48.1
    n= 10 len= 42  cks= 202  chsum= 202
    Temp is 23.2 and RH is 48.1
    n= 10 len= 42  cks= 202  chsum= 202
    Temp is 23.2 and RH is 48.1
    n= 10 len= 42  cks= 201  chsum= 201
    Temp is 23.2 and RH is 48
    n= 10 len= 42  cks= 201  chsum= 201
    Temp is 23.2 and RH is 48
    n= 10 len= 40  cks= 200  chsum= 50
    n= 9 len= 41  cks= 200  chsum= 100
    n= 8 len= 41  cks= 200  chsum= 100
    n= 7 len= 42  cks= 201  chsum= 201
    Temp is 23.2 and RH is 48
    n= 10 len= 0  cks= 0  chsum= NaN
    n= 9 len= 42  cks= 201  chsum= 201
    Temp is 23.2 and RH is 48
    n= 10 len= 42  cks= 202  chsum= 202
    Temp is 23.2 and RH is 48.1
    n= 10 len= 42  cks= 201  chsum= 201
    Temp is 23.2 and RH is 48
    

    Notice that the module code produces a lot of retries n. 1st try n=10, 2nd try n=9...

    By changing the example code to

    var dht=new DHT22(dpin);
    setInterval(function () {
      dht.read(function (a) {});
      dht.read(function (a) {
     F=a.temp;
     H=a.rh;
      console.log("Temp is "+F.toString()+" and RH is "+H.toString());
    });
    }, 2000);
    

    Which gives to following output

    >echo(0);
    =undefined
    n= 10 len= 43  cks= 201  chsum= 201
    n= 10 len= 43  cks= 201  chsum= 201
    Temp is 23.2 and RH is 48
    n= 10 len= 43  cks= 199  chsum= 199
    n= 10 len= 43  cks= 199  chsum= 199
    Temp is 23.2 and RH is 47.8
    n= 10 len= 43  cks= 198  chsum= 198
    n= 10 len= 43  cks= 198  chsum= 198
    Temp is 23.2 and RH is 47.7
    n= 10 len= 43  cks= 198  chsum= 198
    n= 10 len= 43  cks= 198  chsum= 198
    Temp is 23.2 and RH is 47.7
    n= 10 len= 43  cks= 198  chsum= 198
    n= 10 len= 43  cks= 198  chsum= 198
    Temp is 23.2 and RH is 47.7
    n= 10 len= 43  cks= 199  chsum= 199
    n= 10 len= 43  cks= 199  chsum= 199
    Temp is 23.2 and RH is 47.8
    n= 10 len= 43  cks= 199  chsum= 199
    n= 10 len= 43  cks= 199  chsum= 199
    Temp is 23.2 and RH is 47.8
    n= 10 len= 43  cks= 199  chsum= 199
    n= 10 len= 43  cks= 199  chsum= 199
    Temp is 23.2 and RH is 47.8
    

    This gives correct temperature and humidity readings without all the retries in the module code. From a programming perspective it is troubling as Gordon outlined above.
    Even with the setInterval =1000.

    And finally changing the example code one more time so that the one dht.read completes before the 2nd one starts.

    //var dht = require("DHT22").connect(dpin);
    var dht=new DHT22(dpin);
    setInterval(function () {
      dht.read(function (a) {//});
      dht.read(function (a) {
     F=a.temp;
    // F=(a.temp+40)*9/5-40;
     H=a.rh;
      console.log("Temp is "+F.toString()+" and RH is "+H.toString());
    });
        });
    }, 2000);
    
    

    This gives;

    =undefined
    n= 10 len= 40  cks= 199  chsum= 49
    n= 9 len= 42  cks= 197  chsum= 197
    n= 10 len= 0  cks= 0  chsum= NaN
    n= 9 len= 41  cks= 198  chsum= 99
    n= 8 len= 42  cks= 197  chsum= 197
    Temp is 23.2 and RH is 47.6
    n= 10 len= 0  cks= 0  chsum= NaN
    n= 9 len= 41  cks= 198  chsum= 99
    n= 8 len= 41  cks= 198  chsum= 99
    n= 7 len= 41  cks= 198  chsum= 99
    n= 10 len= 0  cks= 0  chsum= NaN
    n= 6 len= 41  cks= 198  chsum= 99
    n= 9 len= 0  cks= 0  chsum= NaN
    n= 5 len= 41  cks= 198  chsum= 99
    n= 8 len= 0  cks= 0  chsum= NaN
    n= 4 len= 42  cks= 197  chsum= 197
    n= 10 len= 0  cks= 0  chsum= NaN
    n= 7 len= 0  cks= 0  chsum= NaN
    n= 9 len= 42  cks= 197  chsum= 197
    Temp is 23.2 and RH is 47.6
    n= 10 len= 0  cks= 0  chsum= NaN
    n= 6 len= 0  cks= 0  chsum= NaN
    n= 9 len= 42  cks= 197  chsum= 197
    n= 10 len= 0  cks= 0  chsum= NaN
    n= 5 len= 0  cks= 0  chsum= NaN
    n= 4 len= 1  cks= 0  chsum= NaN
    n= 9 len= 43  cks= 197  chsum= 197
    Temp is 23.2 and RH is 47.6
    

    Changing the set interval from 1000 to 2000 helps.
    On a scope the entire message takes just under 6ms.

About