A minimal library for the DHT11.

Fork of DHT11 by Eric Fossum

Files at this revision

API Documentation at this revision

Comitter:
amithy
Date:
Thu Nov 09 22:14:56 2017 +0000
Parent:
1:5da6f6de3e42
Commit message:
test export

Changed in this revision

Dht11.cpp Show diff for this revision Revisions of this file
Dht11.h Show diff for this revision Revisions of this file
Dht22.cpp Show annotated file Show diff for this revision Revisions of this file
Dht22.h Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show diff for this revision Revisions of this file
diff -r 5da6f6de3e42 -r 983fdd709bf7 Dht11.cpp
--- a/Dht11.cpp	Mon Feb 16 01:43:08 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,89 +0,0 @@
-#include "Dht11.h"
-
-Dht11::Dht11(PinName const &p) : _pin(p) {
-    // Set creation time so we can make 
-    // sure we pause at least 1 second for 
-    // startup.
-    _timer.start();
-    
-    _temperature = 0;
-    _humidity = 0;
-}
-
-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;
-}
-
-float Dht11::getFahrenheit() {
-    return((_temperature * 1.8) + 32);
-}
-
-int Dht11::getCelsius() {
-    return(_temperature);
-}
-
-int Dht11::getHumidity() {
-    return(_humidity);
-}
diff -r 5da6f6de3e42 -r 983fdd709bf7 Dht11.h
--- a/Dht11.h	Mon Feb 16 01:43:08 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,74 +0,0 @@
-#ifndef DHT11_H
-#define DHT11_H
-
-#include "mbed.h"
-
-#define DHTLIB_OK                0
-#define DHTLIB_ERROR_CHECKSUM   -1
-#define DHTLIB_ERROR_TIMEOUT    -2
-
-/** Class for the DHT11 sensor.
- * 
- * Example:
- * @code
- * #include "mbed.h"
- * #include "Dht11.h"
- *
- * Serial pc(USBTX, USBRX);
- * Dht11 sensor(PTD7);
- * 
- * int main() {
- *     sensor.read()
- *     pc.printf("T: %f, H: %d\r\n", sensor.getFahrenheit(), sensor.getHumidity());
- * }
- * @endcode
- */
-class Dht11
-{
-public:
-    /** Construct the sensor object.
-     *
-     * @param pin PinName for the sensor pin.
-     */
-    Dht11(PinName const &p);
-    
-    /** Update the humidity and temp from the sensor.
-     *
-     * @returns
-     *   0 on success, otherwise error.
-     */
-    int read();
-    
-    /** Get the temp(f) from the saved object.
-     *
-     * @returns
-     *   Fahrenheit float
-     */
-    float getFahrenheit();
-    
-    /** Get the temp(c) from the saved object.
-     *
-     * @returns
-     *   Celsius int
-     */
-    int getCelsius();
-    
-    /** Get the humidity from the saved object.
-     *
-     * @returns
-     *   Humidity percent int
-     */
-    int getHumidity();
-
-private:
-    /// percentage of humidity
-    int _humidity;
-    /// celsius
-    int _temperature;
-    /// pin to read the sensor info on
-    DigitalInOut _pin;
-    /// times startup (must settle for at least a second)
-    Timer _timer;
-};
-
-#endif
diff -r 5da6f6de3e42 -r 983fdd709bf7 Dht22.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Dht22.cpp	Thu Nov 09 22:14:56 2017 +0000
@@ -0,0 +1,104 @@
+#include "Dht22.h"
+
+Dht22::Dht22(PinName const &p) : _pin(p) {
+    // Set creation time so we can make 
+    // sure we pause at least 1 second for 
+    // startup.
+    _timer.start();
+    
+    _temperature = 0;
+    _humidity = 0;
+}
+
+int Dht22::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 - DHT22
+//    // 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;
+    
+    
+    uint8_t dht22_check_sum;
+    dht22_check_sum = bits[0] + bits[1] + bits[2] + bits[3];
+    //printf("dht22 check sum: %d",dht22_check_sum);
+    dht22_check_sum= dht22_check_sum%256;
+    //printf("dht22 check sum: %d",dht22_check_sum);
+    
+    if (dht22_check_sum == bits[4]) {
+        _humidity = bits[0]*256 + bits[1];
+        
+        _temperature = bits[2]*256 + bits[3];
+        //printf("all good \n");
+        return DHTLIB_OK;
+    }
+    return DHTLIB_ERROR_CHECKSUM;
+}
+
+float Dht22::getFahrenheit() {
+    return((_temperature * 1.8) + 32);
+}
+
+int Dht22::getCelsius() {
+    return(_temperature);
+}
+
+int Dht22::getHumidity() {
+    return(_humidity);
+}
diff -r 5da6f6de3e42 -r 983fdd709bf7 Dht22.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Dht22.h	Thu Nov 09 22:14:56 2017 +0000
@@ -0,0 +1,74 @@
+#ifndef DHT22_H
+#define DHT22_H
+
+#include "mbed.h"
+
+#define DHTLIB_OK                0
+#define DHTLIB_ERROR_CHECKSUM   -1
+#define DHTLIB_ERROR_TIMEOUT    -2
+
+/** Class for the DHT22 sensor.
+ * 
+ * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "Dht22.h"
+ *
+ * Serial pc(USBTX, USBRX);
+ * Dht22 sensor(PTD7);
+ * 
+ * int main() {
+ *     sensor.read()
+ *     pc.printf("T: %f, H: %d\r\n", sensor.getFahrenheit(), sensor.getHumidity());
+ * }
+ * @endcode
+ */
+class Dht22
+{
+public:
+    /** Construct the sensor object.
+     *
+     * @param pin PinName for the sensor pin.
+     */
+    Dht22(PinName const &p);
+    
+    /** Update the humidity and temp from the sensor.
+     *
+     * @returns
+     *   0 on success, otherwise error.
+     */
+    int read();
+    
+    /** Get the temp(f) from the saved object.
+     *
+     * @returns
+     *   Fahrenheit float
+     */
+    float getFahrenheit();
+    
+    /** Get the temp(c) from the saved object.
+     *
+     * @returns
+     *   Celsius int
+     */
+    int getCelsius();
+    
+    /** Get the humidity from the saved object.
+     *
+     * @returns
+     *   Humidity percent int
+     */
+    int getHumidity();
+
+private:
+    /// percentage of humidity
+    int _humidity;
+    /// celsius
+    int _temperature;
+    /// pin to read the sensor info on
+    DigitalInOut _pin;
+    /// times startup (must settle for at least a second)
+    Timer _timer;
+};
+
+#endif
diff -r 5da6f6de3e42 -r 983fdd709bf7 mbed.bld
--- a/mbed.bld	Mon Feb 16 01:43:08 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/e188a91d3eaa
\ No newline at end of file