DHT.cpp
Go to the documentation of this file.
1 /*
2  * DHT Library for Digital-output Humidity and Temperature sensors
3  *
4  * Works with DHT11, DHT22
5  * SEN11301P, Grove - Temperature&Humidity Sensor (Seeed Studio)
6  * SEN51035P, Grove - Temperature&Humidity Sensor Pro (Seeed Studio)
7  * AM2302 , temperature-humidity sensor
8  * HM2303 , Digital-output humidity and temperature sensor
9  *
10  * Copyright (C) Wim De Roeve
11  * based on DHT22 sensor library by HO WING KIT
12  * Arduino DHT11 library
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a copy
15  * of this software and associated documnetation files (the "Software"), to deal
16  * in the Software without restriction, including without limitation the rights
17  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18  * copies of the Software, and to permit persons to whom the Software is
19  * furished to do so, subject to the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be included in
22  * all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30  * THE SOFTWARE.
31  */
32 
33 #include "DHT.h"
34 
35 #define DHT_DATA_BIT_COUNT 40
36 
37 DHT::DHT(PinName pin, eType DHTtype)
38 {
39  _pin = pin;
40  _DHTtype = DHTtype;
41  _firsttime = true;
42 }
43 
45 {
46 
47 }
48 
49 eError DHT::stall(DigitalInOut &io, int const level, int const max_time)
50 {
51  int cnt = 0;
52  while (level == io)
53  {
54  if (cnt > max_time)
55  {
56  return ERROR_NO_PATIENCE;
57  }
58  cnt++;
59  wait_us(1);
60  }
61  return ERROR_NONE;
62 }
63 
65 {
66  uint8_t i = 0, j = 0, b = 0, data_valid = 0;
67  uint32_t bit_value[DHT_DATA_BIT_COUNT] = {0};
68 
69  eError err = ERROR_NONE;
70  //time_t currentTime = time(NULL);
71 
72  DigitalInOut DHT_io(_pin);
73 
74  // IO must be in hi state to start
75  if (ERROR_NONE != stall(DHT_io, 0, 250))
76  {
77  return BUS_BUSY;
78  }
79 
80  // start the transfer
81  DHT_io.output();
82  DHT_io = 0;
83  // only 500uS for DHT22 but 18ms for DHT11
84  (_DHTtype == 22) ? wait_ms(18) : wait(1);
85  DHT_io = 1;
86  wait_us(30);
87  DHT_io.input();
88  // wait till the sensor grabs the bus
89  if (ERROR_NONE != stall(DHT_io, 1, 40))
90  {
91  return ERROR_NOT_PRESENT;
92  }
93  // sensor should signal low 80us and then hi 80us
94  if (ERROR_NONE != stall(DHT_io, 0, 100))
95  {
96  return ERROR_SYNC_TIMEOUT;
97  }
98  if (ERROR_NONE != stall(DHT_io, 1, 100))
99  {
100  return ERROR_NO_PATIENCE;
101  }
102  // capture the data
103  for (i = 0; i < 5; i++)
104  {
105  for (j = 0; j < 8; j++)
106  {
107  if (ERROR_NONE != stall(DHT_io, 0, 75))
108  {
109  return ERROR_DATA_TIMEOUT;
110  }
111  // logic 0 is 28us max, 1 is 70us
112  wait_us(40);
113  bit_value[i * 8 + j] = DHT_io;
114  if (ERROR_NONE != stall(DHT_io, 1, 50))
115  {
116  return ERROR_DATA_TIMEOUT;
117  }
118  }
119  }
120  // store the data
121  for (i = 0; i < 5; i++)
122  {
123  b = 0;
124  for (j = 0; j < 8; j++)
125  {
126  if (bit_value[i * 8 + j] == 1)
127  {
128  b |= (1 << (7 - j));
129  }
130  }
131  DHT_data[i] = b;
132  }
133 
134  // uncomment to see the checksum error if it exists
135  //printf(" 0x%02x + 0x%02x + 0x%02x + 0x%02x = 0x%02x \n", DHT_data[0], DHT_data[1], DHT_data[2], DHT_data[3], DHT_data[4]);
136  data_valid = DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3];
137  if (DHT_data[4] == data_valid)
138  {
139  //_lastReadTime = currentTime;
142 
143  }
144  else
145  {
146  err = ERROR_CHECKSUM;
147  }
148 
149  return err;
150 
151 }
152 
154 {
155  int v;
156 
157  switch (_DHTtype)
158  {
159  case DHT11:
160  v = DHT_data[2];
161  return float(v);
162  case DHT22:
163  v = DHT_data[2] & 0x7F;
164  v *= 256;
165  v += DHT_data[3];
166  v /= 10;
167  if (DHT_data[2] & 0x80)
168  v *= -1;
169  return float(v);
170  }
171  return 0;
172 }
173 
175 {
176  return _lastHumidity;
177 }
178 
179 float DHT::ConvertCelciustoFarenheit(float const celsius)
180 {
181  return celsius * 9 / 5 + 32;
182 }
183 
184 float DHT::ConvertCelciustoKelvin(float const celsius)
185 {
186  return celsius + 273.15;
187 }
188 
189 // dewPoint function NOAA
190 // reference: http://wahiduddin.net/calc/density_algorithms.htm
191 float DHT::CalcdewPoint(float const celsius, float const humidity)
192 {
193  float A0 = 373.15 / (273.15 + celsius);
194  float SUM = -7.90298 * (A0 - 1);
195  SUM += 5.02808 * log10(A0);
196  SUM += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / A0))) - 1) ;
197  SUM += 8.1328e-3 * (pow(10, (-3.49149 * (A0 - 1))) - 1) ;
198  SUM += log10(1013.246);
199  float VP = pow(10, SUM - 3) * humidity;
200  float T = log(VP / 0.61078); // temp var
201  return (241.88 * T) / (17.558 - T);
202 }
203 
204 // delta max = 0.6544 wrt dewPoint()
205 // 5x faster than dewPoint()
206 // reference: http://en.wikipedia.org/wiki/Dew_point
207 float DHT::CalcdewPointFast(float const celsius, float const humidity)
208 {
209  float a = 17.271;
210  float b = 237.7;
211  float temp = (a * celsius) / (b + celsius) + log(humidity / 100);
212  float Td = (b * temp) / (a - temp);
213  return Td;
214 }
215 
217 {
218  if (Scale == FARENHEIT)
220  else if (Scale == KELVIN)
222  else
223  return _lastTemperature;
224 }
225 
227 {
228  int v;
229 
230  switch (_DHTtype)
231  {
232  case DHT11:
233  v = DHT_data[0];
234  return float(v);
235  case DHT22:
236  v = DHT_data[0];
237  v *= 256;
238  v += DHT_data[1];
239  v /= 10;
240  return float(v);
241  }
242  return 0;
243 }
244 
245 
eError
Definition: DHT.h:51
float CalcdewPoint(float const celsius, float const humidity)
Definition: DHT.cpp:191
float ReadTemperature(eScale const Scale)
Definition: DHT.cpp:216
#define DHT_DATA_BIT_COUNT
Definition: DHT.cpp:35
float CalcHumidity()
Definition: DHT.cpp:226
float _lastTemperature
Definition: DHT.h:88
float CalcTemperature()
Definition: DHT.cpp:153
Definition: DHT.h:40
float _lastHumidity
Definition: DHT.h:89
Definition: DHT.h:67
~DHT()
Definition: DHT.cpp:44
float ConvertCelciustoKelvin(float const)
Definition: DHT.cpp:184
eScale
Definition: DHT.h:64
int i
eType _DHTtype
Definition: DHT.h:92
eType
Definition: DHT.h:38
Definition: DHT.h:54
Definition: DHT.h:53
float ReadHumidity(void)
Definition: DHT.cpp:174
uint8_t DHT_data[5]
Definition: DHT.h:93
bool _firsttime
Definition: DHT.h:91
eError stall(DigitalInOut &io, int const level, int const max_time)
Definition: DHT.cpp:49
Definition: DHT.h:43
eError readData(void)
Definition: DHT.cpp:64
Definition: DHT.h:68
float CalcdewPointFast(float const celsius, float const humidity)
Definition: DHT.cpp:207
float ConvertCelciustoFarenheit(float const)
Definition: DHT.cpp:179
PinName _pin
Definition: DHT.h:90
DHT(PinName pin, eType DHTtype)
Definition: DHT.cpp:37


rosserial_mbed
Author(s): Gary Servin
autogenerated on Fri Jun 7 2019 22:02:48