A minimal library for the DHT11.

Dependencies:   mbed

Dependents:   EXP10_DHT11_LCD Sushil_MODSERIAL Core1000_SmartFarm idd_summer17_hw3_evey_jenny_seiyoung ... more

Files at this revision

API Documentation at this revision

Comitter:
fossum_13
Date:
Sun Feb 15 02:09:00 2015 +0000
Child:
1:5da6f6de3e42
Commit message:
Initial version

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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Dht11.cpp	Sun Feb 15 02:09:00 2015 +0000
@@ -0,0 +1,86 @@
+#include "Dht11.h"
+
+Dht11::Dht11(PinName p) : _pin(p) {
+    // Set creation time so we can make 
+    // sure we pause at least 1 second for 
+    // startup.
+    _timer.start();
+}
+
+// Return values:
+// DHTLIB_OK
+// DHTLIB_ERROR_CHECKSUM
+// DHTLIB_ERROR_TIMEOUT
+int Dht11::read()
+{
+    // BUFFER TO RECEIVE
+    uint8_t bits[5];
+    uint8_t cnt = 7;
+    uint8_t idx = 0;
+
+    // EMPTY BUFFER
+    for (int i=0; i< 5; i++) bits[i] = 0;
+    
+    // Verify sensor settled after boot
+    while(_timer.read_ms() < 1500) {}
+    _timer.stop();
+
+    // Notify it we are ready to read
+    _pin.output();
+    _pin = 0;
+    wait_ms(18);
+    _pin = 1;
+    wait_us(40);
+    _pin.input();
+
+    // ACKNOWLEDGE or TIMEOUT
+    unsigned int loopCnt = 10000;
+    while(_pin == 0)
+        if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;
+
+    loopCnt = 10000;
+    while(_pin == 1)
+        if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;
+
+    // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
+    for (int i=0; i<40; i++)
+    {
+        loopCnt = 10000;
+        while(_pin == 0)
+            if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;
+
+        //unsigned long t = micros();
+        Timer t;
+        t. start();
+
+        loopCnt = 10000;
+        while(_pin == 1)
+            if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;
+
+        if (t.read_us() > 40) bits[idx] |= (1 << cnt);
+        if (cnt == 0)   // next byte?
+        {
+            cnt = 7;    // restart at MSB
+            idx++;      // next byte!
+        }
+        else cnt--;
+    }
+
+    // WRITE TO RIGHT VARS
+    // as bits[1] and bits[3] are allways zero they are omitted in formulas.
+    _humidity    = bits[0]; 
+    _temperature = bits[2]; 
+
+    uint8_t sum = bits[0] + bits[2];  
+
+    if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
+    return DHTLIB_OK;
+}
+
+int Dht11::temperature() {
+    return(_temperature);
+}
+
+int Dht11::humidity() {
+    return(_humidity);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Dht11.h	Sun Feb 15 02:09:00 2015 +0000
@@ -0,0 +1,24 @@
+#ifndef DHT11_H
+#define DHT11_H
+
+#include "mbed.h"
+
+#define DHTLIB_OK                0
+#define DHTLIB_ERROR_CHECKSUM   -1
+#define DHTLIB_ERROR_TIMEOUT    -2
+
+class Dht11 {
+public:
+    Dht11(PinName p);
+    int read();
+    int temperature();
+    int humidity();
+    
+private:
+    int _humidity;
+    int _temperature;
+    DigitalInOut _pin;
+    Timer _timer;
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Feb 15 02:09:00 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e188a91d3eaa
\ No newline at end of file