• The DHT11 (and higher-spec'ed DHT22 - I've got one on it's way from china to try out) are widely available temperature+relative humidity sensors that communicate with the controller via an (apparently non-standard) single wire protocol, as (inaccurately) described in the datasheet: http://www.micro4you.com/files/sensor/DHT11.pdf The most appealing feature of these sensors is that they are cheaper than dirt (like, $1.50 a pop, shipped cheap) - and their accuracy is good enough for lots of cases.

    After some experimentation, I was able to get correct date out of the DHT11 with the code provided below. I'm sure this code is pretty far from ideal, and I'd like to clean it up - if anyone has any advice on that, I'd love to hear it. I know the fact that I'm doing setTimeout() with a string to be evaluated is bad, particularly since it makes it awkward to select the pin to use - but I can't figure out how to make it work otherwise. I figure I should also put all of those global variables inside of the dht object. Also - is there a better way than making an array of bits and accessing them like I do?

    I found a few caveats when interfacing with these:

    • The spec sheet says it returns 40 bits - 32 data bits plus an 8 bit checksum. My DHT11's are only returning 32 bits. Also, 16 of those bits are always zero, as the DHT11 has no decimal data to return (resolution is only 1 degree, 1% RH).
    • Sometimes it doesn't seem to recognize the start signal. Not sure why - so I have dht.onread() make sure we actually got data, and try again if we didnt.

    usage: dht.read(function());

    ex: dht.read(function(a){print("Data: "+a.rh.toString()+" %RH "+a.temp.toString()+" C");});

    Get data from the DHT11, and then call the supplied function when the data is available. Function is called with one argument, an object with properties 'temp' and 'rh'.

    var dht={};
    var watch;
    var i=0;
    var bits=[];
    var pstart=0;
    dht.read = function (a) {
    	dht.onreadf=a;
    	i=0;
    	bits=[];
    	digitalPulse(C6,1,0.08);
    	setTimeout("pinMode(C6,'input_pullup');watch=setWatch(function(t) {dht.onwatch(t);},C6,{repeat:true});",0.07);
    	setTimeout("dht.onread(dht.endRead());",50);
    }
    dht.onread= function(d) {
    	if (d.temp==-1) {
    		dht.read(dht.onreadf);
    	} else {
    		dht.onreadf(d);
    	}
    }
    dht.onwatch = function(t) {
    	//console.log(t.time+" "+t.state);
    	if (t.state) {
    		pstart=t.time
    	} else {
    		var tt=t.time-pstart;
    		if (tt > 0.000044) {
    			bits[i]=1;
    		} else {
    			bits[i]=0;
    		}
    		i++;
    	}
    }
    dht.endRead = function() {
    	clearWatch(watch);
    	if (i==34 | i==35 | i==36) {
    		var rh=eval("0b"+bits[2]+bits[3]+bits[4]+bits[5]+bits[6]+bits[7]+bits[8]+bits[9]);
    		var temp=eval("0b"+bits[18]+bits[19]+bits[20]+bits[21]+bits[22]+bits[23]+bits[24]+bits[25]);
    		//console.log("Data Obtained: "+rh.toString()+" %RH "+temp.toString()+" C");
    		return {"temp":temp,"rh":rh};
    	} else {
    		//console.log("Bad data: "+i);
    		return {temp:-1,rh:-1};
    	}
    }
    
About

Avatar for DrAzzy @DrAzzy started