Code now shows temperature and humidity infinitely until keyboard interrupt.
This commit is contained in:
parent
5b706691be
commit
7e96b4cadd
25
temp-calc.py
25
temp-calc.py
@ -2,20 +2,25 @@
|
|||||||
|
|
||||||
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()
|
||||||
|
|
||||||
# read data using pin 16
|
try:
|
||||||
instance = dht11.DHT11(pin = 16)
|
while True:
|
||||||
result = instance.read()
|
# read data using pin 16
|
||||||
|
instance = dht11.DHT11(pin = 16)
|
||||||
|
result = instance.read()
|
||||||
|
|
||||||
if result.is_valid():
|
if result.is_valid():
|
||||||
print("Temperature: %-3.1f C" % result.temperature)
|
print("Temperature: %-3.1f C" % result.temperature)
|
||||||
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)
|
||||||
GPIO.cleanup()
|
except KeyboardInterrupt:
|
||||||
|
print("Program stopped by user.")
|
||||||
|
GPIO.cleanup()
|
||||||
|
Loading…
Reference in New Issue
Block a user