forked from adafruit/DHT-sensor-library
Converted to C++ and esp-idf
This commit is contained in:
34
CMakeLists.txt
Normal file
34
CMakeLists.txt
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
set(headers
|
||||||
|
DHT.h
|
||||||
|
DHT_U.h
|
||||||
|
)
|
||||||
|
|
||||||
|
set(sources
|
||||||
|
DHT.cpp
|
||||||
|
DHT_U.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
set(dependencies
|
||||||
|
arduino-esp32
|
||||||
|
Adafruit_Sensor
|
||||||
|
)
|
||||||
|
|
||||||
|
idf_component_register(
|
||||||
|
INCLUDE_DIRS
|
||||||
|
.
|
||||||
|
SRCS
|
||||||
|
${headers}
|
||||||
|
${sources}
|
||||||
|
REQUIRES
|
||||||
|
${dependencies}
|
||||||
|
)
|
||||||
|
|
||||||
|
target_compile_options(${COMPONENT_TARGET}
|
||||||
|
PRIVATE
|
||||||
|
-fstack-reuse=all
|
||||||
|
-fstack-protector-all
|
||||||
|
-Wno-unused-function
|
||||||
|
-Wno-deprecated-declarations
|
||||||
|
-Wno-missing-field-initializers
|
||||||
|
-Wno-parentheses
|
||||||
|
)
|
38
DHT.cpp
38
DHT.cpp
@@ -66,7 +66,7 @@ void DHT::begin(uint8_t usec) {
|
|||||||
// Using this value makes sure that millis() - lastreadtime will be
|
// Using this value makes sure that millis() - lastreadtime will be
|
||||||
// >= MIN_INTERVAL right away. Note that this assignment wraps around,
|
// >= MIN_INTERVAL right away. Note that this assignment wraps around,
|
||||||
// but so will the subtraction.
|
// but so will the subtraction.
|
||||||
_lastreadtime = millis() - MIN_INTERVAL;
|
_lastreadtime = espchrono::millis_clock::now() - std::chrono::milliseconds{MIN_INTERVAL};
|
||||||
DEBUG_PRINT("DHT max clock cycles: ");
|
DEBUG_PRINT("DHT max clock cycles: ");
|
||||||
DEBUG_PRINTLN(_maxcycles, DEC);
|
DEBUG_PRINTLN(_maxcycles, DEC);
|
||||||
pullTime = usec;
|
pullTime = usec;
|
||||||
@@ -82,10 +82,12 @@ void DHT::begin(uint8_t usec) {
|
|||||||
* true if in force mode
|
* true if in force mode
|
||||||
* @return Temperature value in selected scale
|
* @return Temperature value in selected scale
|
||||||
*/
|
*/
|
||||||
float DHT::readTemperature(bool S, bool force) {
|
std::optional<float> DHT::readTemperature(bool S, bool force) {
|
||||||
|
if (!read(force))
|
||||||
|
return std::nullopt;
|
||||||
|
|
||||||
float f = NAN;
|
float f = NAN;
|
||||||
|
|
||||||
if (read(force)) {
|
|
||||||
switch (_type) {
|
switch (_type) {
|
||||||
case DHT11:
|
case DHT11:
|
||||||
f = data[2];
|
f = data[2];
|
||||||
@@ -118,8 +120,10 @@ float DHT::readTemperature(bool S, bool force) {
|
|||||||
f = convertCtoF(f);
|
f = convertCtoF(f);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,9 +149,11 @@ float DHT::convertFtoC(float f) { return (f - 32) * 0.55555; }
|
|||||||
* force read mode
|
* force read mode
|
||||||
* @return float value - humidity in percent
|
* @return float value - humidity in percent
|
||||||
*/
|
*/
|
||||||
float DHT::readHumidity(bool force) {
|
std::optional<float> DHT::readHumidity(bool force) {
|
||||||
|
if (!read(force))
|
||||||
|
return std::nullopt;
|
||||||
|
|
||||||
float f = NAN;
|
float f = NAN;
|
||||||
if (read(force)) {
|
|
||||||
switch (_type) {
|
switch (_type) {
|
||||||
case DHT11:
|
case DHT11:
|
||||||
case DHT12:
|
case DHT12:
|
||||||
@@ -158,7 +164,8 @@ float DHT::readHumidity(bool force) {
|
|||||||
f = ((word)data[0]) << 8 | data[1];
|
f = ((word)data[0]) << 8 | data[1];
|
||||||
f *= 0.1;
|
f *= 0.1;
|
||||||
break;
|
break;
|
||||||
}
|
default:
|
||||||
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
@@ -171,9 +178,16 @@ float DHT::readHumidity(bool force) {
|
|||||||
*(default true)
|
*(default true)
|
||||||
* @return float heat index
|
* @return float heat index
|
||||||
*/
|
*/
|
||||||
float DHT::computeHeatIndex(bool isFahrenheit) {
|
std::optional<float> DHT::computeHeatIndex(bool isFahrenheit) {
|
||||||
float hi = computeHeatIndex(readTemperature(isFahrenheit), readHumidity(),
|
const auto temperature = readTemperature(isFahrenheit);
|
||||||
isFahrenheit);
|
if (!temperature)
|
||||||
|
return std::nullopt;
|
||||||
|
|
||||||
|
const auto humidity = readHumidity();
|
||||||
|
if (!humidity)
|
||||||
|
return std::nullopt;
|
||||||
|
|
||||||
|
float hi = computeHeatIndex(*temperature, *humidity, isFahrenheit);
|
||||||
return hi;
|
return hi;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,8 +245,8 @@ float DHT::computeHeatIndex(float temperature, float percentHumidity,
|
|||||||
bool DHT::read(bool force) {
|
bool DHT::read(bool force) {
|
||||||
// Check if sensor was read less than two seconds ago and return early
|
// Check if sensor was read less than two seconds ago and return early
|
||||||
// to use last reading.
|
// to use last reading.
|
||||||
uint32_t currenttime = millis();
|
espchrono::millis_clock::time_point currenttime = espchrono::millis_clock::now();
|
||||||
if (!force && ((currenttime - _lastreadtime) < MIN_INTERVAL)) {
|
if (!force && ((currenttime - _lastreadtime) < std::chrono::milliseconds{MIN_INTERVAL})) {
|
||||||
return _lastresult; // return last correct measurement
|
return _lastresult; // return last correct measurement
|
||||||
}
|
}
|
||||||
_lastreadtime = currenttime;
|
_lastreadtime = currenttime;
|
||||||
|
13
DHT.h
13
DHT.h
@@ -20,6 +20,10 @@
|
|||||||
|
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
|
#include <espchrono.h>
|
||||||
|
|
||||||
/* Uncomment to enable printing out nice debug messages. */
|
/* Uncomment to enable printing out nice debug messages. */
|
||||||
//#define DHT_DEBUG
|
//#define DHT_DEBUG
|
||||||
|
|
||||||
@@ -64,13 +68,13 @@ class DHT {
|
|||||||
public:
|
public:
|
||||||
DHT(uint8_t pin, uint8_t type, uint8_t count = 6);
|
DHT(uint8_t pin, uint8_t type, uint8_t count = 6);
|
||||||
void begin(uint8_t usec = 55);
|
void begin(uint8_t usec = 55);
|
||||||
float readTemperature(bool S = false, bool force = false);
|
std::optional<float> readTemperature(bool S = false, bool force = false);
|
||||||
float convertCtoF(float);
|
float convertCtoF(float);
|
||||||
float convertFtoC(float);
|
float convertFtoC(float);
|
||||||
float computeHeatIndex(bool isFahrenheit = true);
|
std::optional<float> computeHeatIndex(bool isFahrenheit = true);
|
||||||
float computeHeatIndex(float temperature, float percentHumidity,
|
float computeHeatIndex(float temperature, float percentHumidity,
|
||||||
bool isFahrenheit = true);
|
bool isFahrenheit = true);
|
||||||
float readHumidity(bool force = false);
|
std::optional<float> readHumidity(bool force = false);
|
||||||
bool read(bool force = false);
|
bool read(bool force = false);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -82,7 +86,8 @@ private:
|
|||||||
// digitalRead.
|
// digitalRead.
|
||||||
uint8_t _bit, _port;
|
uint8_t _bit, _port;
|
||||||
#endif
|
#endif
|
||||||
uint32_t _lastreadtime, _maxcycles;
|
espchrono::millis_clock::time_point _lastreadtime;
|
||||||
|
uint32_t _maxcycles;
|
||||||
bool _lastresult;
|
bool _lastresult;
|
||||||
uint8_t pullTime; // Time (in usec) to pull up data line before reading
|
uint8_t pullTime; // Time (in usec) to pull up data line before reading
|
||||||
|
|
||||||
|
140
DHT_U.cpp
140
DHT_U.cpp
@@ -14,6 +14,8 @@
|
|||||||
*/
|
*/
|
||||||
#include "DHT_U.h"
|
#include "DHT_U.h"
|
||||||
|
|
||||||
|
#include <espchrono.h>
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @brief Instantiates a new DHT_Unified class
|
* @brief Instantiates a new DHT_Unified class
|
||||||
* @param pin
|
* @param pin
|
||||||
@@ -107,62 +109,65 @@ DHT_Unified::Temperature::Temperature(DHT_Unified *parent, int32_t id)
|
|||||||
* @param event
|
* @param event
|
||||||
* @return always returns true
|
* @return always returns true
|
||||||
*/
|
*/
|
||||||
bool DHT_Unified::Temperature::getEvent(sensors_event_t *event) {
|
std::optional<sensors_event_t> DHT_Unified::Temperature::getEvent() {
|
||||||
// Clear event definition.
|
const auto temperature = _parent->_dht.readTemperature();
|
||||||
memset(event, 0, sizeof(sensors_event_t));
|
if (!temperature)
|
||||||
// Populate sensor reading values.
|
return std::nullopt;
|
||||||
event->version = sizeof(sensors_event_t);
|
|
||||||
event->sensor_id = _id;
|
|
||||||
event->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
|
|
||||||
event->timestamp = millis();
|
|
||||||
event->temperature = _parent->_dht.readTemperature();
|
|
||||||
|
|
||||||
return true;
|
sensors_event_t event;
|
||||||
|
// Populate sensor reading values.
|
||||||
|
event.version = sizeof(sensors_event_t);
|
||||||
|
event.sensor_id = _id;
|
||||||
|
event.type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
|
||||||
|
event.timestamp = espchrono::millis_clock::now();
|
||||||
|
event.temperature = *temperature;
|
||||||
|
|
||||||
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @brief Provides the sensor_t data for this sensor
|
* @brief Provides the sensor_t data for this sensor
|
||||||
* @param sensor
|
* @param sensor
|
||||||
*/
|
*/
|
||||||
void DHT_Unified::Temperature::getSensor(sensor_t *sensor) {
|
sensor_t DHT_Unified::Temperature::getSensor() {
|
||||||
// Clear sensor definition.
|
sensor_t sensor;
|
||||||
memset(sensor, 0, sizeof(sensor_t));
|
|
||||||
// Set sensor name.
|
// Set sensor name.
|
||||||
_parent->setName(sensor);
|
_parent->setName(&sensor);
|
||||||
// Set version and ID
|
// Set version and ID
|
||||||
sensor->version = DHT_SENSOR_VERSION;
|
sensor.version = DHT_SENSOR_VERSION;
|
||||||
sensor->sensor_id = _id;
|
sensor.sensor_id = _id;
|
||||||
// Set type and characteristics.
|
// Set type and characteristics.
|
||||||
sensor->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
|
sensor.type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
|
||||||
_parent->setMinDelay(sensor);
|
_parent->setMinDelay(&sensor);
|
||||||
switch (_parent->_type) {
|
switch (_parent->_type) {
|
||||||
case DHT11:
|
case DHT11:
|
||||||
sensor->max_value = 50.0F;
|
sensor.max_value = 50.0F;
|
||||||
sensor->min_value = 0.0F;
|
sensor.min_value = 0.0F;
|
||||||
sensor->resolution = 2.0F;
|
sensor.resolution = 2.0F;
|
||||||
break;
|
break;
|
||||||
case DHT12:
|
case DHT12:
|
||||||
sensor->max_value = 60.0F;
|
sensor.max_value = 60.0F;
|
||||||
sensor->min_value = -20.0F;
|
sensor.min_value = -20.0F;
|
||||||
sensor->resolution = 0.5F;
|
sensor.resolution = 0.5F;
|
||||||
break;
|
break;
|
||||||
case DHT21:
|
case DHT21:
|
||||||
sensor->max_value = 80.0F;
|
sensor.max_value = 80.0F;
|
||||||
sensor->min_value = -40.0F;
|
sensor.min_value = -40.0F;
|
||||||
sensor->resolution = 0.1F;
|
sensor.resolution = 0.1F;
|
||||||
break;
|
break;
|
||||||
case DHT22:
|
case DHT22:
|
||||||
sensor->max_value = 125.0F;
|
sensor.max_value = 125.0F;
|
||||||
sensor->min_value = -40.0F;
|
sensor.min_value = -40.0F;
|
||||||
sensor->resolution = 0.1F;
|
sensor.resolution = 0.1F;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// Unknown type, default to 0.
|
// Unknown type, default to 0.
|
||||||
sensor->max_value = 0.0F;
|
sensor.max_value = 0.0F;
|
||||||
sensor->min_value = 0.0F;
|
sensor.min_value = 0.0F;
|
||||||
sensor->resolution = 0.0F;
|
sensor.resolution = 0.0F;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
return sensor;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -180,60 +185,63 @@ DHT_Unified::Humidity::Humidity(DHT_Unified *parent, int32_t id)
|
|||||||
* @param event
|
* @param event
|
||||||
* @return always returns true
|
* @return always returns true
|
||||||
*/
|
*/
|
||||||
bool DHT_Unified::Humidity::getEvent(sensors_event_t *event) {
|
std::optional<sensors_event_t> DHT_Unified::Humidity::getEvent() {
|
||||||
// Clear event definition.
|
const auto humidity = _parent->_dht.readHumidity();
|
||||||
memset(event, 0, sizeof(sensors_event_t));
|
if (!humidity)
|
||||||
// Populate sensor reading values.
|
return std::nullopt;
|
||||||
event->version = sizeof(sensors_event_t);
|
|
||||||
event->sensor_id = _id;
|
|
||||||
event->type = SENSOR_TYPE_RELATIVE_HUMIDITY;
|
|
||||||
event->timestamp = millis();
|
|
||||||
event->relative_humidity = _parent->_dht.readHumidity();
|
|
||||||
|
|
||||||
return true;
|
sensors_event_t event;
|
||||||
|
// Populate sensor reading values.
|
||||||
|
event.version = sizeof(sensors_event_t);
|
||||||
|
event.sensor_id = _id;
|
||||||
|
event.type = SENSOR_TYPE_RELATIVE_HUMIDITY;
|
||||||
|
event.timestamp = espchrono::millis_clock::now();
|
||||||
|
event.relative_humidity = *humidity;
|
||||||
|
|
||||||
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @brief Provides the sensor_t data for this sensor
|
* @brief Provides the sensor_t data for this sensor
|
||||||
* @param sensor
|
* @param sensor
|
||||||
*/
|
*/
|
||||||
void DHT_Unified::Humidity::getSensor(sensor_t *sensor) {
|
sensor_t DHT_Unified::Humidity::getSensor() {
|
||||||
// Clear sensor definition.
|
sensor_t sensor;
|
||||||
memset(sensor, 0, sizeof(sensor_t));
|
|
||||||
// Set sensor name.
|
// Set sensor name.
|
||||||
_parent->setName(sensor);
|
_parent->setName(&sensor);
|
||||||
// Set version and ID
|
// Set version and ID
|
||||||
sensor->version = DHT_SENSOR_VERSION;
|
sensor.version = DHT_SENSOR_VERSION;
|
||||||
sensor->sensor_id = _id;
|
sensor.sensor_id = _id;
|
||||||
// Set type and characteristics.
|
// Set type and characteristics.
|
||||||
sensor->type = SENSOR_TYPE_RELATIVE_HUMIDITY;
|
sensor.type = SENSOR_TYPE_RELATIVE_HUMIDITY;
|
||||||
_parent->setMinDelay(sensor);
|
_parent->setMinDelay(&sensor);
|
||||||
switch (_parent->_type) {
|
switch (_parent->_type) {
|
||||||
case DHT11:
|
case DHT11:
|
||||||
sensor->max_value = 80.0F;
|
sensor.max_value = 80.0F;
|
||||||
sensor->min_value = 20.0F;
|
sensor.min_value = 20.0F;
|
||||||
sensor->resolution = 5.0F;
|
sensor.resolution = 5.0F;
|
||||||
break;
|
break;
|
||||||
case DHT12:
|
case DHT12:
|
||||||
sensor->max_value = 95.0F;
|
sensor.max_value = 95.0F;
|
||||||
sensor->min_value = 20.0F;
|
sensor.min_value = 20.0F;
|
||||||
sensor->resolution = 5.0F;
|
sensor.resolution = 5.0F;
|
||||||
break;
|
break;
|
||||||
case DHT21:
|
case DHT21:
|
||||||
sensor->max_value = 100.0F;
|
sensor.max_value = 100.0F;
|
||||||
sensor->min_value = 0.0F;
|
sensor.min_value = 0.0F;
|
||||||
sensor->resolution = 0.1F;
|
sensor.resolution = 0.1F;
|
||||||
break;
|
break;
|
||||||
case DHT22:
|
case DHT22:
|
||||||
sensor->max_value = 100.0F;
|
sensor.max_value = 100.0F;
|
||||||
sensor->min_value = 0.0F;
|
sensor.min_value = 0.0F;
|
||||||
sensor->resolution = 0.1F;
|
sensor.resolution = 0.1F;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// Unknown type, default to 0.
|
// Unknown type, default to 0.
|
||||||
sensor->max_value = 0.0F;
|
sensor.max_value = 0.0F;
|
||||||
sensor->min_value = 0.0F;
|
sensor.min_value = 0.0F;
|
||||||
sensor->resolution = 0.0F;
|
sensor.resolution = 0.0F;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
return sensor;
|
||||||
}
|
}
|
||||||
|
8
DHT_U.h
8
DHT_U.h
@@ -54,8 +54,8 @@ public:
|
|||||||
class Temperature : public Adafruit_Sensor {
|
class Temperature : public Adafruit_Sensor {
|
||||||
public:
|
public:
|
||||||
Temperature(DHT_Unified *parent, int32_t id);
|
Temperature(DHT_Unified *parent, int32_t id);
|
||||||
bool getEvent(sensors_event_t *event);
|
std::optional<sensors_event_t> getEvent() override;
|
||||||
void getSensor(sensor_t *sensor);
|
sensor_t getSensor() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DHT_Unified *_parent;
|
DHT_Unified *_parent;
|
||||||
@@ -68,8 +68,8 @@ public:
|
|||||||
class Humidity : public Adafruit_Sensor {
|
class Humidity : public Adafruit_Sensor {
|
||||||
public:
|
public:
|
||||||
Humidity(DHT_Unified *parent, int32_t id);
|
Humidity(DHT_Unified *parent, int32_t id);
|
||||||
bool getEvent(sensors_event_t *event);
|
std::optional<sensors_event_t> getEvent() override;
|
||||||
void getSensor(sensor_t *sensor);
|
sensor_t getSensor() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DHT_Unified *_parent;
|
DHT_Unified *_parent;
|
||||||
|
Reference in New Issue
Block a user