MyPyDHT is a small library in python 3 for the Raspberry Pi (1, 2, 3 and Zero) to interact with the humidity & temperature sensors DHT11, DHT22 and AM2302.
Before installing it be sure that your Pi is compatible with Python Extensions. On Raspbian the following commands will ensure it:
apt-get update
sudo apt-get install build-essential python-dev
Since this library use git submodels use the following command to clone the repository:
git clone --recursive https://github.com/freedom27/MyPyDHT
Once the repository has been cloned enter the following command inside the main folder:
sudo python setup.py install
Now MyPyDHT is succesfully installed and ready to be used inside your python projects!
In any python file you whish to use the library import the module and call the sensor_read function as follow:
import MyPyDHT
# if for instance you have a DHT22 sensor connected to the GPIO port 17
humidity, temperature = MyPyDHT.sensor_read(MyPyDHT.Sensor.DHT22, 17)
# humidity = 22.1 (it's a percentage), temperature = 20.3 (celsius degrees)
In the example above th function sensor_read accept in input the BCM GPIO port to which the sensor is connected. With just the GPIO port in input the module will try to access the sensor but in case of failure it raises a DHTException.
The full signature of the sensor_read function is:
def sensor_read(sensor_type, gpio_pin, reading_attempts=1, use_cache=False):
Where:
- sensor_type: is an enum (the Sensor enum) telling which model of DHT sensors is we are trying to read; possible values are DHT11 and DHT22
- gpio_pin: the number of the port to which the sensor is connected
- reading_attempts (optional): this is the total number of attempts to read data from the sensor before returning an excaption in case of failure
- use_cache (optional): if this parameter is set to true in case of failure of all the the reading attempts the function will return the last valid data retrieved from the sensor instead of raising an Exception (if sampling rate is high cached data should be accurate)
The DHTException class has an attribude message describing what issue occurred while attempting to read the sensor.