I've been having problems integrating the code used to extract temp/humidity measurements from the DHT22 into a larger python script. The following code works without issue -

import Adafruit_DHT as dht
h,t = dht.read_retry(dht.DHT22, 4)
print h
print t

When the above script is run the shell prints the temperature and humidity readings as expected. However, if I try to incorporate line 2 (dht.read_retry) into an if statement or a while loop I either encounter an error or the shell becomes unresponsive. For example, if I run the following script nothing happens:

import Adafruit_DHT as dht

var = 1
list1 = []

if var == 1:
    h,t = dht.read_retry(dht.DHT22, 4)

print list1

I would expect the shell to print the list but nothing happens. And in other, more elaborate, scripts I get errors relating to the line containing dht.read_retry.

Does anyone know why this occurs? I have some experience with python but this is my first Raspberry Pi project. Could it be because I'm using the python shell or am I using the Adafruit_DHT module incorrectly? Any help is appreciated.

  • what is the eror message? – Steve Robillard Apr 24 '16 at 18:25
  • I didn't actually get an error when I ran the above code, nothing happened. – Geord359 Apr 25 '16 at 6:43
  • But you menton running more elaborate scripts with an error? Also what happens if you create a .py file with the above code and run that? – Steve Robillard Apr 25 '16 at 6:45
  • Ah right, I'm away for a few days so can't get to my Pi. The error involved 'module', it wasn't 'module not found' but something to do with reading dht.read_retry. Sorry, I can't be more specific. I just don't get why you can extract the temp/humidity values in the first script but not in a loop. – Geord359 Apr 25 '16 at 9:52
  • Maybe I'm wrong because I'm not a python guru, but it looks like the read loop will loop forever because val is always 1. Therefore you won't get to the print statement. – NomadMaker Jun 15 '18 at 17:07

As far as I am aware, those humidity sensors cannot be queried too often. From the top of my ahead, and from the datasheets, both the DHT11 and DHT22 cannot see queries less than 1-2 seconds apart. If you do not respect that interval, they wont answer.

  • I was running the loop every 10 seconds so I don't think it was that. – Geord359 Apr 26 '16 at 17:12

First I enabled the SPI, I2C and 1-wire on raspberry py using

sudo raspi-config

command and selecting Interfacing Options.

Then enabled the resistor using the following two commands.

sudo pigpiod

pigs pud 4 u

Now it's working as I expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.