22 lines
482 B
Python
22 lines
482 B
Python
import time
|
|
import board
|
|
import adafruit_dht
|
|
|
|
# board.D2 is GPIO2, some other pins can work as well
|
|
sensor = adafruit_dht.DHT22(board.D2)
|
|
|
|
while True:
|
|
try:
|
|
print(f"{sensor.temperature=:.1f}ºC, {sensor.humidity=:.1f}%")
|
|
|
|
except RuntimeError as error:
|
|
# Errors happen fairly often, just keep going
|
|
print(error.args[0])
|
|
time.sleep(2)
|
|
continue
|
|
except Exception as error:
|
|
sensor.exit()
|
|
raise error
|
|
|
|
time.sleep(10)
|