DHT_U.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*!
  2. * @file DHT_U.cpp
  3. *
  4. * Temperature & Humidity Unified Sensor Library
  5. *
  6. * This is a library for DHT series of low cost temperature/humidity sensors.
  7. *
  8. * You must have Adafruit Unified Sensor Library library installed to use this
  9. * class.
  10. *
  11. * Adafruit invests time and resources providing this open source code,
  12. * please support Adafruit andopen-source hardware by purchasing products
  13. * from Adafruit!
  14. */
  15. #include "DHT_U.h"
  16. /*!
  17. * @brief Instantiates a new DHT_Unified class
  18. * @param pin
  19. * pin number that sensor is connected
  20. * @param type
  21. * type of sensor
  22. * @param count
  23. * number of sensors
  24. * @param tempSensorId
  25. * temperature sensor id
  26. * @param humiditySensorId
  27. * humidity sensor id
  28. */
  29. DHT_Unified::DHT_Unified(uint8_t pin, uint8_t type, uint8_t count,
  30. int32_t tempSensorId, int32_t humiditySensorId)
  31. : _dht(pin, type, count),
  32. _type(type),
  33. _temp(this, tempSensorId),
  34. _humidity(this, humiditySensorId) {}
  35. /*!
  36. * @brief Setup sensor (calls begin on It)
  37. */
  38. void DHT_Unified::begin() {
  39. _dht.begin();
  40. }
  41. /*!
  42. * @brief Sets sensor name
  43. * @param sensor
  44. * Sensor that will be set
  45. */
  46. void DHT_Unified::setName(sensor_t* sensor) {
  47. switch (_type) {
  48. case DHT11:
  49. strncpy(sensor->name, "DHT11", sizeof(sensor->name) - 1);
  50. break;
  51. case DHT12:
  52. strncpy(sensor->name, "DHT12", sizeof(sensor->name) - 1);
  53. break;
  54. case DHT21:
  55. strncpy(sensor->name, "DHT21", sizeof(sensor->name) - 1);
  56. break;
  57. case DHT22:
  58. strncpy(sensor->name, "DHT22", sizeof(sensor->name) - 1);
  59. break;
  60. default:
  61. // TODO: Perhaps this should be an error? However main DHT library
  62. // doesn't enforce restrictions on the sensor type value. Pick a generic
  63. // name for now.
  64. strncpy(sensor->name, "DHT?", sizeof(sensor->name) - 1);
  65. break;
  66. }
  67. sensor->name[sizeof(sensor->name) - 1] = 0;
  68. }
  69. /*!
  70. * @brief Sets Minimum Delay Value
  71. * @param sensor
  72. * Sensor that will be set
  73. */
  74. void DHT_Unified::setMinDelay(sensor_t* sensor) {
  75. switch (_type) {
  76. case DHT11:
  77. sensor->min_delay = 1000000L; // 1 second (in microseconds)
  78. break;
  79. case DHT12:
  80. sensor->min_delay = 2000000L; // 2 second (in microseconds)
  81. break;
  82. case DHT21:
  83. sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
  84. break;
  85. case DHT22:
  86. sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
  87. break;
  88. default:
  89. // Default to slowest sample rate in case of unknown type.
  90. sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
  91. break;
  92. }
  93. }
  94. /*!
  95. * @brief Instantiates a new DHT_Unified Temperature Class
  96. * @param parent
  97. * Parent Sensor
  98. * @param id
  99. * Sensor id
  100. */
  101. DHT_Unified::Temperature::Temperature(DHT_Unified* parent, int32_t id)
  102. : _parent(parent), _id(id) {}
  103. /*!
  104. * @brief Reads the sensor and returns the data as a sensors_event_t
  105. * @param event
  106. * @return always returns true
  107. */
  108. bool DHT_Unified::Temperature::getEvent(sensors_event_t* event) {
  109. // Clear event definition.
  110. memset(event, 0, sizeof(sensors_event_t));
  111. // Populate sensor reading values.
  112. event->version = sizeof(sensors_event_t);
  113. event->sensor_id = _id;
  114. event->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
  115. event->timestamp = millis();
  116. event->temperature = _parent->_dht.readTemperature();
  117. return true;
  118. }
  119. /*!
  120. * @brief Provides the sensor_t data for this sensor
  121. * @param sensor
  122. */
  123. void DHT_Unified::Temperature::getSensor(sensor_t* sensor) {
  124. // Clear sensor definition.
  125. memset(sensor, 0, sizeof(sensor_t));
  126. // Set sensor name.
  127. _parent->setName(sensor);
  128. // Set version and ID
  129. sensor->version = DHT_SENSOR_VERSION;
  130. sensor->sensor_id = _id;
  131. // Set type and characteristics.
  132. sensor->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
  133. _parent->setMinDelay(sensor);
  134. switch (_parent->_type) {
  135. case DHT11:
  136. sensor->max_value = 50.0F;
  137. sensor->min_value = 0.0F;
  138. sensor->resolution = 2.0F;
  139. break;
  140. case DHT12:
  141. sensor->max_value = 60.0F;
  142. sensor->min_value = -20.0F;
  143. sensor->resolution = 0.5F;
  144. break;
  145. case DHT21:
  146. sensor->max_value = 80.0F;
  147. sensor->min_value = -40.0F;
  148. sensor->resolution = 0.1F;
  149. break;
  150. case DHT22:
  151. sensor->max_value = 125.0F;
  152. sensor->min_value = -40.0F;
  153. sensor->resolution = 0.1F;
  154. break;
  155. default:
  156. // Unknown type, default to 0.
  157. sensor->max_value = 0.0F;
  158. sensor->min_value = 0.0F;
  159. sensor->resolution = 0.0F;
  160. break;
  161. }
  162. }
  163. /*!
  164. * @brief Instantiates a new DHT_Unified Humidity Class
  165. * @param parent
  166. * Parent Sensor
  167. * @param id
  168. * Sensor id
  169. */
  170. DHT_Unified::Humidity::Humidity(DHT_Unified* parent, int32_t id)
  171. : _parent(parent), _id(id) {}
  172. /*!
  173. * @brief Reads the sensor and returns the data as a sensors_event_t
  174. * @param event
  175. * @return always returns true
  176. */
  177. bool DHT_Unified::Humidity::getEvent(sensors_event_t* event) {
  178. // Clear event definition.
  179. memset(event, 0, sizeof(sensors_event_t));
  180. // Populate sensor reading values.
  181. event->version = sizeof(sensors_event_t);
  182. event->sensor_id = _id;
  183. event->type = SENSOR_TYPE_RELATIVE_HUMIDITY;
  184. event->timestamp = millis();
  185. event->relative_humidity = _parent->_dht.readHumidity();
  186. return true;
  187. }
  188. /*!
  189. * @brief Provides the sensor_t data for this sensor
  190. * @param sensor
  191. */
  192. void DHT_Unified::Humidity::getSensor(sensor_t* sensor) {
  193. // Clear sensor definition.
  194. memset(sensor, 0, sizeof(sensor_t));
  195. // Set sensor name.
  196. _parent->setName(sensor);
  197. // Set version and ID
  198. sensor->version = DHT_SENSOR_VERSION;
  199. sensor->sensor_id = _id;
  200. // Set type and characteristics.
  201. sensor->type = SENSOR_TYPE_RELATIVE_HUMIDITY;
  202. _parent->setMinDelay(sensor);
  203. switch (_parent->_type) {
  204. case DHT11:
  205. sensor->max_value = 80.0F;
  206. sensor->min_value = 20.0F;
  207. sensor->resolution = 5.0F;
  208. break;
  209. case DHT12:
  210. sensor->max_value = 95.0F;
  211. sensor->min_value = 20.0F;
  212. sensor->resolution = 5.0F;
  213. break;
  214. case DHT21:
  215. sensor->max_value = 100.0F;
  216. sensor->min_value = 0.0F;
  217. sensor->resolution = 0.1F;
  218. break;
  219. case DHT22:
  220. sensor->max_value = 100.0F;
  221. sensor->min_value = 0.0F;
  222. sensor->resolution = 0.1F;
  223. break;
  224. default:
  225. // Unknown type, default to 0.
  226. sensor->max_value = 0.0F;
  227. sensor->min_value = 0.0F;
  228. sensor->resolution = 0.0F;
  229. break;
  230. }
  231. }