DHT_U.cpp 6.3 KB

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