DHT library

Fork of DHT by Wim De Roeve

Revision:
1:6d97d5338f91
Parent:
0:9b5b3200688f
--- a/DHT.cpp	Mon Jul 09 19:47:43 2012 +0000
+++ b/DHT.cpp	Sun Aug 21 17:48:16 2016 +0000
@@ -32,7 +32,9 @@
 
 #include "DHT.h"
 
-#define DHT_DATA_BIT_COUNT 41
+//extern Serial pc;
+// was 41
+#define DHT_DATA_BIT_COUNT 40
 
 DHT::DHT(PinName pin,int DHTtype) {
     _pin = pin;
@@ -134,12 +136,19 @@
         DHT_data[i]=b;
     }
 
-    if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF)) {
+    if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF) ||
+        DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]-1) & 0xFF)) {
         _lastReadTime = currentTime;
         _lastTemperature=CalcTemperature();
         _lastHumidity=CalcHumidity();
 
     } else {
+/*for( int i=0; i < 5; i++ ) {
+    pc.printf("%02X ",DHT_data[i]);
+    }
+    pc.printf("\n");
+    pc.printf("%04X\n",(DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]));
+*/
         err = ERROR_CHECKSUM;
     }
 
@@ -148,12 +157,12 @@
 }
 
 float DHT::CalcTemperature() {
-    int v;
+    float v;
 
     switch (_DHTtype) {
         case DHT11:
             v = DHT_data[2];
-            return float(v);
+            return v;
         case DHT22:
             v = DHT_data[2] & 0x7F;
             v *= 256;
@@ -161,7 +170,7 @@
             v /= 10;
             if (DHT_data[2] & 0x80)
                 v *= -1;
-            return float(v);
+            return v;
     }
     return 0;
 }
@@ -171,25 +180,25 @@
 }
 
 float DHT::ConvertCelciustoFarenheit(float celsius) {
-    return celsius * 9 / 5 + 32;
+    return celsius * 9 / 5 + 32.0f;
 }
 
 float DHT::ConvertCelciustoKelvin(float celsius) {
-    return celsius + 273.15;
+    return celsius + 273.15f;
 }
 
 // dewPoint function NOAA
 // reference: http://wahiduddin.net/calc/density_algorithms.htm
 float DHT::CalcdewPoint(float celsius, float humidity) {
-    float A0= 373.15/(273.15 + celsius);
-    float SUM = -7.90298 * (A0-1);
-    SUM += 5.02808 * log10(A0);
-    SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
-    SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
+    float A0= 373.15f/(273.15f + celsius);
+    float SUM = -7.90298f * (A0-1);
+    SUM += 5.02808f * log10(A0);
+    SUM += -1.3816e-7 * (pow(10, (11.344f*(1-1/A0)))-1) ;
+    SUM += 8.1328e-3f * (pow(10,(-3.49149f*(A0-1.0f)))-1) ;
     SUM += log10(1013.246);
     float VP = pow(10, SUM-3) * humidity;
-    float T = log(VP/0.61078);   // temp var
-    return (241.88 * T) / (17.558-T);
+    float T = log(VP/0.61078f);   // temp var
+    return (241.88f * T) / (17.558f-T);
 }
 
 // delta max = 0.6544 wrt dewPoint()
@@ -214,18 +223,18 @@
 }
 
 float DHT::CalcHumidity() {
-    int v;
+    float v;
 
     switch (_DHTtype) {
         case DHT11:
             v = DHT_data[0];
-            return float(v);
+            return v;
         case DHT22:
             v = DHT_data[0];
             v *= 256;
             v += DHT_data[1];
             v /= 10;
-            return float(v);
+            return v;
     }
     return 0;
 }