smithjames0
Posts: 42
Joined: Tue Oct 04, 2016 10:04 pm

Problems with dht22

Sun Dec 04, 2016 9:11 am

Hi,

I was using two dht11's sensor but I found them two inaccurate so I swapped them to dht22. The temperature reading it better but the humidity is incorrect, at first I thought that one of the sensors was broken as it only gave a humidity reading of 1%. I replaced that sensor but the new one says the same and the other one says 100% all the time. I am using pull up resistors.

The sensors
http://www.ebay.co.uk/itm/D4-AM2302-Dig ... Swkl5XehOA

The code

Code: Select all

def tempForPin(self, pin, location):
        current = datetime.datetime.now(pytz.timezone(self.city.timezone))
        retries = 3
        humidity, temperature = Adafruit_DHT.read_retry(22, pin)
        while (humidity is None or temperature is None) and retries > 0:
            time.sleep(1)
            humidity, temperature = Adafruit_DHT.read_retry(22, pin)
            retries -= 1

        if humidity is not None and temperature is not None:
            temp_f = temperature
            #if cache[pin] and abs(cache[pin] - temp_f) > 20:
            #    retries -= 1
            #    continue
          #   logger.info('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temp_f, humidity))
            logger.info('Location = {0} Temp={1:0.1f}*C  Humidity={2:0.1f}%'.format(location, temp_f, humidity))
            self.loggerdb('Location = {0} Temp={1:0.1f}*C  Humidity={2:0.1f}%'.format(location, temp_f, humidity))
            query = "INSERT INTO Chicken(Location,Time,Temp,Hum) " \
            "VALUES(%s,%s,%s,%s)"
            args = (location,current,temp_f,humidity)
            cur.execute(query, args)
            db.commit()

        logger.error('Failed to get reading temp. Try again!')
        return (0, 0)

....................


    def check_frost(self):
        global ifrost
        humidity, temperature = Adafruit_DHT.read_retry(22, Coop.PIN_TEMP1)
        if temperature < 3:
            GPIO.output(Coop.PIN_LED_B, GPIO.HIGH)
            if (Coop.ifrost == 0):
                self.sendEmail('Outside temp is less than 3','Current temperature is' + str(temperature))
                Coop.ifrost += 1

        else:
            GPIO.output(Coop.PIN_LED_B, GPIO.LOW)
            if (Coop.ifrost == 1):
                self.sendEmail('Outside temp is above 3','Current temperature is' + str(temperature))
                Coop.ifrost -= 1

Has anyone else had problems like this?

James

User avatar
joan
Posts: 14089
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problems with dht22

Sun Dec 04, 2016 9:15 am

Try different software. There is plenty to choose from.

E.g. my http://abyz.co.uk/rpi/pigpio/examples.html#pdif2_DHTXXD (need pigpiod to be running, sudo pigpiod).

smithjames0
Posts: 42
Joined: Tue Oct 04, 2016 10:04 pm

Re: Problems with dht22

Sun Dec 04, 2016 5:37 pm

I have tried today putting a sleep() in-between both sensors, but it didn't change anything.

James

User avatar
B.Goode
Posts: 8050
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Problems with dht22

Sun Dec 04, 2016 6:13 pm

Even if you have no intention of migrating from the Adafruit library for your final solution, using an alternative such as that proposed by @joan on a trial basis might help you discriminate between a hardware problem with the sensors or the way they are connected on the one hand, and the software that is being used to read the data from them on the other.

recklessfb
Posts: 37
Joined: Wed Jul 06, 2016 12:26 am

Re: Problems with dht22

Sun Dec 04, 2016 9:21 pm

I ordered the same sensor but haven't received it yet.
Have you tried a set up without using the pull up resistors?
I was under the impression that the sensor you linked(ebay) already has the pull up resistor built in.
The dht22 has 4 pins and the one you linked is a dht22 that has been soldered on a circuit board. I think the small black and brown squares are the resisitors.
This is the DHT22 without the resistors.
https://www.adafruit.com/product/385

smithjames0
Posts: 42
Joined: Tue Oct 04, 2016 10:04 pm

Re: Problems with dht22

Sun Dec 18, 2016 8:58 pm

Thanks, I have tried that today but it didn't change anything.

James

Return to “Automation, sensing and robotics”