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

Dependencies:   mbed

Committer:
RazielLopez
Date:
Fri Dec 21 03:47:05 2018 +0000
Revision:
1:6df9454191d4
Parent:
0:b1384299f31c
Child:
2:ce7b80e0bb2e
mejorando recepcion de datos

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