-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
DHT_U.cpp
239 lines (227 loc) · 6.29 KB
/
DHT_U.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*!
* @file DHT_U.cpp
*
* Temperature & Humidity Unified Sensor Library
*
* This is a library for DHT series of low cost temperature/humidity sensors.
*
* You must have Adafruit Unified Sensor Library library installed to use this
* class.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit andopen-source hardware by purchasing products
* from Adafruit!
*/
#include "DHT_U.h"
/*!
* @brief Instantiates a new DHT_Unified class
* @param pin
* pin number that sensor is connected
* @param type
* type of sensor
* @param count
* number of sensors
* @param tempSensorId
* temperature sensor id
* @param humiditySensorId
* humidity sensor id
*/
DHT_Unified::DHT_Unified(uint8_t pin, uint8_t type, uint8_t count,
int32_t tempSensorId, int32_t humiditySensorId)
: _dht(pin, type, count), _type(type), _temp(this, tempSensorId),
_humidity(this, humiditySensorId) {}
/*!
* @brief Setup sensor (calls begin on It)
*/
void DHT_Unified::begin() { _dht.begin(); }
/*!
* @brief Sets sensor name
* @param sensor
* Sensor that will be set
*/
void DHT_Unified::setName(sensor_t *sensor) {
switch (_type) {
case DHT11:
strncpy(sensor->name, "DHT11", sizeof(sensor->name) - 1);
break;
case DHT12:
strncpy(sensor->name, "DHT12", sizeof(sensor->name) - 1);
break;
case DHT21:
strncpy(sensor->name, "DHT21", sizeof(sensor->name) - 1);
break;
case DHT22:
strncpy(sensor->name, "DHT22", sizeof(sensor->name) - 1);
break;
default:
// TODO: Perhaps this should be an error? However main DHT library doesn't
// enforce restrictions on the sensor type value. Pick a generic name for
// now.
strncpy(sensor->name, "DHT?", sizeof(sensor->name) - 1);
break;
}
sensor->name[sizeof(sensor->name) - 1] = 0;
}
/*!
* @brief Sets Minimum Delay Value
* @param sensor
* Sensor that will be set
*/
void DHT_Unified::setMinDelay(sensor_t *sensor) {
switch (_type) {
case DHT11:
sensor->min_delay = 1000000L; // 1 second (in microseconds)
break;
case DHT12:
sensor->min_delay = 2000000L; // 2 second (in microseconds)
break;
case DHT21:
sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
break;
case DHT22:
sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
break;
default:
// Default to slowest sample rate in case of unknown type.
sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
break;
}
}
/*!
* @brief Instantiates a new DHT_Unified Temperature Class
* @param parent
* Parent Sensor
* @param id
* Sensor id
*/
DHT_Unified::Temperature::Temperature(DHT_Unified *parent, int32_t id)
: _parent(parent), _id(id) {}
/*!
* @brief Reads the sensor and returns the data as a sensors_event_t
* @param event
* @return always returns true
*/
bool DHT_Unified::Temperature::getEvent(sensors_event_t *event) {
// Clear event definition.
memset(event, 0, sizeof(sensors_event_t));
// Populate sensor reading values.
event->version = sizeof(sensors_event_t);
event->sensor_id = _id;
event->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
event->timestamp = millis();
event->temperature = _parent->_dht.readTemperature();
return true;
}
/*!
* @brief Provides the sensor_t data for this sensor
* @param sensor
*/
void DHT_Unified::Temperature::getSensor(sensor_t *sensor) {
// Clear sensor definition.
memset(sensor, 0, sizeof(sensor_t));
// Set sensor name.
_parent->setName(sensor);
// Set version and ID
sensor->version = DHT_SENSOR_VERSION;
sensor->sensor_id = _id;
// Set type and characteristics.
sensor->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
_parent->setMinDelay(sensor);
switch (_parent->_type) {
case DHT11:
sensor->max_value = 50.0F;
sensor->min_value = 0.0F;
sensor->resolution = 2.0F;
break;
case DHT12:
sensor->max_value = 60.0F;
sensor->min_value = -20.0F;
sensor->resolution = 0.5F;
break;
case DHT21:
sensor->max_value = 80.0F;
sensor->min_value = -40.0F;
sensor->resolution = 0.1F;
break;
case DHT22:
sensor->max_value = 125.0F;
sensor->min_value = -40.0F;
sensor->resolution = 0.1F;
break;
default:
// Unknown type, default to 0.
sensor->max_value = 0.0F;
sensor->min_value = 0.0F;
sensor->resolution = 0.0F;
break;
}
}
/*!
* @brief Instantiates a new DHT_Unified Humidity Class
* @param parent
* Parent Sensor
* @param id
* Sensor id
*/
DHT_Unified::Humidity::Humidity(DHT_Unified *parent, int32_t id)
: _parent(parent), _id(id) {}
/*!
* @brief Reads the sensor and returns the data as a sensors_event_t
* @param event
* @return always returns true
*/
bool DHT_Unified::Humidity::getEvent(sensors_event_t *event) {
// Clear event definition.
memset(event, 0, sizeof(sensors_event_t));
// Populate sensor reading values.
event->version = sizeof(sensors_event_t);
event->sensor_id = _id;
event->type = SENSOR_TYPE_RELATIVE_HUMIDITY;
event->timestamp = millis();
event->relative_humidity = _parent->_dht.readHumidity();
return true;
}
/*!
* @brief Provides the sensor_t data for this sensor
* @param sensor
*/
void DHT_Unified::Humidity::getSensor(sensor_t *sensor) {
// Clear sensor definition.
memset(sensor, 0, sizeof(sensor_t));
// Set sensor name.
_parent->setName(sensor);
// Set version and ID
sensor->version = DHT_SENSOR_VERSION;
sensor->sensor_id = _id;
// Set type and characteristics.
sensor->type = SENSOR_TYPE_RELATIVE_HUMIDITY;
_parent->setMinDelay(sensor);
switch (_parent->_type) {
case DHT11:
sensor->max_value = 80.0F;
sensor->min_value = 20.0F;
sensor->resolution = 5.0F;
break;
case DHT12:
sensor->max_value = 95.0F;
sensor->min_value = 20.0F;
sensor->resolution = 5.0F;
break;
case DHT21:
sensor->max_value = 100.0F;
sensor->min_value = 0.0F;
sensor->resolution = 0.1F;
break;
case DHT22:
sensor->max_value = 100.0F;
sensor->min_value = 0.0F;
sensor->resolution = 0.1F;
break;
default:
// Unknown type, default to 0.
sensor->max_value = 0.0F;
sensor->min_value = 0.0F;
sensor->resolution = 0.0F;
break;
}
}