DHT_U.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // DHT Temperature & Humidity Unified Sensor Library
  2. // Copyright (c) 2014 Adafruit Industries
  3. // Author: Tony DiCola
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. // SOFTWARE.
  19. #include "DHT_U.h"
  20. DHT_Unified::DHT_Unified(uint8_t pin, uint8_t type, uint8_t count, int32_t tempSensorId, int32_t humiditySensorId):
  21. _dht(pin, type, count),
  22. _type(type),
  23. _temp(this, tempSensorId),
  24. _humidity(this, humiditySensorId)
  25. {}
  26. void DHT_Unified::begin() {
  27. _dht.begin();
  28. }
  29. void DHT_Unified::setName(sensor_t* sensor) {
  30. switch(_type) {
  31. case DHT11:
  32. strncpy(sensor->name, "DHT11", sizeof(sensor->name) - 1);
  33. break;
  34. case DHT21:
  35. strncpy(sensor->name, "DHT21", sizeof(sensor->name) - 1);
  36. break;
  37. case DHT22:
  38. strncpy(sensor->name, "DHT22", sizeof(sensor->name) - 1);
  39. break;
  40. default:
  41. // TODO: Perhaps this should be an error? However main DHT library doesn't enforce
  42. // restrictions on the sensor type value. Pick a generic name for now.
  43. strncpy(sensor->name, "DHT?", sizeof(sensor->name) - 1);
  44. break;
  45. }
  46. sensor->name[sizeof(sensor->name)- 1] = 0;
  47. }
  48. void DHT_Unified::setMinDelay(sensor_t* sensor) {
  49. switch(_type) {
  50. case DHT11:
  51. sensor->min_delay = 1000000L; // 1 second (in microseconds)
  52. break;
  53. case DHT21:
  54. sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
  55. break;
  56. case DHT22:
  57. sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
  58. break;
  59. default:
  60. // Default to slowest sample rate in case of unknown type.
  61. sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
  62. break;
  63. }
  64. }
  65. DHT_Unified::Temperature::Temperature(DHT_Unified* parent, int32_t id):
  66. _parent(parent),
  67. _id(id)
  68. {}
  69. bool DHT_Unified::Temperature::getEvent(sensors_event_t* event) {
  70. // Clear event definition.
  71. memset(event, 0, sizeof(sensors_event_t));
  72. // Populate sensor reading values.
  73. event->version = sizeof(sensors_event_t);
  74. event->sensor_id = _id;
  75. event->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
  76. event->timestamp = millis();
  77. event->temperature = _parent->_dht.readTemperature();
  78. return true;
  79. }
  80. void DHT_Unified::Temperature::getSensor(sensor_t* sensor) {
  81. // Clear sensor definition.
  82. memset(sensor, 0, sizeof(sensor_t));
  83. // Set sensor name.
  84. _parent->setName(sensor);
  85. // Set version and ID
  86. sensor->version = DHT_SENSOR_VERSION;
  87. sensor->sensor_id = _id;
  88. // Set type and characteristics.
  89. sensor->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
  90. _parent->setMinDelay(sensor);
  91. switch (_parent->_type) {
  92. case DHT11:
  93. sensor->max_value = 50.0F;
  94. sensor->min_value = 0.0F;
  95. sensor->resolution = 2.0F;
  96. break;
  97. case DHT21:
  98. sensor->max_value = 80.0F;
  99. sensor->min_value = -40.0F;
  100. sensor->resolution = 0.1F;
  101. break;
  102. case DHT22:
  103. sensor->max_value = 125.0F;
  104. sensor->min_value = -40.0F;
  105. sensor->resolution = 0.1F;
  106. break;
  107. default:
  108. // Unknown type, default to 0.
  109. sensor->max_value = 0.0F;
  110. sensor->min_value = 0.0F;
  111. sensor->resolution = 0.0F;
  112. break;
  113. }
  114. }
  115. DHT_Unified::Humidity::Humidity(DHT_Unified* parent, int32_t id):
  116. _parent(parent),
  117. _id(id)
  118. {}
  119. bool DHT_Unified::Humidity::getEvent(sensors_event_t* event) {
  120. // Clear event definition.
  121. memset(event, 0, sizeof(sensors_event_t));
  122. // Populate sensor reading values.
  123. event->version = sizeof(sensors_event_t);
  124. event->sensor_id = _id;
  125. event->type = SENSOR_TYPE_RELATIVE_HUMIDITY;
  126. event->timestamp = millis();
  127. event->relative_humidity = _parent->_dht.readHumidity();
  128. return true;
  129. }
  130. void DHT_Unified::Humidity::getSensor(sensor_t* sensor) {
  131. // Clear sensor definition.
  132. memset(sensor, 0, sizeof(sensor_t));
  133. // Set sensor name.
  134. _parent->setName(sensor);
  135. // Set version and ID
  136. sensor->version = DHT_SENSOR_VERSION;
  137. sensor->sensor_id = _id;
  138. // Set type and characteristics.
  139. sensor->type = SENSOR_TYPE_RELATIVE_HUMIDITY;
  140. _parent->setMinDelay(sensor);
  141. switch (_parent->_type) {
  142. case DHT11:
  143. sensor->max_value = 80.0F;
  144. sensor->min_value = 20.0F;
  145. sensor->resolution = 5.0F;
  146. break;
  147. case DHT21:
  148. sensor->max_value = 100.0F;
  149. sensor->min_value = 0.0F;
  150. sensor->resolution = 0.1F;
  151. break;
  152. case DHT22:
  153. sensor->max_value = 100.0F;
  154. sensor->min_value = 0.0F;
  155. sensor->resolution = 0.1F;
  156. break;
  157. default:
  158. // Unknown type, default to 0.
  159. sensor->max_value = 0.0F;
  160. sensor->min_value = 0.0F;
  161. sensor->resolution = 0.0F;
  162. break;
  163. }
  164. }