Simple library for the DHT11 temperature and humidity sensor. Forked from an existing Mbed DHT11 project.

Dependents:   UoY-DHT11-test

Simple DHT11 temperature and humidity library.

Example usage

#include "mbed.h"
#include "DHT11.h"

DHT11 dht(D8); // Change pin name here if required

main()
{
    printf("T:%d, H:%d\r\n", dht.readTemperature(), dht.readHumidity());
}

The sensor may be read as often as desired, but temperature and humidity values are cached and will only be updated if they are more than 2 seconds old. This is the underlying sensor update rate.

Please note that this project has been modified only enough to make it work for its intended purpose. Various parts of this project still need work, and the source code should not be seen as an example of best practice.

Files at this revision

API Documentation at this revision

Comitter:
ajp109
Date:
Wed Sep 15 14:47:06 2021 +0000
Parent:
12:af1eadec17e5
Commit message:
Interface changes to remove the need for separate readData() calls

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	Mon May 31 07:02:00 2021 +0000
+++ b/DHT11.cpp	Wed Sep 15 14:47:06 2021 +0000
@@ -1,4 +1,5 @@
 /* Copyright (c) 2014 Shigenori Inoue, MIT License
+ * Modified by Andy Pomfret 2021
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
  * and associated documentation files (the "Software"), to deal in the Software without restriction, 
@@ -25,7 +26,7 @@
     io_irq.fall(callback(this, &DHT11::neg_edge));
     io_irq.disable_irq();
     t.start();
-    first_time = true;
+    readNewData();
 }
 
 // Destructor
@@ -39,10 +40,10 @@
 int DHT11::readData(void)
 {
     // Checking the measurement frequency
-    if (chrono::duration_cast<chrono::milliseconds>(t.elapsed_time()).count() < 2000 && first_time == false) {
-        t.reset();
-        return READ_TOO_OFTEN;
-    }
+//    if (chrono::duration_cast<chrono::milliseconds>(t.elapsed_time()).count() < 2000) {
+//        t.reset();
+//        return READ_TOO_OFTEN;
+//    }
     
     // Initialize
     init();
@@ -57,8 +58,7 @@
     t.reset();
     io.output();
     io = 0;
-    do {
-    } while (chrono::duration_cast<chrono::milliseconds>(t.elapsed_time()).count() < 20 + t_tol_start);
+    thread_sleep_for(10 + t_tol_start);
     io.input();
     io = 1;
     
@@ -72,7 +72,7 @@
         }
     } while (io == 1);
 
-    // Wainting for the start of the ready signal
+    // Waiting for the start of the ready signal
     t.reset();
     do {
         if (t.elapsed_time().count() > 100) {
@@ -81,7 +81,7 @@
         }
     } while (io == 0);
 
-    // Wainting for the end of the ready signal
+    // Waiting for the end of the ready signal
     t.reset();
     do {
         if (t.elapsed_time().count() > 100) {
@@ -91,7 +91,7 @@
     } while (io == 1);
 
     // Starting the pulse width sensing
-    // by the use of interruptions
+    // by the use of interrupts
     io_irq.enable_irq();
 
     do {
@@ -104,30 +104,39 @@
     } while (eod == false);
 
     // Calculating the check sum
-    chksum = ((data & 0xff00000000) >> 32)
-             + ((data & 0x00ff000000) >> 24)
-             + ((data & 0x0000ff0000) >> 16)
-             + ((data & 0x000000ff00) >> 8);
+    chksum = (data >> 32)
+             + (data >> 24)
+             + (data >> 16)
+             + (data >> 8);
 
-    if (chksum != (data & 0x00000000ff)) {
+    if ((chksum & 0xff) != (data & 0x00000000ff)) {
         t.reset();
         return CHKSUM_ERR;
     } else {
         t.reset();
-        first_time = false;
         return OK;
     }
 }
 
+void DHT11::readNewData(void) {
+    do; while (readData() != OK);
+}
+
 // Extracting humidity data from the received data
 int DHT11::readHumidity(void)
 {
+    if (t.elapsed_time() >= 2000ms) {
+        readNewData();
+    }
     return (data & 0xff00000000) >> 32;
 }
 
 // Extracting temperature data from the received data
 int DHT11::readTemperature(void)
 {
+    if (t.elapsed_time() >= 2000ms) {
+        readNewData();
+    }
     return (data & 0x0000ff0000) >> 16;
 }
 
@@ -145,19 +154,19 @@
 
 void DHT11::pos_edge(void)
 {
-    // Disabling the interruptions
+    // Disabling the interrupts
     io_irq.disable_irq();
 
     // Initializing the Timer
     t.reset();
 
-    // Enabling the interruptions
+    // Enabling the interrupts
     io_irq.enable_irq();
 }
 
 void DHT11::neg_edge(void)
 {
-    // Disabling the interruptions
+    // Disabling the interrupts
     io_irq.disable_irq();
 
     // Reading the positive pulse width
--- a/DHT11.h	Mon May 31 07:02:00 2021 +0000
+++ b/DHT11.h	Wed Sep 15 14:47:06 2021 +0000
@@ -29,14 +29,7 @@
  *
  * main()
  * {
- *     int s;
- *     s = d.readData();
- *     if (s != DHT11::OK) {
- *         printf("Error!\r\n");
- *     }
- *     else {
- *         printf("T:%d, H:%d\r\n", d.readTemperature(), d.readHumidity());
- *     }
+ *     printf("T:%d, H:%d\r\n", d.readTemperature(), d.readHumidity());
  * }
  * @endcode
  */
@@ -50,7 +43,8 @@
     DHT11(PinName pin);
     ~DHT11();
 
-    /** Reading the data from the DHT11
+    /** Reading the data from the DHT11.  Calling this is not required, but
+     * may be useful for debug.
      * @return Error code
      *     0: OK.
      *     1: Reading the data too often.
@@ -62,15 +56,17 @@
      */
     int readData(void);
 
-    /** Reading the humidity from the data
-     * @return Humidity in %,
-     * regardless of the error from readData()
+    /** Returns the current humidity from the DHT11.  If the existing data record
+     * is older than 2000ms, initiates a read from the device.  Note that errors
+     * may result in infinite attempts to read.
+     * @return Humidity in %
      */
     int readHumidity(void);
 
-    /** Reading the temperature from the data
-     * @return Temperature in Celcius,
-     * regardless of the error from readData()
+    /** Returns the current temperature from the DHT11.  If the existing data record
+     * is older than 2000ms, initiates a read from the device.  Note that errors
+     * may result in infinite attempts to read.
+     * @return Temperature in Celcius
      */
     int readTemperature(void);
 
@@ -91,7 +87,6 @@
     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;
@@ -100,6 +95,7 @@
     void init(void);
     void pos_edge(void);
     void neg_edge(void);
+    void readNewData(void);
 };
 
 #endif
\ No newline at end of file