-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathDhtTemperatureAndHumiditySensor.ino
291 lines (230 loc) · 9.97 KB
/
DhtTemperatureAndHumiditySensor.ino
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <[email protected]>
* Copyright (C) 2013-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*******************************
*
* REVISION HISTORY
* Version 1.0 (humidity sensor): Henrik EKblad
* Version 1.1 - 2016-07-20 (humidity sensor): Converted to MySensors v2.0 and added various improvements - Torben Woltjen (mozzbozz)
* Version 2.0 - 2018-09-25: Converted to DHTU Adafruit library - Tiberio Galletti
* Version 2.1 - 2018-10-06: Clearer code and... if something changed... every sensor data are sent to gateway - Tiberio Galletti
* Version 2.2 - 2018-12-27: Heat Index calculation included in sketch (based on Adafruit official library) - Tiberio Galletti
*
* DESCRIPTION
* This sketch provides an example of how to implement a humidity/temperature sensor using a DHT11/DHT21/DHT22.
* It inlcudes Heat Index *sensor*
*
* For more information, please visit:
* http://www.mysensors.org/build/TempHumFeel-DHT
*
*/
#define SN "TempHumFeel"
#define SV "2.2"
// Enable debug prints
//#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_RF24
//#define MY_RADIO_RFM69
//#define MY_RS485
//Uncomment (and update) if you want to force Node Id
//#define MY_NODE_ID 1
#define MY_BAUD_RATE 38400
// Uncomment the type of sensor in use:
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Set this to the pin you connected the DHT's data and power pins to; connect wires in coherent pins
#define DHTDATAPIN 3
#define DHTPOWERPIN 8
// Sleep time between sensor updates (in milliseconds) to add to sensor delay (read from sensor data; typically: 1s)
static const uint64_t UPDATE_INTERVAL = 60000;
// Force sending an update of the temperature after n sensor reads, so a controller showing the
// timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
// the value didn't change since;
// i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
static const uint8_t FORCE_UPDATE_N_READS = 10;
#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1
#define CHILD_ID_HEATINDEX 2
// Set this offset if the sensors have permanent small offsets to the real temperatures/humidity.
// In Celsius degrees or moisture percent
#define SENSOR_HUM_OFFSET 0 // used for temperature data and heat index computation
#define SENSOR_TEMP_OFFSET 0 // used for humidity data
#define SENSOR_HEATINDEX_OFFSET 0 // used for heat index data
// used libraries: they have to be installed by Arduino IDE (menu path: tools - manage libraries)
#include <MySensors.h> // *MySensors* by The MySensors Team (tested on version 2.3.2)
#include <Adafruit_Sensor.h> // Official "Adafruit Unified Sensor" by Adafruit (tested on version 1.1.1)
#include <DHT_U.h> // Official *DHT Sensor library* by Adafruit (tested on version 1.3.8)
DHT_Unified dhtu(DHTDATAPIN, DHTTYPE);
// See guide for details on Adafruit sensor wiring and usage:
// https://learn.adafruit.com/dht/overview
uint32_t delayMS;
float lastTemp;
float lastHum;
uint8_t nNoUpdates = FORCE_UPDATE_N_READS; // send data on start-up
bool metric = true;
float temperature;
float humidity;
float heatindex;
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
MyMessage msgHeatIndex(CHILD_ID_HEATINDEX, V_TEMP);
float computeHeatIndex(float temperature, float percentHumidity) {
// Based on Adafruit DHT official library (https://github.com/adafruit/DHT-sensor-library/blob/master/DHT.cpp)
// Using both Rothfusz and Steadman's equations
// http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
float hi;
temperature = temperature + SENSOR_TEMP_OFFSET; //include TEMP_OFFSET in HeatIndex computation too
temperature = 1.8*temperature+32; //convertion to *F
hi = 0.5 * (temperature + 61.0 + ((temperature - 68.0) * 1.2) + (percentHumidity * 0.094));
if (hi > 79) {
hi = -42.379 +
2.04901523 * temperature +
10.14333127 * percentHumidity +
-0.22475541 * temperature*percentHumidity +
-0.00683783 * pow(temperature, 2) +
-0.05481717 * pow(percentHumidity, 2) +
0.00122874 * pow(temperature, 2) * percentHumidity +
0.00085282 * temperature*pow(percentHumidity, 2) +
-0.00000199 * pow(temperature, 2) * pow(percentHumidity, 2);
if((percentHumidity < 13) && (temperature >= 80.0) && (temperature <= 112.0))
hi -= ((13.0 - percentHumidity) * 0.25) * sqrt((17.0 - abs(temperature - 95.0)) * 0.05882);
else if((percentHumidity > 85.0) && (temperature >= 80.0) && (temperature <= 87.0))
hi += ((percentHumidity - 85.0) * 0.1) * ((87.0 - temperature) * 0.2);
}
hi = (hi-32)/1.8;
return hi; //return Heat Index, in *C
}
void presentation()
{
// Send the sketch version information to the gateway
sendSketchInfo(SN, SV);
// Register all sensors to gw (they will be created as child devices)
present(CHILD_ID_HUM, S_HUM, "Humidity");
wait(100); //to check: is it needed
present(CHILD_ID_TEMP, S_TEMP, "Temperature");
wait(100); //to check: is it needed
present(CHILD_ID_HEATINDEX, S_TEMP, "Heat Index");
metric = getControllerConfig().isMetric;
}
void setup()
{
pinMode(DHTPOWERPIN, OUTPUT);
digitalWrite(DHTPOWERPIN, HIGH);
//Serial.begin(9600);
// Initialize device.
dhtu.begin();
Serial.println("DHTxx Unified Sensor Example");
// Print temperature sensor details.
sensor_t sensor;
dhtu.temperature().getSensor(&sensor);
Serial.println("------------------------------------");
Serial.println("Temperature");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" *C");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" *C");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" *C");
Serial.print ("Min Delay: "); Serial.print(sensor.min_delay/1000); Serial.println(" ms");
Serial.println("------------------------------------");
// Print humidity sensor details.
dhtu.humidity().getSensor(&sensor);
Serial.println("------------------------------------");
Serial.println("Humidity");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%");
Serial.print ("Min Delay: "); Serial.print(sensor.min_delay/1000); Serial.println(" ms");
Serial.println("------------------------------------");
// Set delay between sensor readings based on sensor details.
delayMS = sensor.min_delay / 1000;
}
void loop()
{
digitalWrite(DHTPOWERPIN, HIGH);
delay(delayMS);
sensors_event_t event;
// Get temperature event and use its value.
dhtu.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println("Error reading temperature!");
}
else {
temperature = event.temperature;
#ifdef MY_DEBUG
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
#endif
}
// Get humidity event and use its value.
dhtu.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println("Error reading humidity!");
}
else {
humidity = event.relative_humidity;
#ifdef MY_DEBUG
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
#endif
}
if (fabs(humidity - lastHum)>=0.05 || fabs(temperature - lastTemp)>=0.05 || nNoUpdates >= FORCE_UPDATE_N_READS) {
lastTemp = temperature;
lastHum = humidity;
heatindex = computeHeatIndex(temperature,humidity); //computes Heat Index, in *C
nNoUpdates = 0; // Reset no updates counter
#ifdef MY_DEBUG
Serial.print("Heat Index: ");
Serial.print(heatindex);
Serial.println(" *C");
#endif
if (!metric) {
temperature = 1.8*temperature+32; //convertion to *F
heatindex = 1.8*heatindex+32; //convertion to *F
}
#ifdef MY_DEBUG
wait(100);
Serial.print("Sending temperature: ");
Serial.print(temperature);
#endif
send(msgTemp.set(temperature + SENSOR_TEMP_OFFSET, 2));
#ifdef MY_DEBUG
wait(100);
Serial.print("Sending humidity: ");
Serial.print(humidity);
#endif
send(msgHum.set(humidity + SENSOR_HUM_OFFSET, 2));
#ifdef MY_DEBUG
wait(100);
Serial.print("Sending HeatIndex: ");
Serial.print(heatindex);
#endif
send(msgHeatIndex.set(heatindex + SENSOR_HEATINDEX_OFFSET, 2));
}
nNoUpdates++;
// Sleep for a while to save energy
digitalWrite(DHTPOWERPIN, LOW);
wait(300); // waiting for potential presentation requests
sleep(UPDATE_INTERVAL);
}