4 written by Adafruit Industries
9 DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
16 void DHT::begin(void) {
19 digitalWrite(_pin, HIGH);
23 //boolean S == Scale. True == Farenheit; False == Celcius
24 float DHT::readTemperature(bool S) {
52 float DHT::convertCtoF(float c) {
53 return c * 9 / 5 + 32;
56 float DHT::convertFtoC(float f) {
57 return (f - 32) * 5 / 9;
60 float DHT::readHumidity(void) {
79 float DHT::computeHeatIndex(float tempFahrenheit, float percentHumidity) {
80 // Adapted from equation at: https://github.com/adafruit/DHT-sensor-library/issues/9 and
81 // Wikipedia: http://en.wikipedia.org/wiki/Heat_index
83 2.04901523 * tempFahrenheit +
84 10.14333127 * percentHumidity +
85 -0.22475541 * tempFahrenheit*percentHumidity +
86 -0.00683783 * pow(tempFahrenheit, 2) +
87 -0.05481717 * pow(percentHumidity, 2) +
88 0.00122874 * pow(tempFahrenheit, 2) * percentHumidity +
89 0.00085282 * tempFahrenheit*pow(percentHumidity, 2) +
90 -0.00000199 * pow(tempFahrenheit, 2) * pow(percentHumidity, 2);
94 boolean DHT::read(void) {
95 uint8_t laststate = HIGH;
98 unsigned long currenttime;
100 // Check if sensor was read less than two seconds ago and return early
101 // to use last reading.
102 currenttime = millis();
103 if (currenttime < _lastreadtime) {
104 // ie there was a rollover
107 if (!firstreading && ((currenttime - _lastreadtime) < 2000)) {
108 return true; // return last correct measurement
109 //delay(2000 - (currenttime - _lastreadtime));
111 firstreading = false;
113 Serial.print("Currtime: "); Serial.print(currenttime);
114 Serial.print(" Lasttime: "); Serial.print(_lastreadtime);
116 _lastreadtime = millis();
118 data[0] = data[1] = data[2] = data[3] = data[4] = 0;
120 // pull the pin high and wait 250 milliseconds
121 digitalWrite(_pin, HIGH);
124 // now pull it low for ~20 milliseconds
125 pinMode(_pin, OUTPUT);
126 digitalWrite(_pin, LOW);
129 digitalWrite(_pin, HIGH);
130 delayMicroseconds(40);
131 pinMode(_pin, INPUT);
134 for ( i=0; i< MAXTIMINGS; i++) {
136 while (digitalRead(_pin) == laststate) {
138 delayMicroseconds(1);
139 if (counter == 255) {
143 laststate = digitalRead(_pin);
145 if (counter == 255) break;
147 // ignore first 3 transitions
148 if ((i >= 4) && (i%2 == 0)) {
149 // shove each bit into the storage bytes
151 if (counter > _count)
161 Serial.println(j, DEC);
162 Serial.print(data[0], HEX); Serial.print(", ");
163 Serial.print(data[1], HEX); Serial.print(", ");
164 Serial.print(data[2], HEX); Serial.print(", ");
165 Serial.print(data[3], HEX); Serial.print(", ");
166 Serial.print(data[4], HEX); Serial.print(" =? ");
167 Serial.println(data[0] + data[1] + data[2] + data[3], HEX);
170 // check we read 40 bits and that the checksum matches
172 (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {