29 lines
848 B
Python
29 lines
848 B
Python
import time
|
|
import board
|
|
import adafruit_dht
|
|
|
|
# https://randomnerdtutorials.com/raspberry-pi-dht11-dht22-python/
|
|
|
|
# Sensor data pin is connected to GPIO 4
|
|
sensor = adafruit_dht.DHT22(board.D4)
|
|
# Uncomment for DHT11
|
|
#sensor = adafruit_dht.DHT11(board.D4)
|
|
|
|
while True:
|
|
try:
|
|
# Print the values to the serial port
|
|
temperature_c = sensor.temperature
|
|
temperature_f = temperature_c * (9 / 5) + 32
|
|
humidity = sensor.humidity
|
|
print("Temp={0:0.1f}ºC, Temp={1:0.1f}ºF, Humidity={2:0.1f}%".format(temperature_c, temperature_f, 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:
|
|
sensor.exit()
|
|
raise error
|
|
|
|
time.sleep(3.0) |