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