DHT11 and DHT22 Driver Library based on the pigpio GPIO library.
pip install pigpio-dht
Tested against Python >= 3.5.
May work with earlier 3.X versions but this has not been confirmed. Does not work with Python 2.7.
DHT sensors are slow to take readings, and need to settle between reads. For instance, the maximum read rates for the sensors are:
- DHT11 once every 1 seconds
- DHT22 once every 2 seconds
This library monitors the read rate and will pause between successive calls to read()
to honor these limits.
Take a single reading from the sensor.
# NOTE: The import has a _ not - in the module name. from pigpio_dit import DHT11, DHT22 gpio = 21 # BCM Numbering sensor = DHT11(gpio) #sensor = DHT22(gpio) result = sensor.read() print(result)
{'temp_c': 20, 'temp_f': 68.0, 'humidity': 35, 'valid': True}
Take many readings (by repeating calling read()
) from the sensor and return a normalised result.
# NOTE: The import has a _ not - in the module name. from pigpio_dit import DHT11, DHT22 gpio = 21 # BCM Numbering sensor = DHT11(gpio) #sensor = DHT22(gpio) result = sensor.sample(samples=5) print(result)
{'temp_c': 20, 'temp_f': 68.0, 'humidity': 35, 'valid': True}
The classes DHT11
and DHT22
both extend the base class DHTXX
and share a common API.
- gpio GPIO (BCM) pin that data leg of sensor is connected to
- timeout_secs Sensor timeout in second. Default should be adequate unless you receive a TimeoutError advising you to increase the value wuth calling
read()
orsample()
- use_internal_pullup - Enable internal pull-up resistor on gpio
- pi a custom instance of
pigpio.pi()
Take a single reading from the sensor.
- retries number of times to keep retrying when the result contains
valid = False
A Dictionary in the form {'temp_c': 20, 'temp_f': 68.0, 'humidity': 35, 'valid': True}
Where:
- temp_c is the temperature in degrees Celsius
- temp_f is the temperature in degrees Fahrenheit
- humidity is the relative humidity
- valid is true only if sensors checksum matches with returned data.
Discard readings where value == False
and try again.
- If the sensor on
gpio
does not respond - If the sensor responds within
timeout_secs
(see _Constructor), but the response cannot be understood by the library. Try increasingtimeout_secs
Take many readings (by repeating calling read()
) from the sensor and return a normalised result.
Please note that a call to sample()
takes time. For example for the DHT11 with a maximum read rate of once every 1 seconds, 5 samples will take approximately 1 second * 5 samples = 5 seconds.
Parameters:
- samples number of samples to take
- max_retries maximum number of times to keep retrying per sample when the result contains
valid = False
. Default to samples * 2
- Same as for
read()
, plus - If
max_retries
is reached