|
| 1 | + |
| 2 | +/** |
| 3 | + * The MySensors Arduino library handles the wireless radio link and protocol |
| 4 | + * between your home built sensors/actuators and HA controller of choice. |
| 5 | + * The sensors forms a self healing radio network with optional repeaters. Each |
| 6 | + * repeater and gateway builds a routing tables in EEPROM which keeps track of the |
| 7 | + * network topology allowing messages to be routed to nodes. |
| 8 | + * |
| 9 | + * Created by Henrik Ekblad <[email protected]> |
| 10 | + * Copyright (C) 2013-2015 Sensnology AB |
| 11 | + * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors |
| 12 | + * |
| 13 | + * Documentation: http://www.mysensors.org |
| 14 | + * Support Forum: http://forum.mysensors.org |
| 15 | + * |
| 16 | + * This program is free software; you can redistribute it and/or |
| 17 | + * modify it under the terms of the GNU General Public License |
| 18 | + * version 2 as published by the Free Software Foundation. |
| 19 | + * |
| 20 | + ******************************* |
| 21 | + * |
| 22 | + * REVISION HISTORY |
| 23 | + * this sensor is based on air humiditidy sensor (http://www.mysensors.org/build/humidity) by Henrik EKblad and Torben Woltjen (mozzbozz) |
| 24 | + * |
| 25 | + * |
| 26 | + * Version 1.0 - 2018-09-25: Converted to DHTU Adafruit library - Tiberio Galletti |
| 27 | + * Version 1.1 - 2018-10-06: Clearer code and... if something changed... every sensor data are sent to gateway - Tiberio Galletti |
| 28 | + * Version 1.2 - 2018-12-27: Heat Index calculation included in sketch code (based on Adafruit official library) - Tiberio Galletti |
| 29 | + * |
| 30 | + * DESCRIPTION |
| 31 | + * This sketch provides an example of how to implement a humidity/temperature sensor using a DHT11/DHT21/DHT22. |
| 32 | + * It includes Heat Index *sensor* |
| 33 | + * |
| 34 | + * For more information, please visit: |
| 35 | + * http://www.mysensors.org/build/TempHumFeel-DHT |
| 36 | + * |
| 37 | + */ |
| 38 | +#define SN "TempHumFeel" |
| 39 | +#define SV "1.2" |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +// Enable debug prints |
| 44 | +//#define MY_DEBUG |
| 45 | + |
| 46 | +// Enable and select radio type attached |
| 47 | +#define MY_RADIO_RF24 |
| 48 | +//#define MY_RADIO_RFM69 |
| 49 | +//#define MY_RS485 |
| 50 | + |
| 51 | +//Uncomment (and update) if you want to force Node Id |
| 52 | +//#define MY_NODE_ID 1 |
| 53 | + |
| 54 | +#define MY_BAUD_RATE 38400 |
| 55 | + |
| 56 | +// Uncomment the type of sensor in use: |
| 57 | +//#define DHTTYPE DHT11 // DHT 11 |
| 58 | +#define DHTTYPE DHT22 // DHT 22 (AM2302) |
| 59 | +//#define DHTTYPE DHT21 // DHT 21 (AM2301) |
| 60 | + |
| 61 | + |
| 62 | +// Set this to the pin you connected the DHT's data and power pins to; connect wires in coherent pins |
| 63 | +#define DHTDATAPIN 3 |
| 64 | +#define DHTPOWERPIN 8 |
| 65 | + |
| 66 | + |
| 67 | +// Sleep time between sensor updates (in milliseconds) to add to sensor delay (read from sensor data; typically: 1s) |
| 68 | +static const uint64_t UPDATE_INTERVAL = 60000; |
| 69 | + |
| 70 | +// Force sending an update of the temperature after n sensor reads, so a controller showing the |
| 71 | +// timestamp of the last update doesn't show something like 3 hours in the unlikely case, that |
| 72 | +// the value didn't change since; |
| 73 | +// i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms] |
| 74 | +static const uint8_t FORCE_UPDATE_N_READS = 10; |
| 75 | + |
| 76 | +#define CHILD_ID_HUM 0 |
| 77 | +#define CHILD_ID_TEMP 1 |
| 78 | +#define CHILD_ID_HEATINDEX 2 |
| 79 | + |
| 80 | +// Set this offset if the sensors have permanent small offsets to the real temperatures/humidity. |
| 81 | +// In Celsius degrees or moisture percent |
| 82 | +#define SENSOR_HUM_OFFSET 0 // used for temperature data and heat index computation |
| 83 | +#define SENSOR_TEMP_OFFSET 0 // used for humidity data |
| 84 | +#define SENSOR_HEATINDEX_OFFSET 0 // used for heat index data |
| 85 | + |
| 86 | + |
| 87 | +// used libraries: they have to be installed by Arduino IDE (menu path: tools - manage libraries) |
| 88 | +#include <MySensors.h> // *MySensors* by The MySensors Team (tested on version 2.3.2) |
| 89 | +#include <Adafruit_Sensor.h> // Official "Adafruit Unified Sensor" by Adafruit (tested on version 1.1.1) |
| 90 | +#include <DHT_U.h> // Official *DHT Sensor library* by Adafruit (tested on version 1.3.8) |
| 91 | + |
| 92 | +DHT_Unified dhtu(DHTDATAPIN, DHTTYPE); |
| 93 | +// See guide for details on Adafruit sensor wiring and usage: |
| 94 | +// https://learn.adafruit.com/dht/overview |
| 95 | + |
| 96 | +uint32_t delayMS; |
| 97 | +float lastTemp; |
| 98 | +float lastHum; |
| 99 | +uint8_t nNoUpdates = FORCE_UPDATE_N_READS; // send data on start-up |
| 100 | +bool metric = true; |
| 101 | +float temperature; |
| 102 | +float humidity; |
| 103 | +float heatindex; |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +MyMessage msgHum(CHILD_ID_HUM, V_HUM); |
| 108 | +MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); |
| 109 | +MyMessage msgHeatIndex(CHILD_ID_HEATINDEX, V_TEMP); |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +float computeHeatIndex(float temperature, float percentHumidity) { |
| 114 | + // Based on Adafruit DHT official library (https://github.com/adafruit/DHT-sensor-library/blob/master/DHT.cpp) |
| 115 | + // Using both Rothfusz and Steadman's equations |
| 116 | + // http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml |
| 117 | + |
| 118 | + float hi; |
| 119 | + |
| 120 | + temperature = temperature + SENSOR_TEMP_OFFSET; //include TEMP_OFFSET in HeatIndex computation too |
| 121 | + temperature = 1.8*temperature+32; //convertion to *F |
| 122 | + |
| 123 | + hi = 0.5 * (temperature + 61.0 + ((temperature - 68.0) * 1.2) + (percentHumidity * 0.094)); |
| 124 | + |
| 125 | + if (hi > 79) { |
| 126 | + hi = -42.379 + |
| 127 | + 2.04901523 * temperature + |
| 128 | + 10.14333127 * percentHumidity + |
| 129 | + -0.22475541 * temperature*percentHumidity + |
| 130 | + -0.00683783 * pow(temperature, 2) + |
| 131 | + -0.05481717 * pow(percentHumidity, 2) + |
| 132 | + 0.00122874 * pow(temperature, 2) * percentHumidity + |
| 133 | + 0.00085282 * temperature*pow(percentHumidity, 2) + |
| 134 | + -0.00000199 * pow(temperature, 2) * pow(percentHumidity, 2); |
| 135 | + |
| 136 | + if((percentHumidity < 13) && (temperature >= 80.0) && (temperature <= 112.0)) |
| 137 | + hi -= ((13.0 - percentHumidity) * 0.25) * sqrt((17.0 - abs(temperature - 95.0)) * 0.05882); |
| 138 | + |
| 139 | + else if((percentHumidity > 85.0) && (temperature >= 80.0) && (temperature <= 87.0)) |
| 140 | + hi += ((percentHumidity - 85.0) * 0.1) * ((87.0 - temperature) * 0.2); |
| 141 | + } |
| 142 | + |
| 143 | + hi = (hi-32)/1.8; |
| 144 | + return hi; //return Heat Index, in *C |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | +void presentation() |
| 153 | +{ |
| 154 | + // Send the sketch version information to the gateway |
| 155 | + sendSketchInfo(SN, SV); |
| 156 | + // Register all sensors to gw (they will be created as child devices) |
| 157 | + present(CHILD_ID_HUM, S_HUM, "Humidity"); |
| 158 | + wait(100); //to check: is it needed |
| 159 | + present(CHILD_ID_TEMP, S_TEMP, "Temperature"); |
| 160 | + wait(100); //to check: is it needed |
| 161 | + present(CHILD_ID_HEATINDEX, S_TEMP, "Heat Index"); |
| 162 | + metric = getControllerConfig().isMetric; |
| 163 | +} |
| 164 | + |
| 165 | + |
| 166 | +void setup() |
| 167 | +{ |
| 168 | + pinMode(DHTPOWERPIN, OUTPUT); |
| 169 | + digitalWrite(DHTPOWERPIN, HIGH); |
| 170 | +//Serial.begin(9600); |
| 171 | + // Initialize device. |
| 172 | + dhtu.begin(); |
| 173 | + |
| 174 | + |
| 175 | + Serial.println("DHTxx Unified Sensor Example"); |
| 176 | + // Print temperature sensor details. |
| 177 | + sensor_t sensor; |
| 178 | + dhtu.temperature().getSensor(&sensor); |
| 179 | + Serial.println("------------------------------------"); |
| 180 | + Serial.println("Temperature"); |
| 181 | + Serial.print ("Sensor: "); Serial.println(sensor.name); |
| 182 | + Serial.print ("Driver Ver: "); Serial.println(sensor.version); |
| 183 | + Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); |
| 184 | + Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" *C"); |
| 185 | + Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" *C"); |
| 186 | + Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" *C"); |
| 187 | + Serial.print ("Min Delay: "); Serial.print(sensor.min_delay/1000); Serial.println(" ms"); |
| 188 | + Serial.println("------------------------------------"); |
| 189 | + |
| 190 | + // Print humidity sensor details. |
| 191 | + dhtu.humidity().getSensor(&sensor); |
| 192 | + Serial.println("------------------------------------"); |
| 193 | + Serial.println("Humidity"); |
| 194 | + Serial.print ("Sensor: "); Serial.println(sensor.name); |
| 195 | + Serial.print ("Driver Ver: "); Serial.println(sensor.version); |
| 196 | + Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); |
| 197 | + Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%"); |
| 198 | + Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%"); |
| 199 | + Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%"); |
| 200 | + Serial.print ("Min Delay: "); Serial.print(sensor.min_delay/1000); Serial.println(" ms"); |
| 201 | + Serial.println("------------------------------------"); |
| 202 | + // Set delay between sensor readings based on sensor details. |
| 203 | + delayMS = sensor.min_delay / 1000; |
| 204 | +} |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | +void loop() |
| 214 | +{ |
| 215 | + digitalWrite(DHTPOWERPIN, HIGH); |
| 216 | + delay(delayMS); |
| 217 | + sensors_event_t event; |
| 218 | + // Get temperature event and use its value. |
| 219 | + dhtu.temperature().getEvent(&event); |
| 220 | + if (isnan(event.temperature)) { |
| 221 | + Serial.println("Error reading temperature!"); |
| 222 | + } |
| 223 | + else { |
| 224 | + temperature = event.temperature; |
| 225 | + #ifdef MY_DEBUG |
| 226 | + Serial.print("Temperature: "); |
| 227 | + Serial.print(temperature); |
| 228 | + Serial.println(" *C"); |
| 229 | + #endif |
| 230 | + } |
| 231 | + |
| 232 | + // Get humidity event and use its value. |
| 233 | + dhtu.humidity().getEvent(&event); |
| 234 | + if (isnan(event.relative_humidity)) { |
| 235 | + Serial.println("Error reading humidity!"); |
| 236 | + } |
| 237 | + else { |
| 238 | + humidity = event.relative_humidity; |
| 239 | + #ifdef MY_DEBUG |
| 240 | + Serial.print("Humidity: "); |
| 241 | + Serial.print(humidity); |
| 242 | + Serial.println("%"); |
| 243 | + #endif |
| 244 | + } |
| 245 | + |
| 246 | +if (fabs(humidity - lastHum)>=0.05 || fabs(temperature - lastTemp)>=0.05 || nNoUpdates >= FORCE_UPDATE_N_READS) { |
| 247 | + lastTemp = temperature; |
| 248 | + lastHum = humidity; |
| 249 | + heatindex = computeHeatIndex(temperature,humidity); //computes Heat Index, in *C |
| 250 | + nNoUpdates = 0; // Reset no updates counter |
| 251 | + #ifdef MY_DEBUG |
| 252 | + Serial.print("Heat Index: "); |
| 253 | + Serial.print(heatindex); |
| 254 | + Serial.println(" *C"); |
| 255 | + #endif |
| 256 | + |
| 257 | + if (!metric) { |
| 258 | + temperature = 1.8*temperature+32; //convertion to *F |
| 259 | + heatindex = 1.8*heatindex+32; //convertion to *F |
| 260 | + } |
| 261 | + |
| 262 | + #ifdef MY_DEBUG |
| 263 | + wait(100); |
| 264 | + Serial.print("Sending temperature: "); |
| 265 | + Serial.print(temperature); |
| 266 | + #endif |
| 267 | + send(msgTemp.set(temperature + SENSOR_TEMP_OFFSET, 2)); |
| 268 | + |
| 269 | + #ifdef MY_DEBUG |
| 270 | + wait(100); |
| 271 | + Serial.print("Sending humidity: "); |
| 272 | + Serial.print(humidity); |
| 273 | + #endif |
| 274 | + send(msgHum.set(humidity + SENSOR_HUM_OFFSET, 2)); |
| 275 | + |
| 276 | + #ifdef MY_DEBUG |
| 277 | + wait(100); |
| 278 | + Serial.print("Sending HeatIndex: "); |
| 279 | + Serial.print(heatindex); |
| 280 | + #endif |
| 281 | + send(msgHeatIndex.set(heatindex + SENSOR_HEATINDEX_OFFSET, 2)); |
| 282 | + |
| 283 | + } |
| 284 | + |
| 285 | + nNoUpdates++; |
| 286 | + |
| 287 | + // Sleep for a while to save energy |
| 288 | + digitalWrite(DHTPOWERPIN, LOW); |
| 289 | + wait(300); // waiting for potential presentation requests |
| 290 | + sleep(UPDATE_INTERVAL); |
| 291 | + |
| 292 | +} |
0 commit comments