Code now shows temperature and humidity infinitely until keyboard interrupt.

This commit is contained in:
K 2024-10-10 22:40:20 +05:30
parent 5b706691be
commit fd1b6d2073
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F

View File

@ -2,12 +2,15 @@
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
import dht11 import dht11
import time
# initialize GPIO # initialize GPIO
GPIO.setwarnings(False) GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
GPIO.cleanup() GPIO.cleanup()
try:
while True:
# read data using pin 16 # read data using pin 16
instance = dht11.DHT11(pin = 16) instance = dht11.DHT11(pin = 16)
result = instance.read() result = instance.read()
@ -17,5 +20,7 @@ if result.is_valid():
print("Humidity: %-3.1f %%" % result.humidity) print("Humidity: %-3.1f %%" % result.humidity)
else: else:
print("Error: %d" % result.error_code) print("Error: %d" % result.error_code)
time.sleep(3)
except KeyboardInterrupt:
print("Program stopped by user.")
GPIO.cleanup() GPIO.cleanup()