DHT testprogram

Dependencies:   mbed

Fork of DHT_TestProgram by Wim De Roeve

Files at this revision

API Documentation at this revision

Comitter:
zainzafar
Date:
Tue Dec 29 10:55:05 2015 +0000
Parent:
0:9ef709bfeb08
Commit message:
by Zain Zafar;

Changed in this revision

DHT/DHT.cpp Show annotated file Show diff for this revision Revisions of this file
DHT/DHT.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 9ef709bfeb08 -r 731de3b11b74 DHT/DHT.cpp
--- a/DHT/DHT.cpp	Tue Jul 10 13:11:23 2012 +0000
+++ b/DHT/DHT.cpp	Tue Dec 29 10:55:05 2015 +0000
@@ -9,7 +9,7 @@
  *
  *  Copyright (C) Wim De Roeve
  *                based on DHT22 sensor library by HO WING KIT
- *                Arduino DHT11 library 
+ *                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
@@ -29,127 +29,115 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
-
+ 
 #include "DHT.h"
-
-#define DHT_DATA_BIT_COUNT 41
-
-DHT::DHT(PinName pin,int DHTtype) {
+ 
+#define DHT_DATA_BIT_COUNT 40
+ 
+DHT::DHT(PinName pin, eType DHTtype)
+{
     _pin = pin;
     _DHTtype = DHTtype;
-    _firsttime=true;
+    _firsttime = true;
 }
-
-DHT::~DHT() {
+ 
+DHT::~DHT()
+{
+    
 }
-
-int DHT::readData() {
-    int i, j, retryCount,b;
-    unsigned int bitTimes[DHT_DATA_BIT_COUNT];
-
+ 
+eError DHT::stall(DigitalInOut &io, int const level, int const max_time)
+{
+    int cnt = 0;
+    while (level == io) {
+        if (cnt > max_time) {
+            return ERROR_NO_PATIENCE;
+        }
+        cnt++;
+        wait_us(1);
+    }
+    return ERROR_NONE;
+}
+ 
+eError DHT::readData()
+{
+    uint8_t i = 0, j = 0, b = 0, data_valid = 0;
+    uint32_t bit_value[DHT_DATA_BIT_COUNT] = {0};
+ 
     eError err = ERROR_NONE;
     time_t currentTime = time(NULL);
-
+ 
     DigitalInOut DHT_io(_pin);
-
-    for (i = 0; i < DHT_DATA_BIT_COUNT; i++) {
-        bitTimes[i] = 0;
+ 
+    // IO must be in hi state to start
+    if (ERROR_NONE != stall(DHT_io, 0, 250)) {
+        return BUS_BUSY;
     }
-
-    if (!_firsttime) {
-        if (int(currentTime - _lastReadTime) < 2) {
-            err = ERROR_NO_PATIENCE;
-            return err;
-        }
-    } else {
-        _firsttime=false;
-        _lastReadTime=currentTime;
-    }
-    retryCount = 0;
-
-    do {
-        if (retryCount > 125) {
-            err = BUS_BUSY;
-            return err;
-        }
-        retryCount ++;
-        wait_us(2);
-    } while ((DHT_io==0));
-
-
+ 
+    // start the transfer
     DHT_io.output();
     DHT_io = 0;
-    wait_ms(18);
+    // only 500uS for DHT22 but 18ms for DHT11
+    (_DHTtype == 11) ? wait_ms(18) : wait(1);
     DHT_io = 1;
-    wait_us(40);
+    wait_us(30);
     DHT_io.input();
-
-    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;
+    // wait till the sensor grabs the bus
+    if (ERROR_NONE != stall(DHT_io, 1, 40)) {
+        return ERROR_NOT_PRESENT;
     }
-
-    wait_us(80);
-
+    // sensor should signal low 80us and then hi 80us
+    if (ERROR_NONE != stall(DHT_io, 0, 100)) {
+        return ERROR_SYNC_TIMEOUT;
+    }
+    if (ERROR_NONE != stall(DHT_io, 1, 100)) {
+        return ERROR_NO_PATIENCE;
+    }
+    // capture the data
     for (i = 0; i < 5; i++) {
         for (j = 0; j < 8; j++) {
-
-            retryCount = 0;
-            do {
-                if (retryCount > 75)  {
-                    err = ERROR_DATA_TIMEOUT;
-                    return err;
-                }
-                retryCount++;
-                wait_us(1);
-            } while (DHT_io == 0);
+            if (ERROR_NONE != stall(DHT_io, 0, 75)) {
+                return ERROR_DATA_TIMEOUT;
+            }
+            // logic 0 is 28us max, 1 is 70us
             wait_us(40);
-            bitTimes[i*8+j]=DHT_io;
-
-            int count = 0;
-            while (DHT_io == 1 && count < 100) {
-                wait_us(1);
-                count++;
+            bit_value[i*8+j] = DHT_io;
+            if (ERROR_NONE != stall(DHT_io, 1, 50)) {
+                return ERROR_DATA_TIMEOUT;
             }
         }
     }
-    DHT_io.output();
-    DHT_io = 1;
+    // store the data
     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));
+            if (bit_value[i*8+j] == 1) {
+                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)) {
+ 
+    // uncomment to see the checksum error if it exists
+    //printf(" 0x%02x + 0x%02x + 0x%02x + 0x%02x = 0x%02x \n", DHT_data[0], DHT_data[1], DHT_data[2], DHT_data[3], DHT_data[4]);
+    data_valid = DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3];
+    if (DHT_data[4] == data_valid) {
         _lastReadTime = currentTime;
-        _lastTemperature=CalcTemperature();
-        _lastHumidity=CalcHumidity();
-
+        _lastTemperature = CalcTemperature();
+        _lastHumidity = CalcHumidity();
+ 
     } else {
         err = ERROR_CHECKSUM;
     }
-
+ 
     return err;
-
+ 
 }
-
-float DHT::CalcTemperature() {
+ 
+float DHT::CalcTemperature()
+{
     int v;
-
+ 
     switch (_DHTtype) {
         case DHT11:
             v = DHT_data[2];
@@ -165,22 +153,26 @@
     }
     return 0;
 }
-
-float DHT::ReadHumidity() {
+ 
+float DHT::ReadHumidity()
+{
     return _lastHumidity;
 }
-
-float DHT::ConvertCelciustoFarenheit(float celsius) {
+ 
+float DHT::ConvertCelciustoFarenheit(float const celsius)
+{
     return celsius * 9 / 5 + 32;
 }
-
-float DHT::ConvertCelciustoKelvin(float celsius) {
+ 
+float DHT::ConvertCelciustoKelvin(float const celsius)
+{
     return celsius + 273.15;
 }
-
+ 
 // dewPoint function NOAA
 // reference: http://wahiduddin.net/calc/density_algorithms.htm
-float DHT::CalcdewPoint(float celsius, float humidity) {
+float DHT::CalcdewPoint(float const celsius, float const humidity)
+{
     float A0= 373.15/(273.15 + celsius);
     float SUM = -7.90298 * (A0-1);
     SUM += 5.02808 * log10(A0);
@@ -191,20 +183,21 @@
     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 DHT::CalcdewPointFast(float const celsius, float const 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 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) {
+ 
+float DHT::ReadTemperature(eScale Scale)
+{
     if (Scale == FARENHEIT)
         return ConvertCelciustoFarenheit(_lastTemperature);
     else if (Scale == KELVIN)
@@ -212,10 +205,11 @@
     else
         return _lastTemperature;
 }
-
-float DHT::CalcHumidity() {
+ 
+float DHT::CalcHumidity()
+{
     int v;
-
+ 
     switch (_DHTtype) {
         case DHT11:
             v = DHT_data[0];
@@ -228,6 +222,4 @@
             return float(v);
     }
     return 0;
-}
-
-
+}
\ No newline at end of file
diff -r 9ef709bfeb08 -r 731de3b11b74 DHT/DHT.h
--- a/DHT/DHT.h	Tue Jul 10 13:11:23 2012 +0000
+++ b/DHT/DHT.h	Tue Dec 29 10:55:05 2015 +0000
@@ -1,10 +1,10 @@
-/* 
- *  DHT Library for  Digital-output Humidity and Temperature sensors 
- *  
+/*
+ *  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    
+ *             AM2302   ,  temperature-humidity sensor
  *             RHT01,RHT02, RHT03    ,  Humidity and Temperature Sensor         (Sparkfun)
  *
  *  Copyright (C) Wim De Roeve
@@ -29,66 +29,71 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
-
+ 
 #ifndef MBED_DHT_H
 #define MBED_DHT_H
-
+ 
 #include "mbed.h"
-
-enum eType{
-        DHT11     = 11,
-        SEN11301P = 11,
-        RHT01     = 11,
-        DHT22     = 22,
-        AM2302    = 22,
-        SEN51035P = 22,
-        RHT02     = 22,
-        RHT03     = 22
-    } ;
-
+ 
+typedef enum eType eType;
+enum eType {
+    DHT11     = 11,
+    SEN11301P = 11,
+    RHT01     = 11,
+    DHT22     = 22,
+    AM2302    = 22,
+    SEN51035P = 22,
+    RHT02     = 22,
+    RHT03     = 22
+};
+ 
+typedef enum eError eError;
 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 {
-
+    BUS_BUSY,
+    ERROR_NOT_PRESENT,
+    ERROR_ACK_TOO_LONG,
+    ERROR_SYNC_TIMEOUT,
+    ERROR_DATA_TIMEOUT,
+    ERROR_CHECKSUM,
+    ERROR_NO_PATIENCE
+};
+ 
+typedef enum eScale eScale;
+enum eScale {
+    CELCIUS = 0,
+    FARENHEIT,
+    KELVIN
+};
+ 
+ 
+class DHT
+{
+ 
 public:
-
-    DHT(PinName pin,int DHTtype);
+ 
+    DHT(PinName pin, eType DHTtype);
     ~DHT();
-    int readData(void);
+    eError readData(void);
     float ReadHumidity(void);
-    float ReadTemperature(eScale Scale);
-    float CalcdewPoint(float celsius, float humidity);
-    float CalcdewPointFast(float celsius, float humidity);
-
+    float ReadTemperature(eScale const Scale);
+    float CalcdewPoint(float const celsius, float const humidity);
+    float CalcdewPointFast(float const celsius, float const humidity);
+ 
 private:
     time_t  _lastReadTime;
     float _lastTemperature;
     float _lastHumidity;
     PinName _pin;
     bool _firsttime;
-    int _DHTtype;
-    int DHT_data[6];
+    eType _DHTtype;
+    uint8_t DHT_data[5];
     float CalcTemperature();
     float CalcHumidity();
-    float ConvertCelciustoFarenheit(float);
-    float ConvertCelciustoKelvin(float);
-
+    float ConvertCelciustoFarenheit(float const);
+    float ConvertCelciustoKelvin(float const);
+    eError stall(DigitalInOut &io, int const level, int const max_time);
+ 
 };
-
-#endif
+ 
+#endif
\ No newline at end of file
diff -r 9ef709bfeb08 -r 731de3b11b74 main.cpp
--- a/main.cpp	Tue Jul 10 13:11:23 2012 +0000
+++ b/main.cpp	Tue Dec 29 10:55:05 2015 +0000
@@ -2,26 +2,40 @@
 #include "DHT.h"
 
 DigitalOut myled(LED1);
+DigitalInOut data_pin(A0); // Activate digital in
+DigitalInOut data_pin(A1); // Activate digital in
+Serial pc(SERIAL_TX, SERIAL_RX); // Initialize UART connection
+Serial blutooth(PA_9, PA_10);//Tx, Rx
+Timer tmr; //initialize timer
+uint64_t adat; // 64 bit variable for temporary data
+int i;
 
-DHT sensor(p23,SEN11301P); // Use the SEN11301P sensor
+// Use a terminal program (eg. TeraTerm).
+//Timer tmr; //initialize timer
+
+
+DHT sensor(A0,SEN11301P); // Use the SEN11301P sensor
 
 int main() {
     int err;
-    printf("\r\nDHT Test program");
-    printf("\r\n******************\r\n");
+    pc.printf("\r\nDHT Test program");
+    pc.printf("\r\n******************\r\n");
     wait(1); // wait 1 second for device stable status
     while (1) {
         myled = 1;
+        wait(2);
         err = sensor.readData();
         if (err == 0) {
-            printf("Temperature is %4.2f C \r\n",sensor.ReadTemperature(CELCIUS));
-            printf("Temperature is %4.2f F \r\n",sensor.ReadTemperature(FARENHEIT));
-            printf("Temperature is %4.2f K \r\n",sensor.ReadTemperature(KELVIN));
-            printf("Humidity is %4.2f \r\n",sensor.ReadHumidity());
-            printf("Dew point is %4.2f  \r\n",sensor.CalcdewPoint(sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity()));
-            printf("Dew point (fast) is %4.2f  \r\n",sensor.CalcdewPointFast(sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity()));
+            
+            pc.printf("Temperature is %4.2f C \r\n",sensor.ReadTemperature(CELCIUS));
+            pc.printf("Temperature is %4.2f F \r\n",sensor.ReadTemperature(FARENHEIT));
+            pc.printf("Temperature is %4.2f K \r\n",sensor.ReadTemperature(KELVIN));
+            pc.printf("Humidity is %4.2f \r\n",sensor.ReadHumidity());
+            pc.printf("Dew point is %4.2f \r\n",sensor.CalcdewPoint(sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity()));
+            pc.printf("Dew point (fast) is %4.2f  \r\n",sensor.CalcdewPointFast(sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity()));
+            pc.printf("\n\n\n");
         } else
-            printf("\r\nErr %i \n",err);
+            pc.printf("\r\nErr %i \n",err);
         myled = 0;
         wait(5);
     }
diff -r 9ef709bfeb08 -r 731de3b11b74 mbed.bld
--- a/mbed.bld	Tue Jul 10 13:11:23 2012 +0000
+++ b/mbed.bld	Tue Dec 29 10:55:05 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/737756e0b479
+http://mbed.org/users/mbed_official/code/mbed/builds/4336505e4b1c
\ No newline at end of file