Simple library for the DHT11 temperature and humidity sensor. Forked from an existing Mbed DHT11 project.
Diff: DHT11.cpp
- Revision:
- 13:11d0770eb603
- Parent:
- 12:af1eadec17e5
- Child:
- 14:dc91e960af81
--- 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