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.

Revision:
9:056d1e9b428c
Parent:
5:da586c935e88
Child:
10:f0d789f49df7
diff -r 160047ca45bf -r 056d1e9b428c DHT11.h
--- 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);