#!/usr/bin/env python
#############################################################################
# Filename    : DHT11.py
# Description :	read the temperature and humidity data of DHT11
# Author      : freenove
# modification: 2016/07/11
########################################################################
import RPi.GPIO as GPIO
import time
import Freenove_DHT as DHT
DHTPin = 11     #define the pin of DHT11

def loop():
    dht = DHT.DHT(DHTPin)   #create a DHT class object
    sumCnt = 0              #number of reading times 
    while(True):
        sumCnt += 1         #counting number of reading times
        chk = dht.readDHT11(DHTPin)     #read DHT11 and get a return value. Then determine whether data read is normal according to the return value.
        print"The sumCnt is : %d, \t chk    : %d"%(sumCnt,chk)  
        if (chk is dht.DHTLIB_OK):      #read DHT11 and get a return value. Then determine whether data read is normal according to the return value.
            print"DHT11,OK!"        
        elif(chk is dht.DHTLIB_ERROR_CHECKSUM): #data check has errors
            print"DHTLIB_ERROR_CHECKSUM!!"
        elif(chk is dht.DHTLIB_ERROR_TIMEOUT):  #reading DHT times out
            print"DHTLIB_ERROR_TIMEOUT!"
        else:               #other errors
            print"Other error!"
            
        print"Humidity : %.2f, \t Temperature : %.2f \n"%(dht.humidity,dht.temperature)     
        time.sleep(1)       
        
if __name__ == '__main__':
    print 'Program is starting ... '
    try:
        loop()
    except KeyboardInterrupt:
        GPIO.cleanup()
        exit()  

