You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • Hi,

    It's come up in another thread that the existing DHT11 & 22 modules aren't that fast or memory efficient. There's been a pull request for a multipurpose module, but it's still slower than it could be, and doesn't work in a backwards-compatible way with DHT11.

    I don't have a DHT22 here so can't test, but maybe someone else could take a look at this and see if it works on all the various different module types, or whether we need a few more tweaks?

    // Sensor object constructor
    function HT(device, pin) {
      this.device = device;
      this.pin = pin;
    }
    
    // sensor query method...
    /** read sensor as either...
      read(callback);
      read(callback,number_of_tries); - default=3
      */
    HT.prototype.read = function (cb,n) {
      if (!n) n=3;
      var d = ""; 
      var ht = this;
      pinMode(ht.pin); // set pin state to automatic
      digitalWrite(ht.pin,0);
      this.watch = setWatch(function(t) {
        d+=0|(t.time-t.lastTime>0.00005);
      }, ht.pin, {edge:'falling',repeat:true} );
      setTimeout(function() {pinMode(ht.pin,'input_pullup');},1);
      setTimeout(function() {
        clearWatch(ht.watch);
        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);
        if (cks&&((cks&0xFF)==parseInt(d.substr(34,­8),2))) {
          var o = { raw:d };
          if (ht.device=="DHT11") {
            o.rh = parseInt(d.substr(2,8),2);
            o.t = parseInt(d.substr(18,8),2);
          } else {
            o.rh = parseInt(d.substr(2,16),2)*0.1;
            o.t = parseInt(d.substr(19,15),2)*0.2*(d[16]-0­.5);
          }
          cb(o);
        } else {
          if (n>1) setTimeout(function() {ht.read(cb,--n);},500);
          else cb({err:true, raw:d});
        }
      },6);
    };
    
    // Test with
    var dht = new HT("DHT11",A8);
    dht.read(print);
    

    I can then put it into a HTxx module, and can reference that from existing DHT11/22 modules to provide a seamless upgrade for everyone already using them.

About

Avatar for Gordon @Gordon started