Skip to content

Commit 04905bc

Browse files
Add force parameter to read methods
This allows forcing a read, even if the previous read was less than 2 seconds ago. This is useful in cases where the millis() timer is not reliable, such as when sleeping. In this case, it is up to the caller to ensure that at least 2 seconds elapse between calls with force set to true.
1 parent 5cd78ae commit 04905bc

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

DHT.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ void DHT::begin(void) {
2727
}
2828

2929
//boolean S == Scale. True == Fahrenheit; False == Celcius
30-
float DHT::readTemperature(bool S) {
30+
float DHT::readTemperature(bool S, bool force) {
3131
float f = NAN;
3232

33-
if (read()) {
33+
if (read(force)) {
3434
switch (_type) {
3535
case DHT11:
3636
f = data[2];
@@ -64,7 +64,7 @@ float DHT::convertFtoC(float f) {
6464
return (f - 32) * 5 / 9;
6565
}
6666

67-
float DHT::readHumidity(void) {
67+
float DHT::readHumidity(bool force) {
6868
float f = NAN;
6969
if (read()) {
7070
switch (_type) {
@@ -113,15 +113,15 @@ float DHT::computeHeatIndex(float temperature, float percentHumidity, bool isFah
113113
}
114114
}
115115

116-
boolean DHT::read(void) {
116+
boolean DHT::read(bool force) {
117117
// Check if sensor was read less than two seconds ago and return early
118118
// to use last reading.
119119
uint32_t currenttime = millis();
120120
if (currenttime < _lastreadtime) {
121121
// ie there was a rollover
122122
_lastreadtime = 0;
123123
}
124-
if (!_firstreading && ((currenttime - _lastreadtime) < 2000)) {
124+
if (!force && !_firstreading && ((currenttime - _lastreadtime) < 2000)) {
125125
return _lastresult; // return last correct measurement
126126
}
127127
_firstreading = false;

DHT.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ class DHT {
3939
public:
4040
DHT(uint8_t pin, uint8_t type, uint8_t count=6);
4141
void begin(void);
42-
float readTemperature(bool S=false);
42+
float readTemperature(bool S=false, bool force=false);
4343
float convertCtoF(float);
4444
float convertFtoC(float);
4545
float computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit=true);
46-
float readHumidity(void);
47-
boolean read(void);
46+
float readHumidity(bool force=false);
47+
boolean read(bool force=false);
4848

4949
private:
5050
uint8_t data[6];

0 commit comments

Comments
 (0)