From 3d718518469f1944dafa1aeb0e6948dc20dfff4e Mon Sep 17 00:00:00 2001 From: Suhayl <838119509@qq.com> Date: Fri, 16 Oct 2020 17:56:30 +0800 Subject: [PATCH] update dht. Optimize DHT11 code --- Code/C_Code/21.1.1_DHT11/DHT.cpp | 41 +++- Code/C_Code/21.1.1_DHT11/DHT.hpp | 8 +- Code/C_Code/21.1.1_DHT11/DHT11.cpp | 45 ++-- Code/Python_Code/21.1.1_DHT11/DHT11.py | 24 +- Code/Python_Code/21.1.1_DHT11/Freenove_DHT.py | 216 ++++++++++-------- Code/Python_Code/21.1.1_DHT11/setup.py | 18 +- 6 files changed, 199 insertions(+), 153 deletions(-) diff --git a/Code/C_Code/21.1.1_DHT11/DHT.cpp b/Code/C_Code/21.1.1_DHT11/DHT.cpp index 0d4eb54..57c43b9 100644 --- a/Code/C_Code/21.1.1_DHT11/DHT.cpp +++ b/Code/C_Code/21.1.1_DHT11/DHT.cpp @@ -4,10 +4,14 @@ Used for Raspberry Pi. * Program transplantation by Freenove. * Author : freenove -* modification: 2019/12/28 +* modification: 2020/10/16 * Reference : https://github.com/RobTillaart/Arduino/tree/master/libraries/DHTlib **********************************************************************/ #include "DHT.hpp" + +DHT::DHT(){ + wiringPiSetup(); +} //Function: Read DHT sensor, store the original data in bits[] // return values:DHTLIB_OK DHTLIB_ERROR_CHECKSUM DHTLIB_ERROR_TIMEOUT int DHT::readSensor(int pin,int wakeupDelay){ @@ -18,22 +22,41 @@ int DHT::readSensor(int pin,int wakeupDelay){ for (i=0;i<5;i++){ bits[i] = 0; } + // Clear sda pinMode(pin,OUTPUT); + digitalWrite(pin,HIGH); + delay(500); + // Start signal digitalWrite(pin,LOW); delay(wakeupDelay); digitalWrite(pin,HIGH); - delayMicroseconds(40); + // delayMicroseconds(40); pinMode(pin,INPUT); int32_t loopCnt = DHTLIB_TIMEOUT; t = micros(); + // Waiting echo + while(1){ + if(digitalRead(pin)==LOW){ + break; + } + if((micros() - t) > loopCnt){ + return DHTLIB_ERROR_TIMEOUT; + } + } + + loopCnt = DHTLIB_TIMEOUT; + t = micros(); + // Waiting echo low level end while(digitalRead(pin)==LOW){ if((micros() - t) > loopCnt){ return DHTLIB_ERROR_TIMEOUT; } } + loopCnt = DHTLIB_TIMEOUT; t = micros(); + // Waiting echo high level end while(digitalRead(pin)==HIGH){ if((micros() - t) > loopCnt){ return DHTLIB_ERROR_TIMEOUT; @@ -69,7 +92,7 @@ int DHT::readSensor(int pin,int wakeupDelay){ } //Function:Read DHT sensor, analyze the data of temperature and humidity //return:DHTLIB_OK DHTLIB_ERROR_CHECKSUM DHTLIB_ERROR_TIMEOUT -int DHT::readDHT11(int pin){ +int DHT::readDHT11Once(int pin){ int rv ; uint8_t sum; rv = readSensor(pin,DHTLIB_DHT11_WAKEUP); @@ -86,5 +109,17 @@ int DHT::readDHT11(int pin){ return DHTLIB_OK; } +int DHT::readDHT11(int pin){ + int chk = DHTLIB_INVALID_VALUE; + for (int i = 0; i < 15; i++){ + chk = readDHT11Once(pin); //read DHT11 and get a return value. Then determine whether data read is normal according to the return value. + if(chk == DHTLIB_OK){ + return DHTLIB_OK; + } + delay(100); + } + return chk; +} + diff --git a/Code/C_Code/21.1.1_DHT11/DHT.hpp b/Code/C_Code/21.1.1_DHT11/DHT.hpp index 5d572de..b248da3 100644 --- a/Code/C_Code/21.1.1_DHT11/DHT.hpp +++ b/Code/C_Code/21.1.1_DHT11/DHT.hpp @@ -2,9 +2,9 @@ * Filename : DHT.hpp * Description : DHT Temperature & Humidity Sensor library for Raspberry. Used for Raspberry Pi. -* Program transplantation by Freenove. +* Program transplantation by Freenove. * Author : freenove -* modification: 2019/12/28 +* modification: 2020/10/16 * Reference : https://github.com/RobTillaart/Arduino/tree/master/libraries/DHTlib **********************************************************************/ #ifndef _DHT_H_ @@ -20,14 +20,16 @@ #define DHTLIB_ERROR_TIMEOUT -2 #define DHTLIB_INVALID_VALUE -999 -#define DHTLIB_DHT11_WAKEUP 18 +#define DHTLIB_DHT11_WAKEUP 20 #define DHTLIB_DHT_WAKEUP 1 #define DHTLIB_TIMEOUT 100 class DHT{ public: + DHT(); double humidity,temperature; //use to store temperature and humidity data read + int readDHT11Once(int pin); //read DHT11 int readDHT11(int pin); //read DHT11 private: uint8_t bits[5]; //Buffer to receiver data diff --git a/Code/C_Code/21.1.1_DHT11/DHT11.cpp b/Code/C_Code/21.1.1_DHT11/DHT11.cpp index 9d2c48b..dccead4 100644 --- a/Code/C_Code/21.1.1_DHT11/DHT11.cpp +++ b/Code/C_Code/21.1.1_DHT11/DHT11.cpp @@ -1,45 +1,36 @@ + /********************************************************************** * Filename : DHT11.cpp * Description : Read the temperature and humidity data of DHT11 * Author : www.freenove.com -* modification: 2019/12/27 +* modification: 2020/10/16 **********************************************************************/ #include #include #include #include "DHT.hpp" -#define DHT11_Pin 0 //define the pin of sensor +#define DHT11_Pin 0 //define the pin of sensor int main(){ - DHT dht; //create a DHT class object - int chk,sumCnt;//chk:read the return value of sensor; sumCnt:times of reading sensor - + DHT dht; //create a DHT class object + int chk, counts; //chk:read the return value of sensor; sumCnt:times of reading sensor + printf("Program is starting ...\n"); - - wiringPiSetup(); - - while(1){ - chk = dht.readDHT11(DHT11_Pin); //read DHT11 and get a return value. Then determine whether data read is normal according to the return value. - sumCnt++; //counting number of reading times - printf("The sumCnt is : %d \n",sumCnt); - switch(chk){ - case DHTLIB_OK: //if the return value is DHTLIB_OK, the data is normal. - printf("DHT11,OK! \n"); - break; - case DHTLIB_ERROR_CHECKSUM: //data check has errors - printf("DHTLIB_ERROR_CHECKSUM! \n"); - break; - case DHTLIB_ERROR_TIMEOUT: //reading DHT times out - printf("DHTLIB_ERROR_TIMEOUT! \n"); - break; - case DHTLIB_INVALID_VALUE: //other errors - printf("DHTLIB_INVALID_VALUE! \n"); + + while (1){ + counts++; //counting number of reading times + printf("Measurement counts : %d \n", counts); + for (int i = 0; i < 15; i++){ + chk = dht.readDHT11(DHT11_Pin); //read DHT11 and get a return value. Then determine whether data read is normal according to the return value. + if(chk == DHTLIB_OK){ + printf("DHT11,OK! \n"); break; + } + delay(100); } - printf("Humidity is %.2f %%, \t Temperature is %.2f *C\n\n",dht.humidity,dht.temperature); + printf("Humidity is %.2f %%, \t Temperature is %.2f *C\n\n",dht.humidity, dht.temperature); delay(2000); - } + } return 1; } - diff --git a/Code/Python_Code/21.1.1_DHT11/DHT11.py b/Code/Python_Code/21.1.1_DHT11/DHT11.py index d8a16a9..fa63095 100644 --- a/Code/Python_Code/21.1.1_DHT11/DHT11.py +++ b/Code/Python_Code/21.1.1_DHT11/DHT11.py @@ -3,7 +3,7 @@ # Filename : DHT11.py # Description : read the temperature and humidity data of DHT11 # Author : freenove -# modification: 2018/08/03 +# modification: 2020/10/16 ######################################################################## import RPi.GPIO as GPIO import time @@ -12,20 +12,16 @@ DHTPin = 11 #define the pin of DHT11 def loop(): dht = DHT.DHT(DHTPin) #create a DHT class object - sumCnt = 0 #number of reading times + counts = 0 # Measurement counts while(True): - sumCnt += 1 #counting number of reading times - chk = dht.readDHT11() #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!") - + counts += 1 + print("Measurement counts: ", counts) + for i in range(0,15): + chk = dht.readDHT11() #read DHT11 and get a return value. Then determine whether data read is normal according to the return value. + 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!") + break + time.sleep(0.1) print("Humidity : %.2f, \t Temperature : %.2f \n"%(dht.humidity,dht.temperature)) time.sleep(2) diff --git a/Code/Python_Code/21.1.1_DHT11/Freenove_DHT.py b/Code/Python_Code/21.1.1_DHT11/Freenove_DHT.py index d53a34e..72ef1a0 100644 --- a/Code/Python_Code/21.1.1_DHT11/Freenove_DHT.py +++ b/Code/Python_Code/21.1.1_DHT11/Freenove_DHT.py @@ -1,108 +1,130 @@ #!/usr/bin/env python3 ############################################################################# # Filename : Freenove_DHT.py -# Description : DHT Temperature & Humidity Sensor library for Raspberry +# Description : DHT Temperature & Humidity Sensor library for Raspberry # Author : freenove -# modification: 2018/08/03 +# modification: 2020/10/16 ######################################################################## import RPi.GPIO as GPIO import time class DHT(object): - DHTLIB_OK = 0 - DHTLIB_ERROR_CHECKSUM = -1 - DHTLIB_ERROR_TIMEOUT = -2 - DHTLIB_INVALID_VALUE = -999 - - DHTLIB_DHT11_WAKEUP = 0.020#0.018 #18ms - DHTLIB_TIMEOUT = 0.0001 #100us - - humidity = 0 - temperature = 0 - - def __init__(self,pin): - self.pin = pin - self.bits = [0,0,0,0,0] - GPIO.setmode(GPIO.BOARD) - #Read DHT sensor, store the original data in bits[] - def readSensor(self,pin,wakeupDelay): - mask = 0x80 - idx = 0 - self.bits = [0,0,0,0,0] - GPIO.setup(pin,GPIO.OUT) - GPIO.output(pin,GPIO.LOW) - time.sleep(wakeupDelay) - GPIO.output(pin,GPIO.HIGH) - #time.sleep(40*0.000001) - GPIO.setup(pin,GPIO.IN) - - loopCnt = self.DHTLIB_TIMEOUT - t = time.time() - while(GPIO.input(pin) == GPIO.LOW): - if((time.time() - t) > loopCnt): - #print ("Echo LOW") - return self.DHTLIB_ERROR_TIMEOUT - t = time.time() - while(GPIO.input(pin) == GPIO.HIGH): - if((time.time() - t) > loopCnt): - #print ("Echo HIGH") - return self.DHTLIB_ERROR_TIMEOUT - for i in range(0,40,1): - t = time.time() - while(GPIO.input(pin) == GPIO.LOW): - if((time.time() - t) > loopCnt): - #print ("Data Low %d"%(i)) - return self.DHTLIB_ERROR_TIMEOUT - t = time.time() - while(GPIO.input(pin) == GPIO.HIGH): - if((time.time() - t) > loopCnt): - #print ("Data HIGH %d"%(i)) - return self.DHTLIB_ERROR_TIMEOUT - if((time.time() - t) > 0.00005): - self.bits[idx] |= mask - #print("t : %f"%(time.time()-t)) - mask >>= 1 - if(mask == 0): - mask = 0x80 - idx += 1 - #print (self.bits) - GPIO.setup(pin,GPIO.OUT) - GPIO.output(pin,GPIO.HIGH) - return self.DHTLIB_OK - #Read DHT sensor, analyze the data of temperature and humidity - def readDHT11(self): - rv = self.readSensor(self.pin,self.DHTLIB_DHT11_WAKEUP) - if (rv is not self.DHTLIB_OK): - self.humidity = self.DHTLIB_INVALID_VALUE - self.temperature = self.DHTLIB_INVALID_VALUE - return rv - self.humidity = self.bits[0] - self.temperature = self.bits[2] + self.bits[3]*0.1 - sumChk = ((self.bits[0] + self.bits[1] + self.bits[2] + self.bits[3]) & 0xFF) - if(self.bits[4] is not sumChk): - return self.DHTLIB_ERROR_CHECKSUM - return self.DHTLIB_OK - + DHTLIB_OK = 0 + DHTLIB_ERROR_CHECKSUM = -1 + DHTLIB_ERROR_TIMEOUT = -2 + DHTLIB_INVALID_VALUE = -999 + + DHTLIB_DHT11_WAKEUP = 0.020#0.018 #18ms + DHTLIB_TIMEOUT = 0.0001 #100us + + humidity = 0 + temperature = 0 + + def __init__(self,pin): + self.pin = pin + self.bits = [0,0,0,0,0] + GPIO.setmode(GPIO.BOARD) + #Read DHT sensor, store the original data in bits[] + def readSensor(self,pin,wakeupDelay): + mask = 0x80 + idx = 0 + self.bits = [0,0,0,0,0] + # Clear sda + GPIO.setup(pin,GPIO.OUT) + GPIO.output(pin,GPIO.HIGH) + time.sleep(0.5) + # start signal + GPIO.output(pin,GPIO.LOW) + time.sleep(wakeupDelay) + GPIO.output(pin,GPIO.HIGH) + # time.sleep(0.000001) + GPIO.setup(pin,GPIO.IN) + + loopCnt = self.DHTLIB_TIMEOUT + # Waiting echo + t = time.time() + while True: + if (GPIO.input(pin) == GPIO.LOW): + break + if((time.time() - t) > loopCnt): + return self.DHTLIB_ERROR_TIMEOUT + # Waiting echo low level end + t = time.time() + while(GPIO.input(pin) == GPIO.LOW): + if((time.time() - t) > loopCnt): + #print ("Echo LOW") + return self.DHTLIB_ERROR_TIMEOUT + # Waiting echo high level end + t = time.time() + while(GPIO.input(pin) == GPIO.HIGH): + if((time.time() - t) > loopCnt): + #print ("Echo HIGH") + return self.DHTLIB_ERROR_TIMEOUT + for i in range(0,40,1): + t = time.time() + while(GPIO.input(pin) == GPIO.LOW): + if((time.time() - t) > loopCnt): + #print ("Data Low %d"%(i)) + return self.DHTLIB_ERROR_TIMEOUT + t = time.time() + while(GPIO.input(pin) == GPIO.HIGH): + if((time.time() - t) > loopCnt): + #print ("Data HIGH %d"%(i)) + return self.DHTLIB_ERROR_TIMEOUT + if((time.time() - t) > 0.00005): + self.bits[idx] |= mask + #print("t : %f"%(time.time()-t)) + mask >>= 1 + if(mask == 0): + mask = 0x80 + idx += 1 + #print (self.bits) + GPIO.setup(pin,GPIO.OUT) + GPIO.output(pin,GPIO.HIGH) + return self.DHTLIB_OK + #Read DHT sensor, analyze the data of temperature and humidity + def readDHT11Once(self): + rv = self.readSensor(self.pin,self.DHTLIB_DHT11_WAKEUP) + if (rv is not self.DHTLIB_OK): + self.humidity = self.DHTLIB_INVALID_VALUE + self.temperature = self.DHTLIB_INVALID_VALUE + return rv + self.humidity = self.bits[0] + self.temperature = self.bits[2] + self.bits[3]*0.1 + sumChk = ((self.bits[0] + self.bits[1] + self.bits[2] + self.bits[3]) & 0xFF) + if(self.bits[4] is not sumChk): + return self.DHTLIB_ERROR_CHECKSUM + return self.DHTLIB_OK + def readDHT11(self): + result = self.DHTLIB_INVALID_VALUE + for i in range(0,15): + result = self.readDHT11Once() + if result == self.DHTLIB_OK: + return self.DHTLIB_OK + time.sleep(0.1) + return result + + def loop(): - dht = DHT(11) - sumCnt = 0 - okCnt = 0 - while(True): - sumCnt += 1 - chk = dht.readDHT11() - if (chk is 0): - okCnt += 1 - okRate = 100.0*okCnt/sumCnt; - print("sumCnt : %d, \t okRate : %.2f%% "%(sumCnt,okRate)) - print("chk : %d, \t Humidity : %.2f, \t Temperature : %.2f "%(chk,dht.humidity,dht.temperature)) - time.sleep(3) - + dht = DHT(11) + sumCnt = 0 + okCnt = 0 + while(True): + sumCnt += 1 + chk = dht.readDHT11() + if (chk is 0): + okCnt += 1 + okRate = 100.0*okCnt/sumCnt; + print("sumCnt : %d, \t okRate : %.2f%% "%(sumCnt,okRate)) + print("chk : %d, \t Humidity : %.2f, \t Temperature : %.2f "%(chk,dht.humidity,dht.temperature)) + time.sleep(3) + if __name__ == '__main__': - print ('Program is starting ... ') - try: - loop() - except KeyboardInterrupt: - pass - exit() - - + print ('Program is starting ... ') + try: + loop() + except KeyboardInterrupt: + pass + exit() + + diff --git a/Code/Python_Code/21.1.1_DHT11/setup.py b/Code/Python_Code/21.1.1_DHT11/setup.py index 8c0c9d5..e9e5c1a 100644 --- a/Code/Python_Code/21.1.1_DHT11/setup.py +++ b/Code/Python_Code/21.1.1_DHT11/setup.py @@ -2,12 +2,12 @@ from setuptools import setup,find_packages setup( - name = "Freenove_DHT", - version = "V1.0.0", - description = "Read DHT Sensor", - author = "Freenove", - url = "http://www.freenove.com", - license = " ", - packages = find_packages(), - scripts = ["Freenove_DHT.py"], - ) + name = "Freenove_DHT", + version = "V1.0.1", + description = "Read DHT Sensor", + author = "Freenove", + url = "http://www.freenove.com", + license = " ", + packages = find_packages(), + scripts = ["Freenove_DHT.py"], + )