Skip to content

DHT Temperature and Humidity plus Heat Index (feels like temperature) - using official Adafruit Library #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
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...
  • Loading branch information
cnerone authored Jan 6, 2020
commit ad5c76c12805eae2ac13f97d3abbcaf9c008ab56
292 changes: 292 additions & 0 deletions examples/TempHumFeel-DHT.ino
Original file line number Diff line number Diff line change
@@ -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 <[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
* 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.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);

}