This simple class can be used for reading temperature and humidity values from DHT11 sensor on Raspberry Pi.
To install, just run following:
pip install dht11
- Instantiate the
DHT11class with the pin number as constructor parameter. - Call
read()method, which will returnDHT11Resultobject with actual values and error code.
For example:
importRPi.GPIOasGPIOimportdht11# initialize GPIOGPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
# read data using pin 14instance=dht11.DHT11(pin=14)
result=instance.read()
ifresult.is_valid():
print("Temperature: %-3.1f C"%result.temperature)
print("Humidity: %-3.1f %%"%result.humidity)
else:
print("Error: %d"%result.error_code)For working example, see dht11_example.py (you probably need to adjust pin for your configuration)
This project is licensed under the terms of the MIT license.