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:
s_inoue_mbed
Date:
Wed Sep 10 17:43:14 2014 +0000
Parent:
5:da586c935e88
Child:
7:50f5c8efd967
Commit message:
Reorganized initialization function

Changed in this revision

DHT11.cpp Show annotated file Show diff for this revision Revisions of this file
--- 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;