Files
DHT-sensor-library/DHT_U.cpp

248 lines
6.3 KiB
C++
Raw Permalink Normal View History

2019-07-04 17:28:10 +02:00
/*!
* @file DHT_U.cpp
*
* Temperature & Humidity Unified Sensor Library
*
* This is a library for DHT series of low cost temperature/humidity sensors.
*
* You must have Adafruit Unified Sensor Library library installed to use this
* class.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit andopen-source hardware by purchasing products
* from Adafruit!
*/
2016-10-26 23:44:39 +02:00
#include "DHT_U.h"
2021-07-13 16:25:32 +02:00
#include <espchrono.h>
2019-07-04 17:28:10 +02:00
/*!
* @brief Instantiates a new DHT_Unified class
* @param pin
* pin number that sensor is connected
* @param type
* type of sensor
* @param count
* number of sensors
* @param tempSensorId
* temperature sensor id
* @param humiditySensorId
* humidity sensor id
*/
2021-07-15 13:14:07 +02:00
DHT_Unified::DHT_Unified(gpio_num_t pin, uint8_t type, uint8_t count,
2020-04-08 11:36:24 -07:00
int32_t tempSensorId, int32_t humiditySensorId)
: _dht(pin, type, count), _type(type), _temp(this, tempSensorId),
_humidity(this, humiditySensorId) {}
2016-10-26 23:44:39 +02:00
2019-07-04 17:28:10 +02:00
/*!
* @brief Setup sensor (calls begin on It)
*/
bool DHT_Unified::begin() { return _dht.begin(); }
2016-10-26 23:44:39 +02:00
2019-07-04 17:28:10 +02:00
/*!
* @brief Sets sensor name
* @param sensor
* Sensor that will be set
*/
2020-04-08 11:36:24 -07:00
void DHT_Unified::setName(sensor_t *sensor) {
switch (_type) {
case DHT11:
strncpy(sensor->name, "DHT11", sizeof(sensor->name) - 1);
break;
case DHT12:
strncpy(sensor->name, "DHT12", sizeof(sensor->name) - 1);
break;
case DHT21:
strncpy(sensor->name, "DHT21", sizeof(sensor->name) - 1);
break;
case DHT22:
strncpy(sensor->name, "DHT22", sizeof(sensor->name) - 1);
break;
default:
// TODO: Perhaps this should be an error? However main DHT library doesn't
// enforce restrictions on the sensor type value. Pick a generic name for
// now.
strncpy(sensor->name, "DHT?", sizeof(sensor->name) - 1);
break;
2016-10-26 23:44:39 +02:00
}
2020-04-08 11:36:24 -07:00
sensor->name[sizeof(sensor->name) - 1] = 0;
2016-10-26 23:44:39 +02:00
}
2019-07-04 17:28:10 +02:00
/*!
* @brief Sets Minimum Delay Value
* @param sensor
* Sensor that will be set
*/
2020-04-08 11:36:24 -07:00
void DHT_Unified::setMinDelay(sensor_t *sensor) {
switch (_type) {
case DHT11:
sensor->min_delay = 1000000L; // 1 second (in microseconds)
break;
case DHT12:
sensor->min_delay = 2000000L; // 2 second (in microseconds)
break;
case DHT21:
sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
break;
case DHT22:
sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
break;
default:
// Default to slowest sample rate in case of unknown type.
sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
break;
2016-10-26 23:44:39 +02:00
}
}
2019-07-04 17:28:10 +02:00
/*!
* @brief Instantiates a new DHT_Unified Temperature Class
* @param parent
* Parent Sensor
* @param id
* Sensor id
*/
2020-04-08 11:36:24 -07:00
DHT_Unified::Temperature::Temperature(DHT_Unified *parent, int32_t id)
: _parent(parent), _id(id) {}
2016-10-26 23:44:39 +02:00
2019-07-04 17:28:10 +02:00
/*!
* @brief Reads the sensor and returns the data as a sensors_event_t
* @param event
* @return always returns true
*/
2021-07-13 16:25:32 +02:00
std::optional<sensors_event_t> DHT_Unified::Temperature::getEvent() {
const auto temperature = _parent->_dht.readTemperature();
if (!temperature)
return std::nullopt;
sensors_event_t event;
2016-10-26 23:44:39 +02:00
// Populate sensor reading values.
2021-07-13 16:25:32 +02:00
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;
2020-04-08 11:36:24 -07:00
2021-07-13 16:25:32 +02:00
return event;
2016-10-26 23:44:39 +02:00
}
2019-07-04 17:28:10 +02:00
/*!
* @brief Provides the sensor_t data for this sensor
* @param sensor
*/
2021-07-13 16:25:32 +02:00
sensor_t DHT_Unified::Temperature::getSensor() {
sensor_t sensor;
2016-10-26 23:44:39 +02:00
// Set sensor name.
2021-07-13 16:25:32 +02:00
_parent->setName(&sensor);
2016-10-26 23:44:39 +02:00
// Set version and ID
2021-07-13 16:25:32 +02:00
sensor.version = DHT_SENSOR_VERSION;
sensor.sensor_id = _id;
2016-10-26 23:44:39 +02:00
// Set type and characteristics.
2021-07-13 16:25:32 +02:00
sensor.type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
_parent->setMinDelay(&sensor);
2016-10-26 23:44:39 +02:00
switch (_parent->_type) {
2020-04-08 11:36:24 -07:00
case DHT11:
2021-07-13 16:25:32 +02:00
sensor.max_value = 50.0F;
sensor.min_value = 0.0F;
sensor.resolution = 2.0F;
2020-04-08 11:36:24 -07:00
break;
case DHT12:
2021-07-13 16:25:32 +02:00
sensor.max_value = 60.0F;
sensor.min_value = -20.0F;
sensor.resolution = 0.5F;
2020-04-08 11:36:24 -07:00
break;
case DHT21:
2021-07-13 16:25:32 +02:00
sensor.max_value = 80.0F;
sensor.min_value = -40.0F;
sensor.resolution = 0.1F;
2020-04-08 11:36:24 -07:00
break;
case DHT22:
2021-07-13 16:25:32 +02:00
sensor.max_value = 125.0F;
sensor.min_value = -40.0F;
sensor.resolution = 0.1F;
2020-04-08 11:36:24 -07:00
break;
default:
// Unknown type, default to 0.
2021-07-13 16:25:32 +02:00
sensor.max_value = 0.0F;
sensor.min_value = 0.0F;
sensor.resolution = 0.0F;
2020-04-08 11:36:24 -07:00
break;
2016-10-26 23:44:39 +02:00
}
2021-07-13 16:25:32 +02:00
return sensor;
2016-10-26 23:44:39 +02:00
}
2019-07-04 17:28:10 +02:00
/*!
* @brief Instantiates a new DHT_Unified Humidity Class
* @param parent
* Parent Sensor
* @param id
* Sensor id
*/
2020-04-08 11:36:24 -07:00
DHT_Unified::Humidity::Humidity(DHT_Unified *parent, int32_t id)
: _parent(parent), _id(id) {}
2016-10-26 23:44:39 +02:00
2019-07-04 17:28:10 +02:00
/*!
* @brief Reads the sensor and returns the data as a sensors_event_t
* @param event
* @return always returns true
*/
2021-07-13 16:25:32 +02:00
std::optional<sensors_event_t> DHT_Unified::Humidity::getEvent() {
const auto humidity = _parent->_dht.readHumidity();
if (!humidity)
return std::nullopt;
sensors_event_t event;
2016-10-26 23:44:39 +02:00
// Populate sensor reading values.
2021-07-13 16:25:32 +02:00
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;
2020-04-08 11:36:24 -07:00
2021-07-13 16:25:32 +02:00
return event;
2016-10-26 23:44:39 +02:00
}
2019-07-04 17:28:10 +02:00
/*!
* @brief Provides the sensor_t data for this sensor
* @param sensor
*/
2021-07-13 16:25:32 +02:00
sensor_t DHT_Unified::Humidity::getSensor() {
sensor_t sensor;
2016-10-26 23:44:39 +02:00
// Set sensor name.
2021-07-13 16:25:32 +02:00
_parent->setName(&sensor);
2016-10-26 23:44:39 +02:00
// Set version and ID
2021-07-13 16:25:32 +02:00
sensor.version = DHT_SENSOR_VERSION;
sensor.sensor_id = _id;
2016-10-26 23:44:39 +02:00
// Set type and characteristics.
2021-07-13 16:25:32 +02:00
sensor.type = SENSOR_TYPE_RELATIVE_HUMIDITY;
_parent->setMinDelay(&sensor);
2016-10-26 23:44:39 +02:00
switch (_parent->_type) {
2020-04-08 11:36:24 -07:00
case DHT11:
2021-07-13 16:25:32 +02:00
sensor.max_value = 80.0F;
sensor.min_value = 20.0F;
sensor.resolution = 5.0F;
2020-04-08 11:36:24 -07:00
break;
case DHT12:
2021-07-13 16:25:32 +02:00
sensor.max_value = 95.0F;
sensor.min_value = 20.0F;
sensor.resolution = 5.0F;
2020-04-08 11:36:24 -07:00
break;
case DHT21:
2021-07-13 16:25:32 +02:00
sensor.max_value = 100.0F;
sensor.min_value = 0.0F;
sensor.resolution = 0.1F;
2020-04-08 11:36:24 -07:00
break;
case DHT22:
2021-07-13 16:25:32 +02:00
sensor.max_value = 100.0F;
sensor.min_value = 0.0F;
sensor.resolution = 0.1F;
2020-04-08 11:36:24 -07:00
break;
default:
// Unknown type, default to 0.
2021-07-13 16:25:32 +02:00
sensor.max_value = 0.0F;
sensor.min_value = 0.0F;
sensor.resolution = 0.0F;
2020-04-08 11:36:24 -07:00
break;
2016-10-26 23:44:39 +02:00
}
2021-07-13 16:25:32 +02:00
return sensor;
2016-10-26 23:44:39 +02:00
}