1

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)
    list1.append(h)
    list1.append(t)

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.

5
  • what is the eror message? Commented Apr 24, 2016 at 18:25
  • I didn't actually get an error when I ran the above code, nothing happened.
    – Geord359
    Commented Apr 25, 2016 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? Commented Apr 25, 2016 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
    Commented Apr 25, 2016 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
    Commented Jun 15, 2018 at 17:07

2 Answers 2

1

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.

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

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 and acknowledge you have read our privacy policy.

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