2024-10-10 22:15:46 +05:30
|
|
|
# NOTE: GPIO PIN 16 used
|
|
|
|
|
|
|
|
import RPi.GPIO as GPIO
|
|
|
|
import dht11
|
2024-10-10 22:40:20 +05:30
|
|
|
import time
|
2024-10-10 22:15:46 +05:30
|
|
|
|
|
|
|
# initialize GPIO
|
2024-11-06 11:53:41 +05:30
|
|
|
GPIO.setwarnings(False) # Set True if you are having trouble connecting the DHT11 sensor to Raspberry Pi. Doing so will show warnings on screen.
|
2024-10-10 22:15:46 +05:30
|
|
|
GPIO.setmode(GPIO.BCM)
|
|
|
|
GPIO.cleanup()
|
|
|
|
|
2024-10-10 22:40:20 +05:30
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
# read data using pin 16
|
|
|
|
instance = dht11.DHT11(pin = 16)
|
|
|
|
result = instance.read()
|
2024-10-10 22:15:46 +05:30
|
|
|
|
2024-10-10 22:40:20 +05:30
|
|
|
if result.is_valid():
|
|
|
|
print("Temperature: %-3.1f C" % result.temperature)
|
|
|
|
print("Humidity: %-3.1f %%" % result.humidity)
|
|
|
|
else:
|
|
|
|
print("Error: %d" % result.error_code)
|
|
|
|
time.sleep(3)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("Program stopped by user.")
|
|
|
|
GPIO.cleanup()
|