update DHT11 program

The DHT manufacturer updates the firmware, so we also need to update the
program
This commit is contained in:
Suhayl
2018-03-07 16:52:07 +08:00
parent e7d3fb753d
commit c399737b41
7 changed files with 27 additions and 22 deletions
+6 -5
View File
@@ -2,7 +2,7 @@
* Filename : DHT.cpp
* Description : DHT Temperature & Humidity Sensor library for Raspberry
* Author : freenove
* modification: 2016/07/10
* modification: 2018/03/07
**********************************************************************/
#include "DHT.hpp"
//Function: Read DHT sensor, store the original data in bits[]
@@ -22,7 +22,7 @@ int DHT::readSensor(int pin,int wakeupDelay){
delayMicroseconds(40);
pinMode(pin,INPUT);
int loopCnt = DHTLIB_TIMEOUT;
int32_t loopCnt = DHTLIB_TIMEOUT;
t = micros();
while(digitalRead(pin)==LOW){
if((micros() - t) > loopCnt){
@@ -61,13 +61,14 @@ int DHT::readSensor(int pin,int wakeupDelay){
}
pinMode(pin,OUTPUT);
digitalWrite(pin,HIGH);
//printf("bits:\t%d,\t%d,\t%d,\t%d,\t%d\n",bits[0],bits[1],bits[2],bits[3],bits[4]);
return DHTLIB_OK;
}
//FunctionRead DHT sensor, analyze the data of temperature and humidity
//returnDHTLIB_OK DHTLIB_ERROR_CHECKSUM DHTLIB_ERROR_TIMEOUT
int DHT::readDHT11(int pin){
int rv ;
int8_t sum;
uint8_t sum;
rv = readSensor(pin,DHTLIB_DHT11_WAKEUP);
if(rv != DHTLIB_OK){
humidity = DHTLIB_INVALID_VALUE;
@@ -75,8 +76,8 @@ int DHT::readDHT11(int pin){
return rv;
}
humidity = bits[0];
temperature = bits[2];
sum = bits[0] + bits[2];
temperature = bits[2] + bits[3] * 0.1;
sum = bits[0] + bits[1] + bits[2] + bits[3];
if(bits[4] != sum)
return DHTLIB_ERROR_CHECKSUM;
return DHTLIB_OK;
+2 -2
View File
@@ -2,7 +2,7 @@
* Filename : DHT.hpp
* Description : DHT Temperature & Humidity Sensor library for Raspberry
* Author : freenove
* modification: 2016/07/10
* modification: 2018/03/07
**********************************************************************/
#ifndef _DHT_H_
#define _DHT_H_
@@ -27,7 +27,7 @@ class DHT{
double humidity,temperature; //use to store temperature and humidity data read
int readDHT11(int pin); //read DHT11
private:
int bits[5]; //Buffer to receiver data
uint8_t bits[5]; //Buffer to receiver data
int readSensor(int pin,int wakeupDelay); //
};
Binary file not shown.
Binary file not shown.
+2 -2
View File
@@ -2,7 +2,7 @@
* Filename : DHT11.cpp
* Description : read the temperature and humidity data of DHT11
* Author : freenove
* modification: 2016/07/10
* modification: 2018/03/07
**********************************************************************/
#include <wiringPi.h>
#include <stdio.h>
@@ -37,7 +37,7 @@ int main(){
break;
}
printf("Humidity is %.2f %%, \t Temperature is %.2f *C\n\n",dht.humidity,dht.temperature);
delay(1000);
delay(2000);
}
return 1;
}
+3 -3
View File
@@ -3,7 +3,7 @@
# Filename : DHT11.py
# Description : read the temperature and humidity data of DHT11
# Author : freenove
# modification: 2016/07/11
# modification: 2018/03/07
########################################################################
import RPi.GPIO as GPIO
import time
@@ -15,7 +15,7 @@ def loop():
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.
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!"
@@ -27,7 +27,7 @@ def loop():
print"Other error!"
print"Humidity : %.2f, \t Temperature : %.2f \n"%(dht.humidity,dht.temperature)
time.sleep(1)
time.sleep(2)
if __name__ == '__main__':
print 'Program is starting ... '
+14 -10
View File
@@ -2,7 +2,7 @@
# Filename : Freenove_DHT.py
# Description : DHT Temperature & Humidity Sensor library for Raspberry
# Author : freenove
# modification: 2016/07/11
# modification: 2018/03/07
########################################################################
import RPi.GPIO as GPIO
import time
@@ -13,7 +13,7 @@ class DHT(object):
DHTLIB_ERROR_TIMEOUT = -2
DHTLIB_INVALID_VALUE = -999
DHTLIB_DHT11_WAKEUP = 0.018 #18ms
DHTLIB_DHT11_WAKEUP = 0.020#0.018 #18ms
DHTLIB_TIMEOUT = 0.0001 #100us
humidity = 0
@@ -32,26 +32,30 @@ class DHT(object):
GPIO.output(pin,GPIO.LOW)
time.sleep(wakeupDelay)
GPIO.output(pin,GPIO.HIGH)
time.sleep(40*0.000001)
#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
@@ -60,20 +64,20 @@ class DHT(object):
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,pin):
rv = self.readSensor(pin,self.DHTLIB_DHT11_WAKEUP)
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]
sumChk = ((self.bits[0] + self.bits[2]) & 0xff)
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
@@ -84,13 +88,13 @@ def loop():
okCnt = 0
while(True):
sumCnt += 1
chk = dht.readDHT11(11)
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(1)
time.sleep(3)
if __name__ == '__main__':
print 'Program is starting ... '