#!/usr/bin/python
import RPi.GPIO as GPIO
from time import sleep
import dht11
SignalPin = 18
delayTime = 5
def setup():
# Disable warning "This channel is already in use . . ."
#GPIO.setwarnings(False)
# Set the GPIO pins as numbering
GPIO.setmode(GPIO.BOARD)
def destroy():
# Release resource
GPIO.cleanup()
def loop():
instance = dht11.DHT11(pin = SignalPin)
while True:
sleep( delayTime )
#get DHT11 sensor value
result = instance.read()
print "Temperature = ",result.temperature,"C"," Humidity = ",result.humidity,"%"
# The Program will start from here
if __name__ == '__main__':
setup()
try:
loop()
# When control c is pressed child program destroy() will be executed.
except KeyboardInterrupt:
destroy()
|