Based on another DHT library, this is just a modified one without using the RTC component
DHT/DHT.cpp@3:8c402316ce7e, 2019-01-06 (annotated)
- Committer:
- RazielLopez
- Date:
- Sun Jan 06 03:08:10 2019 +0000
- Revision:
- 3:8c402316ce7e
- Parent:
- 2:ce7b80e0bb2e
Agregando mejoras a la libreria original
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| RazielLopez | 0:b1384299f31c | 1 | |
| RazielLopez | 0:b1384299f31c | 2 | |
| RazielLopez | 0:b1384299f31c | 3 | #include "DHT.h" |
| RazielLopez | 0:b1384299f31c | 4 | |
| RazielLopez | 0:b1384299f31c | 5 | #define DHT_DATA_BIT_COUNT 41 |
| RazielLopez | 0:b1384299f31c | 6 | |
| RazielLopez | 0:b1384299f31c | 7 | DHT::DHT(PinName pin,int DHTtype) { |
| RazielLopez | 0:b1384299f31c | 8 | _pin = pin; |
| RazielLopez | 0:b1384299f31c | 9 | _DHTtype = DHTtype; |
| RazielLopez | 0:b1384299f31c | 10 | _firsttime=true; |
| RazielLopez | 0:b1384299f31c | 11 | } |
| RazielLopez | 0:b1384299f31c | 12 | |
| RazielLopez | 0:b1384299f31c | 13 | DHT::~DHT() { |
| RazielLopez | 0:b1384299f31c | 14 | } |
| RazielLopez | 0:b1384299f31c | 15 | |
| RazielLopez | 0:b1384299f31c | 16 | int DHT::readData() { |
| RazielLopez | 0:b1384299f31c | 17 | int i, j, retryCount,b; |
| RazielLopez | 0:b1384299f31c | 18 | unsigned int bitTimes[DHT_DATA_BIT_COUNT]; |
| RazielLopez | 0:b1384299f31c | 19 | |
| RazielLopez | 0:b1384299f31c | 20 | eError err = ERROR_NONE; |
| RazielLopez | 0:b1384299f31c | 21 | uint32_t currentTime = us_ticker_read() / 1000000; |
| RazielLopez | 0:b1384299f31c | 22 | |
| RazielLopez | 0:b1384299f31c | 23 | DigitalInOut DHT_io(_pin); |
| RazielLopez | 0:b1384299f31c | 24 | |
| RazielLopez | 0:b1384299f31c | 25 | //Clear the bit buffer. |
| RazielLopez | 0:b1384299f31c | 26 | for (i = 0; i < DHT_DATA_BIT_COUNT; i++) { |
| RazielLopez | 0:b1384299f31c | 27 | bitTimes[i] = 0; |
| RazielLopez | 0:b1384299f31c | 28 | } |
| RazielLopez | 0:b1384299f31c | 29 | |
| RazielLopez | 0:b1384299f31c | 30 | if (!_firsttime) { |
| RazielLopez | 0:b1384299f31c | 31 | if (int(currentTime - _lastReadTime) < 2) { |
| RazielLopez | 0:b1384299f31c | 32 | err = ERROR_NO_PATIENCE; |
| RazielLopez | 0:b1384299f31c | 33 | return err; |
| RazielLopez | 0:b1384299f31c | 34 | } |
| RazielLopez | 0:b1384299f31c | 35 | } else { |
| RazielLopez | 0:b1384299f31c | 36 | _firsttime = false; |
| RazielLopez | 0:b1384299f31c | 37 | _lastReadTime = currentTime; |
| RazielLopez | 0:b1384299f31c | 38 | } |
| RazielLopez | 0:b1384299f31c | 39 | retryCount = 0; |
| RazielLopez | 0:b1384299f31c | 40 | |
| RazielLopez | 0:b1384299f31c | 41 | do { |
| RazielLopez | 0:b1384299f31c | 42 | if (retryCount > 125) { |
| RazielLopez | 0:b1384299f31c | 43 | err = BUS_BUSY; |
| RazielLopez | 0:b1384299f31c | 44 | return err; |
| RazielLopez | 0:b1384299f31c | 45 | } |
| RazielLopez | 0:b1384299f31c | 46 | retryCount++; |
| RazielLopez | 0:b1384299f31c | 47 | wait_us(2); |
| RazielLopez | 0:b1384299f31c | 48 | } while (DHT_io == 0); |
| RazielLopez | 0:b1384299f31c | 49 | |
| RazielLopez | 0:b1384299f31c | 50 | |
| RazielLopez | 0:b1384299f31c | 51 | //Set pin as output |
| RazielLopez | 0:b1384299f31c | 52 | DHT_io.output(); |
| RazielLopez | 0:b1384299f31c | 53 | //Put the Line as High impedance |
| RazielLopez | 0:b1384299f31c | 54 | DHT_io = 1; |
| RazielLopez | 0:b1384299f31c | 55 | wait_ms(250); |
| RazielLopez | 0:b1384299f31c | 56 | |
| RazielLopez | 0:b1384299f31c | 57 | //Start of message. |
| RazielLopez | 0:b1384299f31c | 58 | DHT_io = 0; |
| RazielLopez | 0:b1384299f31c | 59 | wait_ms(20); |
| RazielLopez | 0:b1384299f31c | 60 | |
| RazielLopez | 0:b1384299f31c | 61 | DHT_io = 1; |
| RazielLopez | 0:b1384299f31c | 62 | wait_us(40); |
| RazielLopez | 0:b1384299f31c | 63 | //Set line as input |
| RazielLopez | 0:b1384299f31c | 64 | DHT_io.input(); |
| RazielLopez | 0:b1384299f31c | 65 | |
| RazielLopez | 0:b1384299f31c | 66 | wait_us(10); |
| RazielLopez | 0:b1384299f31c | 67 | |
| RazielLopez | 0:b1384299f31c | 68 | retryCount = 0; |
| RazielLopez | 0:b1384299f31c | 69 | |
| RazielLopez | 0:b1384299f31c | 70 | do { |
| RazielLopez | 0:b1384299f31c | 71 | if (retryCount > 40) { |
| RazielLopez | 0:b1384299f31c | 72 | err = ERROR_NOT_PRESENT; |
| RazielLopez | 0:b1384299f31c | 73 | return err; |
| RazielLopez | 0:b1384299f31c | 74 | } |
| RazielLopez | 0:b1384299f31c | 75 | retryCount++; |
| RazielLopez | 0:b1384299f31c | 76 | wait_us(1); |
| RazielLopez | 0:b1384299f31c | 77 | } while ( DHT_io == 1 ); |
| RazielLopez | 0:b1384299f31c | 78 | |
| RazielLopez | 0:b1384299f31c | 79 | if (err != ERROR_NONE) { |
| RazielLopez | 0:b1384299f31c | 80 | return err; |
| RazielLopez | 0:b1384299f31c | 81 | } |
| RazielLopez | 0:b1384299f31c | 82 | |
| RazielLopez | 0:b1384299f31c | 83 | for (i = 0; i < 5; i++) { |
| RazielLopez | 0:b1384299f31c | 84 | for (j = 0; j < 8; j++) { |
| RazielLopez | 0:b1384299f31c | 85 | retryCount = 0; |
| RazielLopez | 0:b1384299f31c | 86 | do { |
| RazielLopez | 0:b1384299f31c | 87 | if (retryCount > 78) { |
| RazielLopez | 0:b1384299f31c | 88 | err = ERROR_DATA_TIMEOUT; |
| RazielLopez | 0:b1384299f31c | 89 | return err; |
| RazielLopez | 0:b1384299f31c | 90 | } |
| RazielLopez | 0:b1384299f31c | 91 | retryCount++; |
| RazielLopez | 0:b1384299f31c | 92 | wait_us(1); |
| RazielLopez | 0:b1384299f31c | 93 | } while (DHT_io == 0); |
| RazielLopez | 1:6df9454191d4 | 94 | wait_us(30); |
| RazielLopez | 1:6df9454191d4 | 95 | bitTimes[i*8 + j] = DHT_io; |
| RazielLopez | 0:b1384299f31c | 96 | int count = 0; |
| RazielLopez | 2:ce7b80e0bb2e | 97 | while (DHT_io == 1 && count < 100) { |
| RazielLopez | 0:b1384299f31c | 98 | wait_us(1); |
| RazielLopez | 0:b1384299f31c | 99 | count++; |
| RazielLopez | 0:b1384299f31c | 100 | } |
| RazielLopez | 0:b1384299f31c | 101 | } |
| RazielLopez | 0:b1384299f31c | 102 | } |
| RazielLopez | 0:b1384299f31c | 103 | DHT_io.output(); |
| RazielLopez | 0:b1384299f31c | 104 | DHT_io = 1; |
| RazielLopez | 2:ce7b80e0bb2e | 105 | |
| RazielLopez | 2:ce7b80e0bb2e | 106 | BuildRxBytes(bitTimes); |
| RazielLopez | 2:ce7b80e0bb2e | 107 | if(ValidCheckSum()){ |
| RazielLopez | 2:ce7b80e0bb2e | 108 | _lastReadTime = currentTime; |
| RazielLopez | 2:ce7b80e0bb2e | 109 | } |
| RazielLopez | 2:ce7b80e0bb2e | 110 | else{ |
| RazielLopez | 2:ce7b80e0bb2e | 111 | err = ERROR_CHECKSUM; |
| RazielLopez | 0:b1384299f31c | 112 | } |
| RazielLopez | 0:b1384299f31c | 113 | |
| RazielLopez | 0:b1384299f31c | 114 | return err; |
| RazielLopez | 0:b1384299f31c | 115 | } |
| RazielLopez | 0:b1384299f31c | 116 | |
| RazielLopez | 0:b1384299f31c | 117 | float DHT::CalcTemperature() { |
| RazielLopez | 0:b1384299f31c | 118 | int v; |
| RazielLopez | 0:b1384299f31c | 119 | |
| RazielLopez | 0:b1384299f31c | 120 | switch (_DHTtype) { |
| RazielLopez | 0:b1384299f31c | 121 | case DHT11: |
| RazielLopez | 0:b1384299f31c | 122 | v = DHT_data[2]; |
| RazielLopez | 0:b1384299f31c | 123 | return float(v); |
| RazielLopez | 0:b1384299f31c | 124 | case DHT22: |
| RazielLopez | 0:b1384299f31c | 125 | v = DHT_data[2] & 0x7F; |
| RazielLopez | 0:b1384299f31c | 126 | v *= 256; |
| RazielLopez | 0:b1384299f31c | 127 | v += DHT_data[3]; |
| RazielLopez | 0:b1384299f31c | 128 | v /= 10; |
| RazielLopez | 0:b1384299f31c | 129 | if (DHT_data[2] & 0x80) |
| RazielLopez | 0:b1384299f31c | 130 | v *= -1; |
| RazielLopez | 0:b1384299f31c | 131 | return float(v); |
| RazielLopez | 0:b1384299f31c | 132 | } |
| RazielLopez | 0:b1384299f31c | 133 | return 0; |
| RazielLopez | 0:b1384299f31c | 134 | } |
| RazielLopez | 0:b1384299f31c | 135 | |
| RazielLopez | 0:b1384299f31c | 136 | float DHT::ReadHumidity() { |
| RazielLopez | 0:b1384299f31c | 137 | return _lastHumidity; |
| RazielLopez | 0:b1384299f31c | 138 | } |
| RazielLopez | 0:b1384299f31c | 139 | |
| RazielLopez | 0:b1384299f31c | 140 | float DHT::ConvertCelciustoFarenheit(float celsius) { |
| RazielLopez | 0:b1384299f31c | 141 | return celsius * 9 / 5 + 32; |
| RazielLopez | 0:b1384299f31c | 142 | } |
| RazielLopez | 0:b1384299f31c | 143 | |
| RazielLopez | 0:b1384299f31c | 144 | float DHT::ConvertCelciustoKelvin(float celsius) { |
| RazielLopez | 0:b1384299f31c | 145 | return celsius + 273.15; |
| RazielLopez | 0:b1384299f31c | 146 | } |
| RazielLopez | 0:b1384299f31c | 147 | |
| RazielLopez | 0:b1384299f31c | 148 | // dewPoint function NOAA |
| RazielLopez | 0:b1384299f31c | 149 | // reference: http://wahiduddin.net/calc/density_algorithms.htm |
| RazielLopez | 0:b1384299f31c | 150 | float DHT::CalcdewPoint(float celsius, float humidity) { |
| RazielLopez | 0:b1384299f31c | 151 | float A0= 373.15/(273.15 + celsius); |
| RazielLopez | 0:b1384299f31c | 152 | float SUM = -7.90298 * (A0-1); |
| RazielLopez | 0:b1384299f31c | 153 | SUM += 5.02808 * log10(A0); |
| RazielLopez | 0:b1384299f31c | 154 | SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ; |
| RazielLopez | 0:b1384299f31c | 155 | SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; |
| RazielLopez | 0:b1384299f31c | 156 | SUM += log10(1013.246); |
| RazielLopez | 0:b1384299f31c | 157 | float VP = pow(10, SUM-3) * humidity; |
| RazielLopez | 0:b1384299f31c | 158 | float T = log(VP/0.61078); // temp var |
| RazielLopez | 0:b1384299f31c | 159 | return (241.88 * T) / (17.558-T); |
| RazielLopez | 0:b1384299f31c | 160 | } |
| RazielLopez | 0:b1384299f31c | 161 | |
| RazielLopez | 0:b1384299f31c | 162 | // delta max = 0.6544 wrt dewPoint() |
| RazielLopez | 0:b1384299f31c | 163 | // 5x faster than dewPoint() |
| RazielLopez | 0:b1384299f31c | 164 | // reference: http://en.wikipedia.org/wiki/Dew_point |
| RazielLopez | 0:b1384299f31c | 165 | float DHT::CalcdewPointFast(float celsius, float humidity) |
| RazielLopez | 0:b1384299f31c | 166 | { |
| RazielLopez | 0:b1384299f31c | 167 | float a = 17.271; |
| RazielLopez | 0:b1384299f31c | 168 | float b = 237.7; |
| RazielLopez | 0:b1384299f31c | 169 | float temp = (a * celsius) / (b + celsius) + log(humidity/100); |
| RazielLopez | 0:b1384299f31c | 170 | float Td = (b * temp) / (a - temp); |
| RazielLopez | 0:b1384299f31c | 171 | return Td; |
| RazielLopez | 0:b1384299f31c | 172 | } |
| RazielLopez | 0:b1384299f31c | 173 | |
| RazielLopez | 0:b1384299f31c | 174 | float DHT::ReadTemperature(eScale Scale) { |
| RazielLopez | 0:b1384299f31c | 175 | if (Scale == FARENHEIT) |
| RazielLopez | 0:b1384299f31c | 176 | return ConvertCelciustoFarenheit(_lastTemperature); |
| RazielLopez | 0:b1384299f31c | 177 | else if (Scale == KELVIN) |
| RazielLopez | 0:b1384299f31c | 178 | return ConvertCelciustoKelvin(_lastTemperature); |
| RazielLopez | 0:b1384299f31c | 179 | else |
| RazielLopez | 0:b1384299f31c | 180 | return _lastTemperature; |
| RazielLopez | 0:b1384299f31c | 181 | } |
| RazielLopez | 0:b1384299f31c | 182 | |
| RazielLopez | 0:b1384299f31c | 183 | float DHT::CalcHumidity() { |
| RazielLopez | 0:b1384299f31c | 184 | int v; |
| RazielLopez | 0:b1384299f31c | 185 | |
| RazielLopez | 0:b1384299f31c | 186 | switch (_DHTtype) { |
| RazielLopez | 0:b1384299f31c | 187 | case DHT11: |
| RazielLopez | 0:b1384299f31c | 188 | v = DHT_data[0]; |
| RazielLopez | 0:b1384299f31c | 189 | return float(v); |
| RazielLopez | 0:b1384299f31c | 190 | case DHT22: |
| RazielLopez | 0:b1384299f31c | 191 | v = DHT_data[0]; |
| RazielLopez | 0:b1384299f31c | 192 | v *= 256; |
| RazielLopez | 0:b1384299f31c | 193 | v += DHT_data[1]; |
| RazielLopez | 0:b1384299f31c | 194 | v /= 10; |
| RazielLopez | 0:b1384299f31c | 195 | return float(v); |
| RazielLopez | 0:b1384299f31c | 196 | } |
| RazielLopez | 0:b1384299f31c | 197 | return 0; |
| RazielLopez | 0:b1384299f31c | 198 | } |
| RazielLopez | 0:b1384299f31c | 199 | |
| RazielLopez | 2:ce7b80e0bb2e | 200 | void DHT::BuildRxBytes(unsigned int * bitTimes ){ |
| RazielLopez | 2:ce7b80e0bb2e | 201 | int byteValue = 0; |
| RazielLopez | 2:ce7b80e0bb2e | 202 | for (int byteCount = 0; byteCount < 5; byteCount++) { |
| RazielLopez | 2:ce7b80e0bb2e | 203 | byteValue = 0; |
| RazielLopez | 2:ce7b80e0bb2e | 204 | for (int bit = 0; bit < 8; bit++) { |
| RazielLopez | 2:ce7b80e0bb2e | 205 | if ( bitTimes[ (byteCount * 8)+ bit + 1] > 0) { |
| RazielLopez | 2:ce7b80e0bb2e | 206 | byteValue |= ( 1 << (7 - bit) ); |
| RazielLopez | 2:ce7b80e0bb2e | 207 | } |
| RazielLopez | 2:ce7b80e0bb2e | 208 | } |
| RazielLopez | 2:ce7b80e0bb2e | 209 | DHT_data[byteCount] = byteValue; |
| RazielLopez | 2:ce7b80e0bb2e | 210 | } |
| RazielLopez | 2:ce7b80e0bb2e | 211 | } |
| RazielLopez | 0:b1384299f31c | 212 | |
| RazielLopez | 2:ce7b80e0bb2e | 213 | bool DHT::ValidCheckSum(void){ |
| RazielLopez | 2:ce7b80e0bb2e | 214 | bool CorrectCheckSum = false; |
| RazielLopez | 2:ce7b80e0bb2e | 215 | |
| RazielLopez | 2:ce7b80e0bb2e | 216 | if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF)) { |
| RazielLopez | 2:ce7b80e0bb2e | 217 | _lastTemperature = CalcTemperature(); |
| RazielLopez | 2:ce7b80e0bb2e | 218 | _lastHumidity = CalcHumidity(); |
| RazielLopez | 2:ce7b80e0bb2e | 219 | CorrectCheckSum = true; |
| RazielLopez | 2:ce7b80e0bb2e | 220 | } |
| RazielLopez | 2:ce7b80e0bb2e | 221 | |
| RazielLopez | 2:ce7b80e0bb2e | 222 | return CorrectCheckSum; |
| RazielLopez | 2:ce7b80e0bb2e | 223 | } |