2024-10-10 21:56:13 +05:30
|
|
|
import time
|
|
|
|
import board
|
|
|
|
import adafruit_dht
|
2024-10-07 00:03:57 +05:30
|
|
|
|
2024-10-10 21:56:13 +05:30
|
|
|
# Initial the dht device, with data pin connected to:
|
|
|
|
dhtDevice = adafruit_dht.DHT11(board.D16) # by default I'm using GPIO PIN 16 for data/signal transmission of DHT11
|
2024-10-07 00:03:57 +05:30
|
|
|
|
2024-10-10 21:56:13 +05:30
|
|
|
# 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)
|
2024-10-07 00:03:57 +05:30
|
|
|
|
2024-10-10 21:56:13 +05:30
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
# Print the values to the serial port
|
|
|
|
temperature_c = dhtDevice.temperature # Celsius
|
|
|
|
temperature_f = temperature_c * (9 / 5) + 32 # Fahrenheit
|
|
|
|
humidity = dhtDevice.humidity
|
|
|
|
print(
|
|
|
|
"Temp: {:.1f} F / {:.1f} C Humidity: {}% ".format(
|
|
|
|
temperature_f, temperature_c, humidity
|
|
|
|
)
|
|
|
|
)
|
2024-10-07 00:03:57 +05:30
|
|
|
|
2024-10-10 21:56:13 +05:30
|
|
|
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)
|
2024-10-07 00:03:57 +05:30
|
|
|
|