realease DHT lib working with Nucleo Board Thanks Somlak Mangnimit

Fork of DHT by Wim De Roeve

Files at this revision

API Documentation at this revision

Comitter:
moisesmarangoni
Date:
Sat Apr 19 18:05:38 2014 +0000
Parent:
0:9b5b3200688f
Commit message:
First realease DHT working with Nucleo Board. Thanks (Somlak Mangnimit)

Changed in this revision

DHT.cpp Show annotated file Show diff for this revision Revisions of this file
DHT.h Show annotated file Show diff for this revision Revisions of this file
diff -r 9b5b3200688f -r 932d451474dc DHT.cpp
--- a/DHT.cpp	Mon Jul 09 19:47:43 2012 +0000
+++ b/DHT.cpp	Sat Apr 19 18:05:38 2014 +0000
@@ -1,233 +1,134 @@
 /*
  *  DHT Library for  Digital-output Humidity and Temperature sensors
  *
- *  Works with DHT11, DHT22
- *             SEN11301P,  Grove - Temperature&Humidity Sensor     (Seeed Studio)
- *             SEN51035P,  Grove - Temperature&Humidity Sensor Pro (Seeed Studio)
- *             AM2302   ,  temperature-humidity sensor
- *             HM2303   ,  Digital-output humidity and temperature sensor
+ *  Works with DHT11, DHT22 Nucleo Board tested on F103RB
  *
- *  Copyright (C) Wim De Roeve
+ *  Copyright (C) Wim De Roeve 
+ *                ported to work on Nucleo Board:
+ *                                              Moises Marangoni
+ *                                              Somlak Mangnimit
  *                based on DHT22 sensor library by HO WING KIT
  *                Arduino DHT11 library 
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documnetation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to  whom the Software is
- * furished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
  */
 
 #include "DHT.h"
+#include "mbed.h"
 
 #define DHT_DATA_BIT_COUNT 41
 
+//DigitalInOut data_pin(PA_10);
+
 DHT::DHT(PinName pin,int DHTtype) {
     _pin = pin;
     _DHTtype = DHTtype;
-    _firsttime=true;
+    
+
 }
 
 DHT::~DHT() {
 }
 
 int DHT::readData() {
-    int i, j, retryCount,b;
-    unsigned int bitTimes[DHT_DATA_BIT_COUNT];
+    Timer tmr;
+    DigitalInOut data_pin(_pin);
+    // BUFFER TO RECEIVE
+    //uint8_t bits[5];
+    uint8_t cnt = 7;
+    uint8_t idx = 0;
+    
+    tmr.stop();
+    tmr.reset();
 
-    eError err = ERROR_NONE;
-    time_t currentTime = time(NULL);
-
-    DigitalInOut DHT_io(_pin);
+    // EMPTY BUFFER
+    for(int i=0; i< 5; i++) bits[i] = 0;
 
-    for (i = 0; i < DHT_DATA_BIT_COUNT; i++) {
-        bitTimes[i] = 0;
-    }
+    // REQUEST SAMPLE
+    data_pin.output();
+    data_pin.write(0);
+    wait_ms(18);
+    data_pin.write(1);
+    wait_us(40);
+    data_pin.input();
 
-    if (!_firsttime) {
-        if (int(currentTime - _lastReadTime) < 2) {
-            err = ERROR_NO_PATIENCE;
-            return err;
-        }
-    } else {
-        _firsttime=false;
-        _lastReadTime=currentTime;
-    }
-    retryCount = 0;
+    // ACKNOWLEDGE or TIMEOUT
+    unsigned int loopCnt = 10000;
+    
+    while(!data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
 
-    do {
-        if (retryCount > 125) {
-            err = BUS_BUSY;
-            return err;
-        }
-        retryCount ++;
-        wait_us(2);
-    } while ((DHT_io==0));
+    loopCnt = 10000;
+    
+    while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
 
+    // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
+    for(int i=0; i<40; i++){
+        
+        loopCnt = 10000;
+        
+        while(!data_pin.read())if(loopCnt-- == 0)return DHTLIB_ERROR_TIMEOUT;
+
+        //unsigned long t = micros();
+        tmr.start();
 
-    DHT_io.output();
-    DHT_io = 0;
-    wait_ms(18);
-    DHT_io = 1;
-    wait_us(40);
-    DHT_io.input();
+        loopCnt = 10000;
+        
+        while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
 
-    retryCount = 0;
-    do {
-        if (retryCount > 40)  {
-            err = ERROR_NOT_PRESENT;
-            return err;
-        }
-        retryCount++;
-        wait_us(1);
-    } while ((DHT_io==1));
-
-    if (err != ERROR_NONE) {
-        return err;
+        if(tmr.read_us() > 40) bits[idx] |= (1 << cnt);
+        
+        tmr.stop();
+        tmr.reset();
+        
+        if(cnt == 0){   // next byte?
+        
+            cnt = 7;    // restart at MSB
+            idx++;      // next byte!
+            
+        }else cnt--;
+        
     }
-
-    wait_us(80);
-
-    for (i = 0; i < 5; i++) {
-        for (j = 0; j < 8; j++) {
+    // WRITE TO RIGHT VARS
+    // as bits[1] and bits[3] are allways zero they are omitted in formulas.
+    //humidity    = bits[0]; 
+    //temperature = bits[2]; 
 
-            retryCount = 0;
-            do {
-                if (retryCount > 75)  {
-                    err = ERROR_DATA_TIMEOUT;
-                    return err;
-                }
-                retryCount++;
-                wait_us(1);
-            } while (DHT_io == 0);
-            wait_us(40);
-            bitTimes[i*8+j]=DHT_io;
+    uint8_t sum = bits[0] + bits[2];  
 
-            int count = 0;
-            while (DHT_io == 1 && count < 100) {
-                wait_us(1);
-                count++;
-            }
-        }
-    }
-    DHT_io.output();
-    DHT_io = 1;
-    for (i = 0; i < 5; i++) {
-        b=0;
-        for (j=0; j<8; j++) {
-            if (bitTimes[i*8+j+1] > 0) {
-                b |= ( 1 << (7-j));
-            }
-        }
-        DHT_data[i]=b;
-    }
-
-    if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF)) {
-        _lastReadTime = currentTime;
-        _lastTemperature=CalcTemperature();
-        _lastHumidity=CalcHumidity();
-
-    } else {
-        err = ERROR_CHECKSUM;
-    }
-
-    return err;
-
+    if(bits[4] != sum)return DHTLIB_ERROR_CHECKSUM;
+    
+    return DHTLIB_OK;
 }
-
-float DHT::CalcTemperature() {
-    int v;
+float DHT::ReadTemperature() {
+    //int retornotemp;
 
     switch (_DHTtype) {
         case DHT11:
-            v = DHT_data[2];
-            return float(v);
+            temperature = bits[2];
+            return float(temperature);
         case DHT22:
-            v = DHT_data[2] & 0x7F;
-            v *= 256;
-            v += DHT_data[3];
-            v /= 10;
-            if (DHT_data[2] & 0x80)
-                v *= -1;
-            return float(v);
+            temperature = bits[2] & 0x7F;
+            temperature *= 256;
+            temperature += bits[3];
+            temperature /= 10;
+           if (bits[2] & 0x80)
+            {temperature *= -1;}
+            return float(temperature);
     }
     return 0;
 }
 
-float DHT::ReadHumidity() {
-    return _lastHumidity;
-}
-
-float DHT::ConvertCelciustoFarenheit(float celsius) {
-    return celsius * 9 / 5 + 32;
-}
-
-float DHT::ConvertCelciustoKelvin(float celsius) {
-    return celsius + 273.15;
-}
-
-// 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) ;
-    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);
-}
-
-// delta max = 0.6544 wrt dewPoint()
-// 5x faster than dewPoint()
-// reference: http://en.wikipedia.org/wiki/Dew_point
-float DHT::CalcdewPointFast(float celsius, float humidity)
-{
-        float a = 17.271;
-        float b = 237.7;
-        float temp = (a * celsius) / (b + celsius) + log(humidity/100);
-        float Td = (b * temp) / (a - temp);
-        return Td;
-}
-
-float DHT::ReadTemperature(eScale Scale) {
-    if (Scale == FARENHEIT)
-        return ConvertCelciustoFarenheit(_lastTemperature);
-    else if (Scale == KELVIN)
-        return ConvertCelciustoKelvin(_lastTemperature);
-    else
-        return _lastTemperature;
-}
-
-float DHT::CalcHumidity() {
-    int v;
+int DHT::ReadHumidity() {
+    //int v;
 
     switch (_DHTtype) {
         case DHT11:
-            v = DHT_data[0];
-            return float(v);
+            humidity = bits[0];
+            return float(humidity);
         case DHT22:
-            v = DHT_data[0];
-            v *= 256;
-            v += DHT_data[1];
-            v /= 10;
-            return float(v);
+            humidity = bits[0];
+            humidity *= 256;
+            humidity += bits[1];
+            humidity /= 10;
+            return float(humidity);
     }
     return 0;
-}
-
-
+}
\ No newline at end of file
diff -r 9b5b3200688f -r 932d451474dc DHT.h
--- a/DHT.h	Mon Jul 09 19:47:43 2012 +0000
+++ b/DHT.h	Sat Apr 19 18:05:38 2014 +0000
@@ -1,33 +1,14 @@
-/* 
- *  DHT Library for  Digital-output Humidity and Temperature sensors 
- *  
- *  Works with DHT11, DHT21, DHT22
- *             SEN11301P,  Grove - Temperature&Humidity Sensor     (Seeed Studio)
- *             SEN51035P,  Grove - Temperature&Humidity Sensor Pro (Seeed Studio)
- *             AM2302   ,  temperature-humidity sensor    
- *             RHT01,RHT02, RHT03    ,  Humidity and Temperature Sensor         (Sparkfun)
+/*
+ *  DHT Library for  Digital-output Humidity and Temperature sensors
  *
- *  Copyright (C) Wim De Roeve
- *                based on DHT22 sensor library by HO WING KIT
- *                Arduino DHT11 library
+ *  Works with DHT11, DHT22 Nucleo Board tested on F103RB
  *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documnetation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to  whom the Software is
- * furished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
+ *  Copyright (C) Wim De Roeve 
+ *                ported to work on Nucleo Board:
+ *                                                Moises Marangoni
+ *                                                Somlak Mangnimit
+ *                based on DHT22 sensor library by HO WING KIT
+ *                Arduino DHT11 library 
  */
 
 #ifndef MBED_DHT_H
@@ -37,58 +18,33 @@
 
 enum eType{
         DHT11     = 11,
-        SEN11301P = 11,
-        RHT01     = 11,
         DHT22     = 22,
-        AM2302    = 22,
-        SEN51035P = 22,
-        RHT02     = 22,
-        RHT03     = 22
     } ;
 
-enum eError {
-    ERROR_NONE = 0,
-    BUS_BUSY =1,
-    ERROR_NOT_PRESENT =2 ,
-    ERROR_ACK_TOO_LONG =3 ,
-    ERROR_SYNC_TIMEOUT = 4,
-    ERROR_DATA_TIMEOUT =5 ,
-    ERROR_CHECKSUM = 6,
-    ERROR_NO_PATIENCE =7
-} ;
-
-typedef enum {
-    CELCIUS =0 ,
-    FARENHEIT =1,
-    KELVIN=2
-} eScale;
-
-
 class DHT {
 
 public:
 
+#define DHTLIB_OK                0
+#define DHTLIB_ERROR_CHECKSUM   -1
+#define DHTLIB_ERROR_TIMEOUT    -2
+
     DHT(PinName pin,int DHTtype);
     ~DHT();
+    uint8_t bits[5];
     int readData(void);
-    float ReadHumidity(void);
-    float ReadTemperature(eScale Scale);
-    float CalcdewPoint(float celsius, float humidity);
-    float CalcdewPointFast(float celsius, float humidity);
+    int ReadHumidity(void);
+    float ReadTemperature(void);
+    int humidity;
+    float temperature;
+    Timer tmr;
 
 private:
-    time_t  _lastReadTime;
-    float _lastTemperature;
-    float _lastHumidity;
+    //time_t  _lastReadTime;
     PinName _pin;
-    bool _firsttime;
     int _DHTtype;
     int DHT_data[6];
-    float CalcTemperature();
-    float CalcHumidity();
-    float ConvertCelciustoFarenheit(float);
-    float ConvertCelciustoKelvin(float);
-
+    //DigitalInOut data_pin(_pin);
 };
 
 #endif