Based on another DHT library, this is just a modified one without using the RTC component

Dependencies:   mbed

Committer:
RazielLopez
Date:
Thu Dec 27 21:38:08 2018 +0000
Revision:
2:ce7b80e0bb2e
Parent:
1:6df9454191d4
Child:
3:8c402316ce7e
Compilado pero sin ser probado

Who changed what in which revision?

UserRevisionLine numberNew 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 /*for (i = 0; i < 5; i++) {
RazielLopez 2:ce7b80e0bb2e 108 b = 0;
RazielLopez 1:6df9454191d4 109 for (j = 0; j< 8; j++) {
RazielLopez 2:ce7b80e0bb2e 110 if ( bitTimes[ i*8+j+1 ] > 0) {
RazielLopez 0:b1384299f31c 111 b |= ( 1 << (7-j));
RazielLopez 0:b1384299f31c 112 }
RazielLopez 0:b1384299f31c 113 }
RazielLopez 0:b1384299f31c 114 DHT_data[i]=b;
RazielLopez 2:ce7b80e0bb2e 115 }*/
RazielLopez 0:b1384299f31c 116
RazielLopez 2:ce7b80e0bb2e 117 /*if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF)) {
RazielLopez 0:b1384299f31c 118 _lastReadTime = currentTime;
RazielLopez 0:b1384299f31c 119 _lastTemperature = CalcTemperature();
RazielLopez 0:b1384299f31c 120 _lastHumidity = CalcHumidity();
RazielLopez 0:b1384299f31c 121
RazielLopez 0:b1384299f31c 122 } else {
RazielLopez 0:b1384299f31c 123 err = ERROR_CHECKSUM;
RazielLopez 2:ce7b80e0bb2e 124 }*/
RazielLopez 2:ce7b80e0bb2e 125
RazielLopez 2:ce7b80e0bb2e 126 if(ValidCheckSum()){
RazielLopez 2:ce7b80e0bb2e 127 _lastReadTime = currentTime;
RazielLopez 2:ce7b80e0bb2e 128 }
RazielLopez 2:ce7b80e0bb2e 129 else{
RazielLopez 2:ce7b80e0bb2e 130 err = ERROR_CHECKSUM;
RazielLopez 0:b1384299f31c 131 }
RazielLopez 0:b1384299f31c 132
RazielLopez 0:b1384299f31c 133 return err;
RazielLopez 0:b1384299f31c 134 }
RazielLopez 0:b1384299f31c 135
RazielLopez 0:b1384299f31c 136 float DHT::CalcTemperature() {
RazielLopez 0:b1384299f31c 137 int v;
RazielLopez 0:b1384299f31c 138
RazielLopez 0:b1384299f31c 139 switch (_DHTtype) {
RazielLopez 0:b1384299f31c 140 case DHT11:
RazielLopez 0:b1384299f31c 141 v = DHT_data[2];
RazielLopez 0:b1384299f31c 142 return float(v);
RazielLopez 0:b1384299f31c 143 case DHT22:
RazielLopez 0:b1384299f31c 144 v = DHT_data[2] & 0x7F;
RazielLopez 0:b1384299f31c 145 v *= 256;
RazielLopez 0:b1384299f31c 146 v += DHT_data[3];
RazielLopez 0:b1384299f31c 147 v /= 10;
RazielLopez 0:b1384299f31c 148 if (DHT_data[2] & 0x80)
RazielLopez 0:b1384299f31c 149 v *= -1;
RazielLopez 0:b1384299f31c 150 return float(v);
RazielLopez 0:b1384299f31c 151 }
RazielLopez 0:b1384299f31c 152 return 0;
RazielLopez 0:b1384299f31c 153 }
RazielLopez 0:b1384299f31c 154
RazielLopez 0:b1384299f31c 155 float DHT::ReadHumidity() {
RazielLopez 0:b1384299f31c 156 return _lastHumidity;
RazielLopez 0:b1384299f31c 157 }
RazielLopez 0:b1384299f31c 158
RazielLopez 0:b1384299f31c 159 float DHT::ConvertCelciustoFarenheit(float celsius) {
RazielLopez 0:b1384299f31c 160 return celsius * 9 / 5 + 32;
RazielLopez 0:b1384299f31c 161 }
RazielLopez 0:b1384299f31c 162
RazielLopez 0:b1384299f31c 163 float DHT::ConvertCelciustoKelvin(float celsius) {
RazielLopez 0:b1384299f31c 164 return celsius + 273.15;
RazielLopez 0:b1384299f31c 165 }
RazielLopez 0:b1384299f31c 166
RazielLopez 0:b1384299f31c 167 // dewPoint function NOAA
RazielLopez 0:b1384299f31c 168 // reference: http://wahiduddin.net/calc/density_algorithms.htm
RazielLopez 0:b1384299f31c 169 float DHT::CalcdewPoint(float celsius, float humidity) {
RazielLopez 0:b1384299f31c 170 float A0= 373.15/(273.15 + celsius);
RazielLopez 0:b1384299f31c 171 float SUM = -7.90298 * (A0-1);
RazielLopez 0:b1384299f31c 172 SUM += 5.02808 * log10(A0);
RazielLopez 0:b1384299f31c 173 SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
RazielLopez 0:b1384299f31c 174 SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
RazielLopez 0:b1384299f31c 175 SUM += log10(1013.246);
RazielLopez 0:b1384299f31c 176 float VP = pow(10, SUM-3) * humidity;
RazielLopez 0:b1384299f31c 177 float T = log(VP/0.61078); // temp var
RazielLopez 0:b1384299f31c 178 return (241.88 * T) / (17.558-T);
RazielLopez 0:b1384299f31c 179 }
RazielLopez 0:b1384299f31c 180
RazielLopez 0:b1384299f31c 181 // delta max = 0.6544 wrt dewPoint()
RazielLopez 0:b1384299f31c 182 // 5x faster than dewPoint()
RazielLopez 0:b1384299f31c 183 // reference: http://en.wikipedia.org/wiki/Dew_point
RazielLopez 0:b1384299f31c 184 float DHT::CalcdewPointFast(float celsius, float humidity)
RazielLopez 0:b1384299f31c 185 {
RazielLopez 0:b1384299f31c 186 float a = 17.271;
RazielLopez 0:b1384299f31c 187 float b = 237.7;
RazielLopez 0:b1384299f31c 188 float temp = (a * celsius) / (b + celsius) + log(humidity/100);
RazielLopez 0:b1384299f31c 189 float Td = (b * temp) / (a - temp);
RazielLopez 0:b1384299f31c 190 return Td;
RazielLopez 0:b1384299f31c 191 }
RazielLopez 0:b1384299f31c 192
RazielLopez 0:b1384299f31c 193 float DHT::ReadTemperature(eScale Scale) {
RazielLopez 0:b1384299f31c 194 if (Scale == FARENHEIT)
RazielLopez 0:b1384299f31c 195 return ConvertCelciustoFarenheit(_lastTemperature);
RazielLopez 0:b1384299f31c 196 else if (Scale == KELVIN)
RazielLopez 0:b1384299f31c 197 return ConvertCelciustoKelvin(_lastTemperature);
RazielLopez 0:b1384299f31c 198 else
RazielLopez 0:b1384299f31c 199 return _lastTemperature;
RazielLopez 0:b1384299f31c 200 }
RazielLopez 0:b1384299f31c 201
RazielLopez 0:b1384299f31c 202 float DHT::CalcHumidity() {
RazielLopez 0:b1384299f31c 203 int v;
RazielLopez 0:b1384299f31c 204
RazielLopez 0:b1384299f31c 205 switch (_DHTtype) {
RazielLopez 0:b1384299f31c 206 case DHT11:
RazielLopez 0:b1384299f31c 207 v = DHT_data[0];
RazielLopez 0:b1384299f31c 208 return float(v);
RazielLopez 0:b1384299f31c 209 case DHT22:
RazielLopez 0:b1384299f31c 210 v = DHT_data[0];
RazielLopez 0:b1384299f31c 211 v *= 256;
RazielLopez 0:b1384299f31c 212 v += DHT_data[1];
RazielLopez 0:b1384299f31c 213 v /= 10;
RazielLopez 0:b1384299f31c 214 return float(v);
RazielLopez 0:b1384299f31c 215 }
RazielLopez 0:b1384299f31c 216 return 0;
RazielLopez 0:b1384299f31c 217 }
RazielLopez 0:b1384299f31c 218
RazielLopez 2:ce7b80e0bb2e 219 void DHT::BuildRxBytes(unsigned int * bitTimes ){
RazielLopez 2:ce7b80e0bb2e 220 int byteValue = 0;
RazielLopez 2:ce7b80e0bb2e 221 for (int byteCount = 0; byteCount < 5; byteCount++) {
RazielLopez 2:ce7b80e0bb2e 222 byteValue = 0;
RazielLopez 2:ce7b80e0bb2e 223 for (int bit = 0; bit < 8; bit++) {
RazielLopez 2:ce7b80e0bb2e 224 if ( bitTimes[ (byteCount * 8)+ bit + 1] > 0) {
RazielLopez 2:ce7b80e0bb2e 225 byteValue |= ( 1 << (7 - bit) );
RazielLopez 2:ce7b80e0bb2e 226 }
RazielLopez 2:ce7b80e0bb2e 227 }
RazielLopez 2:ce7b80e0bb2e 228 DHT_data[byteCount] = byteValue;
RazielLopez 2:ce7b80e0bb2e 229 }
RazielLopez 2:ce7b80e0bb2e 230 }
RazielLopez 0:b1384299f31c 231
RazielLopez 2:ce7b80e0bb2e 232 bool DHT::ValidCheckSum(void){
RazielLopez 2:ce7b80e0bb2e 233 bool CorrectCheckSum = false;
RazielLopez 2:ce7b80e0bb2e 234
RazielLopez 2:ce7b80e0bb2e 235 if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF)) {
RazielLopez 2:ce7b80e0bb2e 236 _lastTemperature = CalcTemperature();
RazielLopez 2:ce7b80e0bb2e 237 _lastHumidity = CalcHumidity();
RazielLopez 2:ce7b80e0bb2e 238 CorrectCheckSum = true;
RazielLopez 2:ce7b80e0bb2e 239 }
RazielLopez 2:ce7b80e0bb2e 240
RazielLopez 2:ce7b80e0bb2e 241 return CorrectCheckSum;
RazielLopez 2:ce7b80e0bb2e 242 }