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:
6:257e2ab66d0f
Parent:
4:48798b126d93
Child:
7:50f5c8efd967
--- a/DHT11.cpp	Wed Sep 10 17:35:27 2014 +0000
+++ b/DHT11.cpp	Wed Sep 10 17:43:14 2014 +0000
@@ -11,21 +11,18 @@
     io_irq.rise(this, &DHT11::pos_edge);
     io_irq.fall(this, &DHT11::neg_edge);
     io_irq.disable_irq();
-    init();
+    init();    
+    first_time = true;
 }
 
 // Destructor
 DHT11::~DHT11(void) {}
 
-// Reading the data bits from DHT11
+// Reading the data bits from the DHT11
 int DHT11::readData()
 {
     // Initialize
-    eod = false;
-    err = OK;
-    data = 0;
-    cnt = 0;
-    wdt = 0;
+    init();
 
     // Checking the measurement frequency
     if (t.read_ms() < 2000 & first_time == false) {
@@ -128,7 +125,6 @@
 void DHT11::init(void)
 {
     t_pulse_us = 0;
-    first_time = true;
     data = 0;
     chksum = 0;
     cnt = 0;