Simple library for the DHT11 temperature and humidity sensor. Forked from an existing Mbed DHT11 project.
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 12:af1eadec17e5, committed 2021-05-31
- Comitter:
- JohnnyK
- Date:
- Mon May 31 07:02:00 2021 +0000
- Parent:
- 11:e91c151d1798
- Child:
- 13:11d0770eb603
- Commit message:
- Update to MbedOS6+
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 Thu Sep 25 14:13:10 2014 +0000 +++ b/DHT11.cpp Mon May 31 07:02:00 2021 +0000 @@ -21,8 +21,8 @@ // Constructor DHT11::DHT11(PinName pin) : io(pin, PIN_INPUT, OpenDrain, 1), io_irq(pin) { - io_irq.rise(this, &DHT11::pos_edge); - io_irq.fall(this, &DHT11::neg_edge); + io_irq.rise(callback(this, &DHT11::pos_edge)); + io_irq.fall(callback(this, &DHT11::neg_edge)); io_irq.disable_irq(); t.start(); first_time = true; @@ -39,7 +39,7 @@ int DHT11::readData(void) { // Checking the measurement frequency - if (t.read_ms() < 2000 && first_time == false) { + if (chrono::duration_cast<chrono::milliseconds>(t.elapsed_time()).count() < 2000 && first_time == false) { t.reset(); return READ_TOO_OFTEN; } @@ -58,7 +58,7 @@ io.output(); io = 0; do { - } while (t.read_ms() < 20 + t_tol_start); + } while (chrono::duration_cast<chrono::milliseconds>(t.elapsed_time()).count() < 20 + t_tol_start); io.input(); io = 1; @@ -66,7 +66,7 @@ // Waiting for the start of the response signal t.reset(); do { - if (t.read_us() > 100) { + if (t.elapsed_time().count() > 100) { t.reset(); return NOT_PRESENT; } @@ -75,7 +75,7 @@ // Wainting for the start of the ready signal t.reset(); do { - if (t.read_us() > 100) { + if (t.elapsed_time().count() > 100) { t.reset(); return NOT_READY; } @@ -84,7 +84,7 @@ // Wainting for the end of the ready signal t.reset(); do { - if (t.read_us() > 100) { + if (t.elapsed_time().count() > 100) { t.reset(); return WATCHDOG_ERR; } @@ -161,7 +161,7 @@ io_irq.disable_irq(); // Reading the positive pulse width - t_pulse_us = t.read_us(); + t_pulse_us = t.elapsed_time().count(); // Detecting 0 if the pulse width ranges around 25 us if (25 - t_tol_pulse <= t_pulse_us && t_pulse_us <= 30 + t_tol_pulse) {
--- a/DHT11.h Thu Sep 25 14:13:10 2014 +0000 +++ b/DHT11.h Mon May 31 07:02:00 2021 +0000 @@ -25,7 +25,7 @@ * #include "mbed.h" * #include "DHT11.h" * - * DHT11 d; + * DHT11 d(D8); // Here fill your PIN mask/name * * main() * {