From df306b8dc4a5029addddc524625d2d691c555010 Mon Sep 17 00:00:00 2001
From: user2684 <user2684@users.noreply.github.com>
Date: Sat, 29 Jul 2017 14:58:14 +0200
Subject: [PATCH] DHT library update and fixes (#175, #145, #176, #148)

---
 NodeManager.cpp | 23 +++++++++++------------
 NodeManager.h   |  5 ++---
 README.md       |  5 +++--
 3 files changed, 16 insertions(+), 17 deletions(-)

diff --git a/NodeManager.cpp b/NodeManager.cpp
index 8012987..d437b12 100755
--- a/NodeManager.cpp
+++ b/NodeManager.cpp
@@ -1268,22 +1268,23 @@ SensorDHT::SensorDHT(NodeManager* node_manager, int child_id, int pin, DHT* dht,
 
 // what to do during before
 void SensorDHT::onBefore() {
-    // initialize the dht library
-    _dht->begin();
 }
 
 // what to do during setup
 void SensorDHT::onSetup() {
+  // initialize the dht library
+  _dht->setup(_pin,_dht_type);
 }
 
 // what to do during loop
 void SensorDHT::onLoop() {
+  wait(_dht->getMinimumSamplingPeriod());
+  _dht->readSensor(true);
   // temperature sensor
   if (_sensor_type == SensorDHT::TEMPERATURE) {
     // read the temperature
-    float temperature = _dht->readTemperature();
-    // convert it
-    temperature = _node_manager->celsiusToFahrenheit(temperature);
+    float temperature = _dht->getTemperature();
+    if (! _node_manager->getIsMetric()) temperature = _dht->toFahrenheit(temperature);
     #if DEBUG == 1
       Serial.print(F("DHT I="));
       Serial.print(_child_id);
@@ -1296,8 +1297,7 @@ void SensorDHT::onLoop() {
   // humidity sensor
   else if (_sensor_type == SensorDHT::HUMIDITY) {
     // read humidity
-    float humidity = _dht->readHumidity();
-    if (isnan(humidity)) return;
+    float humidity = _dht->getHumidity();
     #if DEBUG == 1
       Serial.print(F("DHT I="));
       Serial.print(_child_id);
@@ -2965,12 +2965,11 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) {
     else if (sensor_type == SENSOR_LATCHING_RELAY) return registerSensor(new SensorLatchingRelay(this,child_id, pin));
   #endif
   #if MODULE_DHT == 1
-    else if (sensor_type == SENSOR_DHT11 || sensor_type == SENSOR_DHT22 || sensor_type == SENSOR_DHT21) {
+    else if (sensor_type == SENSOR_DHT11 || sensor_type == SENSOR_DHT22) {
       int dht_type;
-      if (sensor_type == SENSOR_DHT11) dht_type = DHT11;
-      else if (sensor_type == SENSOR_DHT21) dht_type = DHT21;
-      else if (sensor_type == SENSOR_DHT22) dht_type = DHT22;
-      DHT* dht = new DHT(pin,dht_type);
+      if (sensor_type == SENSOR_DHT11) dht_type = DHT::DHT11;
+      else if (sensor_type == SENSOR_DHT22) dht_type = DHT::DHT22;
+      DHT* dht = new DHT();
       // register temperature sensor
       registerSensor(new SensorDHT(this,child_id,pin,dht,SensorDHT::TEMPERATURE,dht_type));
       // register humidity sensor
diff --git a/NodeManager.h b/NodeManager.h
index 8728831..5314483 100755
--- a/NodeManager.h
+++ b/NodeManager.h
@@ -114,7 +114,7 @@
 #ifndef MODULE_DIGITAL_OUTPUT
   #define MODULE_DIGITAL_OUTPUT 0
 #endif
-// Enable this module to use one of the following sensors: SENSOR_DHT11, SENSOR_DHT22, SENSOR_DHT21
+// Enable this module to use one of the following sensors: SENSOR_DHT11, SENSOR_DHT22
 #ifndef MODULE_DHT
   #define MODULE_DHT 0
 #endif
@@ -221,7 +221,6 @@ enum supported_sensors {
     // DHT11/DHT22 sensors, return temperature/humidity based on the attached DHT sensor
     SENSOR_DHT11,
     SENSOR_DHT22,
-    SENSOR_DHT21,
   #endif
   #if MODULE_SHT21 == 1
     // SHT21 sensor, return temperature/humidity based on the attached SHT21 sensor
@@ -827,7 +826,7 @@ class SensorDHT: public Sensor {
     const static int HUMIDITY = 1;
   protected:
     DHT* _dht;
-    int _dht_type = DHT11;
+    int _dht_type;
     float _offset = 0;
     int _sensor_type = 0;
 };
diff --git a/README.md b/README.md
index daf0971..15d2b57 100755
--- a/README.md
+++ b/README.md
@@ -152,7 +152,7 @@ The next step is to enable NodeManager's additional functionalities and the modu
 #define MODULE_DIGITAL_INPUT 0
 // Enable this module to use one of the following sensors: SENSOR_DIGITAL_OUTPUT, SENSOR_RELAY, SENSOR_LATCHING_RELAY
 #define MODULE_DIGITAL_OUTPUT 0
-// Enable this module to use one of the following sensors: SENSOR_DHT11, SENSOR_DHT22, SENSOR_DHT21
+// Enable this module to use one of the following sensors: SENSOR_DHT11, SENSOR_DHT22
 #define MODULE_DHT 0
 // Enable this module to use one of the following sensors: SENSOR_SHT21
 #define MODULE_SHT21 0
@@ -196,7 +196,7 @@ Some of the modules above rely on third party libraries. Those libraries are not
 Module  | Required Library
  ------------- | -------------
 MODULE_SHT21 | https://github.com/SodaqMoja/Sodaq_SHT2x
-MODULE_DHT | https://github.com/adafruit/DHT-sensor-library
+MODULE_DHT | https://github.com/mysensors/MySensorsArduinoExamples/tree/master/libraries/DHT
 MODULE_DS18B20 | https://github.com/milesburton/Arduino-Temperature-Control-Library
 MODULE_BH1750 | https://github.com/claws/BH1750
 MODULE_MLX90614 | https://github.com/adafruit/Adafruit-MLX90614-Library
@@ -1325,3 +1325,4 @@ v1.6:
 * SensorMQ now depends on its own module
 * Added automatic off capability (safeguard) to SensorDigitalOutput
 * Any sensor can now access all NodeManager's functions
+* DHT sensor now using MySensors' DHT library
\ No newline at end of file
-- 
GitLab