DHT11 and DHT22 Driver Library based on the pigpio_ GPIO library.
.. _pigpio: http://www.python.org/
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.
GitHub Repository
_.. _GitHub Repository : https://github.com/garyns/pigpio-dht
DHT sensors are slow to take readings, and need to settle between reads. For instance, the maximum read rates for the sensors are:
This library monitors the read rate and will pause between successive calls to read()
to honor these limits.
One-Shot Read
Take a single reading from the sensor.
Code ^^^^ ::
from pigpio_dht import DHT11, DHT22
gpio = 21 # BCM Numbering
sensor = DHT11(gpio)
result = sensor.read() print(result)
Output ^^^^^^ ::
{'temp_c': 20, 'temp_f': 68.0, 'humidity': 35, 'valid': True}
Also see ^^^^^^^^
read()
__
_ read(retries=0) raises TimeoutError
Sampled Read
Take many readings (by repeating calling read()
) from the sensor and return a normalised result.
Code ^^^^
::
from pigpio_dht import DHT11, DHT22
gpio = 21 # BCM Numbering
sensor = DHT11(gpio)
result = sensor.sample(samples=5) print(result)
Output ^^^^^^
::
{'temp_c': 20, 'temp_f': 68.0, 'humidity': 35, 'valid': True}
Also see ^^^^^^^^
sample()
__
__ sample(samples=5, max_retries=None) raises TimeoutError
_
The classes DHT11
and DHT22
both extend the base class DHTXX
and share a common API.
Constructor: DHT11 | DHT22(gpio, timeout_secs=0.5, use_internal_pullup=True, pi=None)
Parameters ^^^^^^^^^^
read()
or sample()
pigpio.pi()
read(retries=0) raises TimeoutError
Take a single reading from the sensor.
Parameters ^^^^^^^^^^
valid = False
Returns
^^^^^^^
A Dictionary in the form {'temp_c': 20, 'temp_f': 68.0, 'humidity': 35, 'valid': True}
Where:
Discard readings where value == False
and try again.
Raises ^^^^^^
TimeoutError """"""""""""
gpio
does not respondtimeout_secs
(see _Constructor), but the response cannot be understood by the library. Try increasing timeout_secs
Also see ^^^^^^^^
DHT Sensors are Slow
_
sample(samples=5, max_retries=None) raises TimeoutError
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:
valid = False
. Default to samples * 2Raises ^^^^^^
TimeoutError """"""""""""
read()
, plusmax_retries
is reached