1234567891011121314151617181920212223242526272829 |
- import time
- import board
- import adafruit_dht
-
- # connect Device
- dhtDevice = adafruit_dht.DHT22(board.D4, use_pulseio=False)
-
- # you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio.
- # This may be necessary on a Linux single board computer like the Raspberry Pi,
- # but it will not work in CircuitPython.
- # dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False)
-
- while True:
- try:
- # Print the values to the serial port
- temperature_c = dhtDevice.temperature
- humidity = dhtDevice.humidity
- print("Temp: {:.1f} C Humidity: {}% ".format(temperature_c, humidity))
-
- except RuntimeError as error:
- # Errors happen fairly often, DHT's are hard to read, just keep going
- print(error.args[0])
- time.sleep(2.0)
- continue
- except Exception as error:
- dhtDevice.exit()
- raise error
-
- time.sleep(2.0)
|