-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht22.cpp
71 lines (59 loc) · 1.58 KB
/
dht22.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "dht22.h"
// Number of times to re-poll the dht for temperature on error.
int temperature_retries = 5;
/**
* Takes Digitial pin index to set up connection.
*/
Dht::Dht() {}
/**
* Initialize the DHT22 sensor attached to sensor_pin.
*/
void Dht::sensorPin(int sensor_pin) {
this->setup(sensor_pin, DHTesp::DHT22);
}
/**
* Store the current values for future requests.
*
* If non-number comes back, wait 1 second and retry, up to 5 times.
*/
void Dht::takeMeasurements(int retries_left) {
Serial.println("Dht::takeMeasurements - Begin");
this->moisture = this->getHumidity();
this->temperature = this->getTemperature();
if(!isnan(this->moisture) && !isnan(this->temperature)) {
Serial.println("Dht::takeMeasurements - End");
return;
}
if (retries_left-- > 0) {
delay(1000);
Serial.print("Dht::takeMeasurements - retrying, temp ");
Serial.print(this->temperature);
Serial.print(isnan(this->moisture));
Serial.print(" humidity ");
Serial.print(this->moisture);
Serial.println(isnan(this->moisture));
this->takeMeasurements(retries_left--);
}
}
/**
* Convert cenigrate to farenheit.
*/
float Dht::cToF(float temp_c) {
Serial.print("Dht::cToF, converting temp ");
Serial.print(temp_c);
Serial.println("c");
return (temp_c * 1.8) + 32;
}
float Dht::humidity() {
return this->moisture;
}
float Dht::tempC() {
return this->temperature;
}
float Dht::tempF() {
Serial.println("Dht::tempF begin");
float result = this->cToF(this->temperature);
Serial.print("Dht::tempF, returning");
Serial.println(result);
return result;
}