A library for the use of AM2303 (a.k.a. DHT22), a temperature and humidity sensor.

Dependents:   AM2303_Hello_World

Fork of DHT11 by Shigenori Inoue

Files at this revision

API Documentation at this revision

Comitter:
s_inoue_mbed
Date:
Thu Sep 11 13:45:00 2014 +0000
Parent:
8:160047ca45bf
Child:
10:f0d789f49df7
Commit message:
Bug fix

Changed in this revision

DHT11.cpp Show annotated file Show diff for this revision Revisions of this file
DHT11.h Show annotated file Show diff for this revision Revisions of this file
--- a/DHT11.cpp	Wed Sep 10 18:00:31 2014 +0000
+++ b/DHT11.cpp	Thu Sep 11 13:45:00 2014 +0000
@@ -11,72 +11,77 @@
     io_irq.rise(this, &DHT11::pos_edge);
     io_irq.fall(this, &DHT11::neg_edge);
     io_irq.disable_irq();
-    init();    
+    t.start();
     first_time = true;
 }
 
 // Destructor
 DHT11::~DHT11(void) {}
 
+// Constants
+const int DHT11::t_tol_start = 2;
+const int DHT11::t_tol_pulse = 10;
+
 // Reading the data bits from the DHT11
-int DHT11::readData()
+int DHT11::readData(void)
 {
+    // Checking the measurement frequency
+    if (t.read_ms() < 2000 && first_time == false) {
+        t.reset();
+        return READ_TOO_OFTEN;
+    }
+    
     // Initialize
     init();
 
-    // Checking the measurement frequency
-    if (t.read_ms() < 2000 & first_time == false) {
-        t.reset();
-        return TOO_FAST_READ;
-    }
-
     // Checking the data bus
     if (io == 0) {
-        io.input();
+        t.reset();
         return BUS_BUSY;
     }
 
-    // Sending start signal, low signal for 20 ms
-    io.output();
+    // Sending start signal, low signal for around 10 ms
     t.reset();
-    t.start();
     do {
         io = 0;
-    } while (t.read_ms() < 20);
+    } while (t.read_ms() < 20 + t_tol_start);
     io = 1;
-    io.input();
 
     // Waiting for the start of the response signal
     t.reset();
-    t.start();
     do {
         if (t.read_us() > 100) {
-            io.input();
+            t.reset();
             return NOT_PRESENT;
         }
     } while (io == 1);
 
     // Wainting for the start of the ready signal
     t.reset();
-    t.start();
     do {
         if (t.read_us() > 100) {
-            io.input();
+            t.reset();
             return NOT_READY;
         }
     } while (io == 0);
 
     // Wainting for the end of the ready signal
     t.reset();
-    t.start();
-    do {} while (io == 1);
+    do {
+        if (t.read_us() > 100) {
+            t.reset();
+            return WATCHDOG_ERR;
+        }
+    } while (io == 1);
 
     // Starting the pulse width sensing
+    // by the use of interruptions
     io_irq.enable_irq();
 
     do {
         wait_us(100);
         if (wdt > 50) {
+            t.reset();
             return WATCHDOG_ERR;
         }
         wdt++;
@@ -87,7 +92,9 @@
              + ((data & 0x00ff000000) >> 24)
              + ((data & 0x0000ff0000) >> 16)
              + ((data & 0x000000ff00) >> 8);
+
     if (chksum != (data & 0x00000000ff)) {
+        t.reset();
         return CHKSUM_ERR;
     } else {
         t.reset();
@@ -97,23 +104,15 @@
 }
 
 // Extracting humidity data from the received data
-int DHT11::readHumidity()
+int DHT11::readHumidity(void)
 {
-    if (err == OK) {
-        return (data & 0xff00000000) >> 32;
-    } else {
-        return 0xffffffff;
-    }
+    return (data & 0xff00000000) >> 32;
 }
 
 // Extracting temperature data from the received data
-int DHT11::readTemperature()
+int DHT11::readTemperature(void)
 {
-    if (err == OK) {
-        return (data & 0x0000ff0000) >> 16;
-    } else {
-        return 0xffffffff;
-    }
+    return (data & 0x0000ff0000) >> 16;
 }
 
 // Initialization of variables
@@ -124,8 +123,8 @@
     chksum = 0;
     cnt = 0;
     wdt = 0;
-    err = OK;
     eod = false;
+    t.reset();
 }
 
 void DHT11::pos_edge(void)
@@ -148,8 +147,8 @@
     // Reading the positive pulse width
     t_pulse_us = t.read_us();
 
-    // Detecting 0 if the pulse width ranges from 20 us to 30 us
-    if (20 <= t_pulse_us && t_pulse_us <= 30) {
+    // Detecting 0 if the pulse width ranges around 25 us
+    if (25 - t_tol_pulse <= t_pulse_us && t_pulse_us <= 30 + t_tol_pulse) {
         // Shifting the data buffer and not adding 1 (because this bit is zero)
         data = data << 1;
 
@@ -157,8 +156,8 @@
         cnt++;
     }
 
-    // Detecting 1 if the pulse width ranges from 60 us to 80 us
-    else if (60 <= t_pulse_us && t_pulse_us <= 80) {
+    // Detecting 1 if the pulse width ranges from 70 us
+    else if (70 - t_tol_pulse <= t_pulse_us && t_pulse_us <= 70 + t_tol_pulse) {
         // Shifting the data buffer and adding 1 (because this bit is one)
         data = data << 1;
         data++;
--- a/DHT11.h	Wed Sep 10 18:00:31 2014 +0000
+++ b/DHT11.h	Thu Sep 11 13:45:00 2014 +0000
@@ -28,7 +28,7 @@
  */
 
 class DHT11
-{
+{   
 public:
     /** Create a DHT11 interface
      * @param pin 1-wire-like serial I/O port of DHT11
@@ -49,20 +49,20 @@
     int readData(void);
 
     /** Reading the humidity from the data
-     * @return Humidity in % if readData() returns no error.
-     * Otherwise, returns 0xffffffff.
+     * @return Humidity in %,
+     * regardless of the error from readData()
      */
     int readHumidity(void);
 
     /** Reading the temperature from the data
-     * @return Temperature in Celcius if readData() returns no error.
-     * Otherwise, returns 0xffffffff.
+     * @return Temperature in Celcius,
+     * regardless of the error from readData()
      */
     int readTemperature(void);
 
     enum ErrorDHT11 {
         OK = 0,
-        TOO_FAST_READ = 1,
+        READ_TOO_OFTEN = 1,
         BUS_BUSY = 2,
         NOT_PRESENT = 3,
         NOT_READY = 4,
@@ -75,13 +75,14 @@
     InterruptIn io_irq;
     Timer t;
     uint32_t t_pulse_us;
+    const static int t_tol_start;    
+    const static int t_tol_pulse;
     bool first_time;
     uint64_t data;
     uint32_t chksum;
     uint32_t cnt;
     uint32_t wdt;
     bool eod;
-    ErrorDHT11 err;
     void init(void);
     void pos_edge(void);
     void neg_edge(void);