From 922cdaaff772db3a59aef8d784d4ef4e43c9a50a Mon Sep 17 00:00:00 2001 From: Tiberio Date: Thu, 27 Dec 2018 10:23:13 +0100 Subject: [PATCH 1/9] DHT - Using official Adafruit Library, included Heat Index (feels like) sensor --- .../DhtTemperatureAndHumiditySensor.ino | 277 +++++++++++++----- 1 file changed, 206 insertions(+), 71 deletions(-) diff --git a/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino b/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino index 2fd8e63..daf9f62 100644 --- a/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino +++ b/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino @@ -1,3 +1,4 @@ + /** * The MySensors Arduino library handles the wireless radio link and protocol * between your home built sensors/actuators and HA controller of choice. @@ -21,38 +22,49 @@ * REVISION HISTORY * Version 1.0: Henrik EKblad * Version 1.1 - 2016-07-20: Converted to MySensors v2.0 and added various improvements - Torben Woltjen (mozzbozz) + * Version 2.0 - 2018-09-25: Converted to DHTU Adafruit library + * Version 2.1 - 2018-10-06: Clearer code and... if something changed... every sensor data are sent to gateway + * Version 2.2 - 2018-12-27: Heat Index calculation included in sketch (based on Adafruit official library) * * DESCRIPTION - * This sketch provides an example of how to implement a humidity/temperature - * sensor using a DHT11/DHT-22. + * 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/humidity + * http://www.mysensors.org/build/TemperatureAndHumidity * */ +#define SN "TemperatureAndHumidity" +#define SV "2.2" + + // Enable debug prints -#define MY_DEBUG +//#define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 //#define MY_RS485 - -#include -#include -#include -// Set this to the pin you connected the DHT's data pin to -#define DHT_DATA_PIN 3 +//Uncomment (and update) if you want to force Node Id +//#define MY_NODE_ID 1 + +#define MY_BAUD_RATE 38400 -// Set this offset if the sensor has a permanent small offset to the real temperatures. -// In Celsius degrees (as measured by the device) -#define SENSOR_TEMP_OFFSET 0 +// Uncomment the type of sensor in use: +//#define DHTTYPE DHT11 // DHT 11 +#define DHTTYPE DHT22 // DHT 22 (AM2302) +//#define DHTTYPE DHT21 // DHT 21 (AM2301) -// Sleep time between sensor updates (in milliseconds) -// Must be >1000ms for DHT22 and >2000ms for DHT11 -static const uint64_t UPDATE_INTERVAL = 60000; + +// 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; tipically: 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 @@ -62,95 +74,218 @@ static const uint8_t FORCE_UPDATE_N_READS = 10; #define CHILD_ID_HUM 0 #define CHILD_ID_TEMP 1 +#define CHILD_ID_TEMP_HI 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_TEMP_HI_OFFSET 0 // used for heat index data + + +#include +#include +#include + +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 nNoUpdatesTemp; -uint8_t nNoUpdatesHum; +uint8_t nNoUpdates = FORCE_UPDATE_N_READS; // send data on start-up bool metric = true; +float temperature; +float humidity; +float temperatureHI; + + MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); -DHT dht; +MyMessage msgTempHI(CHILD_ID_TEMP_HI, 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("TemperatureAndHumidity", "1.1"); - + sendSketchInfo(SN, SV); // Register all sensors to gw (they will be created as child devices) - present(CHILD_ID_HUM, S_HUM); - present(CHILD_ID_TEMP, S_TEMP); - + present(CHILD_ID_HUM, S_HUM, "Umidity"); + wait(100); //to check: is it needed + present(CHILD_ID_TEMP, S_TEMP, "Temperature"); + wait(100); //to check: is it needed + present(CHILD_ID_TEMP_HI, S_TEMP, "Heat Index"); metric = getControllerConfig().isMetric; } void setup() { - dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor - if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) { - Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!"); - } - // Sleep for the time of the minimum sampling period to give the sensor time to power up - // (otherwise, timeout errors might occure for the first reading) - sleep(dht.getMinimumSamplingPeriod()); + 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() -{ - // Force reading sensor, so it works also after sleep() - dht.readSensor(true); - - // Get temperature from DHT library - float temperature = dht.getTemperature(); - if (isnan(temperature)) { - Serial.println("Failed reading temperature from DHT!"); - } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) { - // Only send temperature if it changed since the last measurement or if we didn't send an update for n times - lastTemp = temperature; - // apply the offset before converting to something different than Celsius degrees - temperature += SENSOR_TEMP_OFFSET; - if (!metric) { - temperature = dht.toFahrenheit(temperature); - } - // Reset no updates counter - nNoUpdatesTemp = 0; - send(msgTemp.set(temperature, 1)); + + + +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("T: "); - Serial.println(temperature); + Serial.print("Humidity: "); + Serial.print(humidity); + Serial.println("%"); #endif - } else { - // Increase no update counter if the temperature stayed the same - nNoUpdatesTemp++; } - // Get humidity from DHT library - float humidity = dht.getHumidity(); - if (isnan(humidity)) { - Serial.println("Failed reading humidity from DHT"); - } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) { - // Only send humidity if it changed since the last measurement or if we didn't send an update for n times +if (fabs(humidity - lastHum)>=0.05 || fabs(temperature - lastTemp)>=0.05 || nNoUpdates >= FORCE_UPDATE_N_READS) { + lastTemp = temperature; lastHum = humidity; - // Reset no updates counter - nNoUpdatesHum = 0; - send(msgHum.set(humidity, 1)); + temperatureHI = computeHeatIndex(temperature,humidity); //computes Heat Index, in *C + nNoUpdates = 0; // Reset no updates counter + #ifdef MY_DEBUG + Serial.print("Heat Index: "); + Serial.print(temperatureHI); + Serial.println(" *C"); + #endif + + if (!metric) { + temperature = 1.8*temperature+32; //convertion to *F + temperatureHI = 1.8*temperatureHI+32; //convertion to *F + } #ifdef MY_DEBUG - Serial.print("H: "); - Serial.println(humidity); - #endif - } else { - // Increase no update counter if the humidity stayed the same - nNoUpdatesHum++; + 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(temperatureHI); + #endif + send(msgTempHI.set(temperatureHI + SENSOR_TEMP_HI_OFFSET, 2)); + } + nNoUpdates++; + // Sleep for a while to save energy + digitalWrite(DHTPOWERPIN, LOW); + wait(300); // waiting for potential presentation requests sleep(UPDATE_INTERVAL); + } From 11b1da8e7e50c89eda36900f4f79e57aa563d3b7 Mon Sep 17 00:00:00 2001 From: cnerone Date: Sat, 4 Jan 2020 23:21:22 +0100 Subject: [PATCH 2/9] Update DhtTemperatureAndHumiditySensor.ino --- .../DhtTemperatureAndHumiditySensor.ino | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino b/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino index daf9f62..8f267f3 100644 --- a/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino +++ b/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino @@ -63,7 +63,7 @@ #define DHTPOWERPIN 8 -// Sleep time between sensor updates (in milliseconds) to add to sensor delay (read from sensor data; tipically: 1s) +// 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 @@ -74,13 +74,13 @@ static const uint8_t FORCE_UPDATE_N_READS = 10; #define CHILD_ID_HUM 0 #define CHILD_ID_TEMP 1 -#define CHILD_ID_TEMP_HI 2 +#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_TEMP_HI_OFFSET 0 // used for heat index data +#define SENSOR_HEATINDEX_OFFSET 0 // used for heat index data @@ -99,13 +99,13 @@ uint8_t nNoUpdates = FORCE_UPDATE_N_READS; // send data on start-up bool metric = true; float temperature; float humidity; -float temperatureHI; +float heatindex; MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); -MyMessage msgTempHI(CHILD_ID_TEMP_HI, V_TEMP); +MyMessage msgHeatIndex(CHILD_ID_HEATINDEX, V_TEMP); @@ -153,11 +153,11 @@ 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, "Umidity"); + 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_TEMP_HI, S_TEMP, "Heat Index"); + present(CHILD_ID_HEATINDEX, S_TEMP, "Heat Index"); metric = getControllerConfig().isMetric; } @@ -245,17 +245,17 @@ void loop() if (fabs(humidity - lastHum)>=0.05 || fabs(temperature - lastTemp)>=0.05 || nNoUpdates >= FORCE_UPDATE_N_READS) { lastTemp = temperature; lastHum = humidity; - temperatureHI = computeHeatIndex(temperature,humidity); //computes Heat Index, in *C + heatindex = computeHeatIndex(temperature,humidity); //computes Heat Index, in *C nNoUpdates = 0; // Reset no updates counter #ifdef MY_DEBUG Serial.print("Heat Index: "); - Serial.print(temperatureHI); + Serial.print(heatindex); Serial.println(" *C"); #endif if (!metric) { temperature = 1.8*temperature+32; //convertion to *F - temperatureHI = 1.8*temperatureHI+32; //convertion to *F + heatindex = 1.8*heatindex+32; //convertion to *F } #ifdef MY_DEBUG @@ -275,9 +275,9 @@ if (fabs(humidity - lastHum)>=0.05 || fabs(temperature - lastTemp)>=0.05 || nNoU #ifdef MY_DEBUG wait(100); Serial.print("Sending HeatIndex: "); - Serial.print(temperatureHI); + Serial.print(heatindex); #endif - send(msgTempHI.set(temperatureHI + SENSOR_TEMP_HI_OFFSET, 2)); + send(msgHeatIndex.set(heatindex + SENSOR_HEATINDEX_OFFSET, 2)); } From 36849b0deeba501e32d6469e308a89812ffae595 Mon Sep 17 00:00:00 2001 From: cnerone Date: Sat, 4 Jan 2020 23:36:25 +0100 Subject: [PATCH 3/9] Update DhtTemperatureAndHumiditySensor.ino --- .../DhtTemperatureAndHumiditySensor.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino b/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino index 8f267f3..591c0b2 100644 --- a/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino +++ b/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino @@ -22,9 +22,9 @@ * REVISION HISTORY * Version 1.0: Henrik EKblad * Version 1.1 - 2016-07-20: Converted to MySensors v2.0 and added various improvements - Torben Woltjen (mozzbozz) - * Version 2.0 - 2018-09-25: Converted to DHTU Adafruit library - * Version 2.1 - 2018-10-06: Clearer code and... if something changed... every sensor data are sent to gateway - * Version 2.2 - 2018-12-27: Heat Index calculation included in sketch (based on Adafruit official library) + * 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. From 6974215cf77c338182f1dc982bf3000d644c5ac1 Mon Sep 17 00:00:00 2001 From: cnerone Date: Sun, 5 Jan 2020 22:21:59 +0100 Subject: [PATCH 4/9] Update DhtTemperatureAndHumiditySensor.ino --- .../DhtTemperatureAndHumiditySensor.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino b/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino index 591c0b2..cd01d24 100644 --- a/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino +++ b/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino @@ -31,7 +31,7 @@ * It inlcudes Heat Index *sensor* * * For more information, please visit: - * http://www.mysensors.org/build/TemperatureAndHumidity + * http://www.mysensors.org/build/humidity * */ #define SN "TemperatureAndHumidity" @@ -43,7 +43,7 @@ //#define MY_DEBUG // Enable and select radio type attached -#define MY_RADIO_NRF24 +#define MY_RADIO_RF24 //#define MY_RADIO_RFM69 //#define MY_RS485 @@ -85,8 +85,8 @@ static const uint8_t FORCE_UPDATE_N_READS = 10; #include -#include -#include +#include // Adafruit Unified Sensor +#include //DHT Sensor library by Adafruit DHT_Unified dhtu(DHTDATAPIN, DHTTYPE); // See guide for details on Adafruit sensor wiring and usage: From d723ca6406d945991d9df2fd7a40da939b4d62ec Mon Sep 17 00:00:00 2001 From: cnerone Date: Sun, 5 Jan 2020 22:29:29 +0100 Subject: [PATCH 5/9] Update DhtTemperatureAndHumiditySensor.ino --- .../DhtTemperatureAndHumiditySensor.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino b/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino index cd01d24..e10fdb0 100644 --- a/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino +++ b/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino @@ -86,7 +86,7 @@ static const uint8_t FORCE_UPDATE_N_READS = 10; #include #include // Adafruit Unified Sensor -#include //DHT Sensor library by Adafruit +#include // DHT Sensor library by Adafruit DHT_Unified dhtu(DHTDATAPIN, DHTTYPE); // See guide for details on Adafruit sensor wiring and usage: From 68d3db54589b40c1246aa41a62fc50806c42d6ce Mon Sep 17 00:00:00 2001 From: cnerone Date: Mon, 6 Jan 2020 11:38:26 +0100 Subject: [PATCH 6/9] Update DhtTemperatureAndHumiditySensor.ino --- .../DhtTemperatureAndHumiditySensor.ino | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino b/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino index e10fdb0..cf9932b 100644 --- a/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino +++ b/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino @@ -20,8 +20,8 @@ ******************************* * * REVISION HISTORY - * Version 1.0: Henrik EKblad - * Version 1.1 - 2016-07-20: Converted to MySensors v2.0 and added various improvements - Torben Woltjen (mozzbozz) + * Version 1.0 (umidity 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 @@ -31,10 +31,10 @@ * It inlcudes Heat Index *sensor* * * For more information, please visit: - * http://www.mysensors.org/build/humidity + * http://www.mysensors.org/build/TempHumFeel-DHT * */ -#define SN "TemperatureAndHumidity" +#define SN "TempHumFeel" #define SV "2.2" @@ -83,10 +83,10 @@ static const uint8_t FORCE_UPDATE_N_READS = 10; #define SENSOR_HEATINDEX_OFFSET 0 // used for heat index data - -#include -#include // Adafruit Unified Sensor -#include // DHT Sensor library by Adafruit +// used libraries: they have to be installed by Arduino IDE (menu path: tools - manage libraries) +#include // *MySensors* by The MySensors Team (tested on version 2.3.2) +#include // Official "Adafruit Unified Sensor" by Adafruit (tested on version 1.1.1) +#include // 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: From 93b79de556f896988a7a375c0a38ff17a767ede9 Mon Sep 17 00:00:00 2001 From: cnerone Date: Mon, 6 Jan 2020 11:38:55 +0100 Subject: [PATCH 7/9] Update DhtTemperatureAndHumiditySensor.ino --- .../DhtTemperatureAndHumiditySensor.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino b/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino index cf9932b..ddc1a11 100644 --- a/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino +++ b/examples/DhtTemperatureAndHumiditySensor/DhtTemperatureAndHumiditySensor.ino @@ -20,7 +20,7 @@ ******************************* * * REVISION HISTORY - * Version 1.0 (umidity sensor): Henrik EKblad + * 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 From ad5c76c12805eae2ac13f97d3abbcaf9c008ab56 Mon Sep 17 00:00:00 2001 From: cnerone Date: Mon, 6 Jan 2020 12:10:23 +0100 Subject: [PATCH 8/9] Create TempHumFeel-DHT.ino This is to large and complex sketch for newbie to overwrite DHT humidity sensor one, Mikael Falkvidd said and I agree. So I suggest to upload it as a different, more sophisticated example; indeed it could be incorrect to consider it as a simple Humidity sensor... --- examples/TempHumFeel-DHT.ino | 292 +++++++++++++++++++++++++++++++++++ 1 file changed, 292 insertions(+) create mode 100644 examples/TempHumFeel-DHT.ino diff --git a/examples/TempHumFeel-DHT.ino b/examples/TempHumFeel-DHT.ino new file mode 100644 index 0000000..d518978 --- /dev/null +++ b/examples/TempHumFeel-DHT.ino @@ -0,0 +1,292 @@ + +/** + * 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 + * 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 + * this sensor is based on air humiditidy sensor (http://www.mysensors.org/build/humidity) by Henrik EKblad and Torben Woltjen (mozzbozz) + * + * + * Version 1.0 - 2018-09-25: Converted to DHTU Adafruit library - Tiberio Galletti + * Version 1.1 - 2018-10-06: Clearer code and... if something changed... every sensor data are sent to gateway - Tiberio Galletti + * Version 1.2 - 2018-12-27: Heat Index calculation included in sketch code (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 includes Heat Index *sensor* + * + * For more information, please visit: + * http://www.mysensors.org/build/TempHumFeel-DHT + * + */ +#define SN "TempHumFeel" +#define SV "1.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* by The MySensors Team (tested on version 2.3.2) +#include // Official "Adafruit Unified Sensor" by Adafruit (tested on version 1.1.1) +#include // 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); + +} From c27d26ffdaacc5f9dfb0fae22e8b751e1910c66e Mon Sep 17 00:00:00 2001 From: cnerone Date: Mon, 6 Jan 2020 12:16:36 +0100 Subject: [PATCH 9/9] Rename examples/TempHumFeel-DHT.ino to examples/DhtTemperatureAndHumiditySensor/TempHumFeel-DHT.ino --- .../{ => DhtTemperatureAndHumiditySensor}/TempHumFeel-DHT.ino | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/{ => DhtTemperatureAndHumiditySensor}/TempHumFeel-DHT.ino (100%) diff --git a/examples/TempHumFeel-DHT.ino b/examples/DhtTemperatureAndHumiditySensor/TempHumFeel-DHT.ino similarity index 100% rename from examples/TempHumFeel-DHT.ino rename to examples/DhtTemperatureAndHumiditySensor/TempHumFeel-DHT.ino