Adding .ino files for IoTCloud library tests (periodic write and sleep); adding DHT...
[iotcloud.git] / version2 / src / others / DHT / DHT.cpp
1 /* DHT library 
2
3 MIT license
4 written by Adafruit Industries
5 */
6
7 #include "DHT.h"
8
9 DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
10   _pin = pin;
11   _type = type;
12   _count = count;
13   firstreading = true;
14 }
15
16 void DHT::begin(void) {
17   // set up the pins!
18   pinMode(_pin, INPUT);
19   digitalWrite(_pin, HIGH);
20   _lastreadtime = 0;
21 }
22
23 //boolean S == Scale.  True == Farenheit; False == Celcius
24 float DHT::readTemperature(bool S) {
25   float f;
26
27   if (read()) {
28     switch (_type) {
29     case DHT11:
30       f = data[2];
31       if(S)
32         f = convertCtoF(f);
33         
34       return f;
35     case DHT22:
36     case DHT21:
37       f = data[2] & 0x7F;
38       f *= 256;
39       f += data[3];
40       f /= 10;
41       if (data[2] & 0x80)
42         f *= -1;
43       if(S)
44         f = convertCtoF(f);
45
46       return f;
47     }
48   }
49   return NAN;
50 }
51
52 float DHT::convertCtoF(float c) {
53         return c * 9 / 5 + 32;
54 }
55
56 float DHT::convertFtoC(float f) {
57   return (f - 32) * 5 / 9; 
58 }
59
60 float DHT::readHumidity(void) {
61   float f;
62   if (read()) {
63     switch (_type) {
64     case DHT11:
65       f = data[0];
66       return f;
67     case DHT22:
68     case DHT21:
69       f = data[0];
70       f *= 256;
71       f += data[1];
72       f /= 10;
73       return f;
74     }
75   }
76   return NAN;
77 }
78
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
82   return -42.379 + 
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);
91 }
92
93
94 boolean DHT::read(void) {
95   uint8_t laststate = HIGH;
96   uint8_t counter = 0;
97   uint8_t j = 0, i;
98   unsigned long currenttime;
99
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
105     _lastreadtime = 0;
106   }
107   if (!firstreading && ((currenttime - _lastreadtime) < 2000)) {
108     return true; // return last correct measurement
109     //delay(2000 - (currenttime - _lastreadtime));
110   }
111   firstreading = false;
112   /*
113     Serial.print("Currtime: "); Serial.print(currenttime);
114     Serial.print(" Lasttime: "); Serial.print(_lastreadtime);
115   */
116   _lastreadtime = millis();
117
118   data[0] = data[1] = data[2] = data[3] = data[4] = 0;
119   
120   // pull the pin high and wait 250 milliseconds
121   digitalWrite(_pin, HIGH);
122   delay(250);
123
124   // now pull it low for ~20 milliseconds
125   pinMode(_pin, OUTPUT);
126   digitalWrite(_pin, LOW);
127   delay(20);
128   noInterrupts();
129   digitalWrite(_pin, HIGH);
130   delayMicroseconds(40);
131   pinMode(_pin, INPUT);
132
133   // read in timings
134   for ( i=0; i< MAXTIMINGS; i++) {
135     counter = 0;
136     while (digitalRead(_pin) == laststate) {
137       counter++;
138       delayMicroseconds(1);
139       if (counter == 255) {
140         break;
141       }
142     }
143     laststate = digitalRead(_pin);
144
145     if (counter == 255) break;
146
147     // ignore first 3 transitions
148     if ((i >= 4) && (i%2 == 0)) {
149       // shove each bit into the storage bytes
150       data[j/8] <<= 1;
151       if (counter > _count)
152         data[j/8] |= 1;
153       j++;
154     }
155
156   }
157
158   interrupts();
159   
160   /*
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);
168   */
169
170   // check we read 40 bits and that the checksum matches
171   if ((j >= 40) && 
172       (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {
173     return true;
174   }
175   
176
177   return false;
178
179 }