First of all, as this question involves a raspberry, python, Rpi. Gpio module and hardware build, I thought this was the place for it.
In the past I've done some work around Dht11 sensor and python seamlessly. I used the little library below, consisting in two classes that handle a dht11 and it's results (or errors). It's not mine and I found it somewhere, kind of undocumented but stable, readable and working out of the box:
But recently I've came across a bunch of dht11 with different pinout and a built in led. The library above doesn't work quite well. It returns an error of missing data often, stored in the DHT11Result.error_code returned from DHT11.read()
I wanted to fiddle with them in arrays, so I made this workaround to avoid missing reads to sensors. The key is in __read() method, where a while loops over reads while the result error is different from 0 (success) the rest is just convenience:
class DHTarray: def __init__(self, pinArray): self.sensor_array =  self.tR =  #temperature results self.hR =  #humidity results self.avT= 0 #average temperature reading self.avH= 0 #average humidity reading for val in pinArray: self.sensor_array.append(DHT11(val)) ##add a DHT instance per pin def __read(self): self.tR =  #erase previous readings self.hR =  #erase previous readings for sen in self.sensor_array: r = sen.read() while r.error_code != 0: #workaround loop for frequen missing data errors r = sen.read() self.tR.append(r.temperature) self.hR.append(r.humidity) def __average_read(self): self.__read() #get updated reads self.avT = 0 self.avH = 0 for i in self.tR: self.avT += i self.avT = self.avT/len(self.tR) for i in self.hR: self.avH += i self.avH = self.avH/len(self.hR) def update(self): self.__average_read()
Then i can come up with code like:
import dhtLib import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) myarray = dhtLib.DHTarray([16,18]) myarray.update() print myarray.avT,myarray.avH #average temp and hum print myarray.tR, myarray.hR #individual readings GPIO.cleanup()
While this works (and need improvement like a maxRetry setting or something to prevent endless loops), I'd like to know where the problem is, if it's software stuff, because this solution really slows down performance (sometimes it takes 8seconds to make a valid reading, or even more)
But what if the problem is the hardware? I've checked solder traces, no shorts, data lines are not being messed by anything. Every sensor in the array (2 in this example) shares vcc and gnd lines. Using one sensor with jumpers and same pin configuration gives the same problems.
Could be power supply problems? I've never had problems with using 5v line from raspberry, even with things that usually are a no-no like motors. But what if pi isn't capable of feed properly this version of dht, because of the built-in led and who knows what else?