DHT_U.cpp 6.6 KB

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