import Adafruit_DHT sensor = Adafruit_DHT.DHT11 pin = 4 humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) print('Temp: {}'.format(temperature)) print('Humidity: {}'.format(humidity))
import Adafruit_DHT sensor = Adafruit_DHT.DHT11 pin = 4 humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) if humidity is not None and temperature is not None: print('Temp: {}'.format(temperature)) print('Humidity: {}'.format(humidity)) else: print('Failed to read sensor data')This code is similar to the first example but includes error handling. The `if` statement checks that the sensor data was properly read and prints it to the console. If there was an error in reading the sensor data, an error message is displayed. The package library for Adafruit_DHT is available on GitHub at https://github.com/adafruit/Adafruit_Python_DHT.