DHT.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /* DHT library
  2. MIT license
  3. written by Adafruit Industries
  4. */
  5. #include <math.h>
  6. #include "DHT.h"
  7. //#define NAN 0
  8. #ifdef DEBUG
  9. #define DEBUG_PRINT(...) Serial.println(__VA_ARGS__)
  10. #else
  11. #define DEBUG_PRINT(...)
  12. #endif
  13. DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
  14. _pin = pin;
  15. _type = type;
  16. _count = count;
  17. firstreading = true;
  18. }
  19. void DHT::begin(void) {
  20. if (_type == DHT10) {
  21. if (DHT10Init()) {
  22. SERIALPRINT.println("Error : Failed to init DHT 11\n");
  23. while (1);
  24. }
  25. } else {
  26. // set up the pins!
  27. pinMode(_pin, INPUT);
  28. digitalWrite(_pin, HIGH);
  29. _lastreadtime = 0;
  30. }
  31. }
  32. /** Common interface to get temp&humi value.support all DHT device.
  33. @return 0 for calibrated failed,1 for succeed.
  34. **/
  35. int DHT::readTempAndHumidity(float* data) {
  36. uint32_t target_val[2] = {0};
  37. uint32_t cnt;
  38. if (_type == DHT10) {
  39. while (DHT10ReadStatus() == 0) {
  40. DHT10Init();
  41. delay(30);
  42. cnt++;
  43. if (cnt > 3) {
  44. return -1;
  45. }
  46. }
  47. //wait for data ready。
  48. while (readTargetData(target_val)) {
  49. cnt++;
  50. delay(50);
  51. if (cnt > 5) {
  52. return -1;
  53. }
  54. }
  55. data[0] = target_val[0] * 100.0 / 1024 / 1024;
  56. data[1] = target_val[1] * 200.0 / 1024 / 1024 - 50;
  57. } else {
  58. data[0] = readHumidity();
  59. data[1] = readTemperature();
  60. if (isnan(data[0]) || isnan(data[1])) {
  61. return -1;
  62. }
  63. }
  64. return 0;
  65. }
  66. //boolean S == Scale. True == Farenheit; False == Celcius
  67. float DHT::readTemperature(bool S) {
  68. float f;
  69. if (read()) {
  70. switch (_type) {
  71. case DHT11:
  72. f = data[2];
  73. if (S) {
  74. f = convertCtoF(f);
  75. }
  76. return f;
  77. case DHT22:
  78. case DHT21:
  79. f = data[2] & 0x7F;
  80. f *= 256;
  81. f += data[3];
  82. f /= 10;
  83. if (data[2] & 0x80) {
  84. f *= -1;
  85. }
  86. if (S) {
  87. f = convertCtoF(f);
  88. }
  89. return f;
  90. }
  91. }
  92. DEBUG_PRINT("Read fail");
  93. return NAN;
  94. }
  95. float DHT::convertCtoF(float c) {
  96. return c * 9 / 5 + 32;
  97. }
  98. float DHT::readHumidity(void) {
  99. float f;
  100. if (read()) {
  101. switch (_type) {
  102. case DHT11:
  103. f = data[0];
  104. return f;
  105. case DHT22:
  106. case DHT21:
  107. f = data[0];
  108. f *= 256;
  109. f += data[1];
  110. f /= 10;
  111. return f;
  112. }
  113. }
  114. DEBUG_PRINT("Read fail");
  115. return NAN;
  116. }
  117. boolean DHT::read(void) {
  118. uint8_t laststate = HIGH;
  119. uint8_t counter = 0;
  120. uint8_t j = 0, i;
  121. unsigned long currenttime;
  122. // pull the pin high and wait 250 milliseconds
  123. digitalWrite(_pin, HIGH);
  124. delay(250);
  125. currenttime = millis();
  126. if (currenttime < _lastreadtime) {
  127. // ie there was a rollover
  128. _lastreadtime = 0;
  129. }
  130. if (!firstreading && ((currenttime - _lastreadtime) < 2000)) {
  131. return true; // return last correct measurement
  132. //delay(2000 - (currenttime - _lastreadtime));
  133. }
  134. firstreading = false;
  135. /*
  136. DEBUG_PRINT("Currtime: "); DEBUG_PRINT(currenttime);
  137. DEBUG_PRINT(" Lasttime: "); DEBUG_PRINT(_lastreadtime);
  138. */
  139. _lastreadtime = millis();
  140. data[0] = data[1] = data[2] = data[3] = data[4] = 0;
  141. // now pull it low for ~20 milliseconds
  142. pinMode(_pin, OUTPUT);
  143. digitalWrite(_pin, LOW);
  144. delay(20);
  145. //cli();
  146. digitalWrite(_pin, HIGH);
  147. delayMicroseconds(40);
  148. pinMode(_pin, INPUT);
  149. // read in timings
  150. for (i = 0; i < MAXTIMINGS; i++) {
  151. counter = 0;
  152. while (digitalRead(_pin) == laststate) {
  153. counter++;
  154. delayMicroseconds(1);
  155. if (counter == 255) {
  156. break;
  157. }
  158. }
  159. laststate = digitalRead(_pin);
  160. if (counter == 255) {
  161. break;
  162. }
  163. // ignore first 3 transitions
  164. if ((i >= 4) && (i % 2 == 0)) {
  165. // shove each bit into the storage bytes
  166. data[j / 8] <<= 1;
  167. if (counter > _count) {
  168. data[j / 8] |= 1;
  169. }
  170. j++;
  171. }
  172. }
  173. //sei();
  174. /*
  175. DEBUG_PRINTln(j, DEC);
  176. DEBUG_PRINT(data[0], HEX); DEBUG_PRINT(", ");
  177. DEBUG_PRINT(data[1], HEX); DEBUG_PRINT(", ");
  178. DEBUG_PRINT(data[2], HEX); DEBUG_PRINT(", ");
  179. DEBUG_PRINT(data[3], HEX); DEBUG_PRINT(", ");
  180. DEBUG_PRINT(data[4], HEX); DEBUG_PRINT(" =? ");
  181. DEBUG_PRINTln(data[0] + data[1] + data[2] + data[3], HEX);
  182. */
  183. // check we read 40 bits and that the checksum matches
  184. if ((j >= 40) &&
  185. (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))) {
  186. return true;
  187. }
  188. return false;
  189. }
  190. /*****************************************************************************/
  191. /*****************************************************************************/
  192. /** Reset sensor.
  193. @return 0 for calibrated failed,1 for succeed.
  194. **/
  195. int DHT::DHT10Reset(void) {
  196. if (_type == DHT10) {
  197. return i2cWriteByte(RESET_REG_ADDR);
  198. } else {
  199. return 0;
  200. SERIALPRINT.println("This function only support for DHT10");
  201. }
  202. }
  203. /** Read status register.check the calibration flag - bit[3]: 1- calibrated ok ,0 - Not calibrated.
  204. @return 0 for calibrated failed,1 for succeed.
  205. **/
  206. int DHT::DHT10ReadStatus(void) {
  207. int ret = 0;
  208. uint8_t statu = 0;
  209. if (_type == DHT10) {
  210. ret = i2cReadByte(statu);
  211. if (ret) {
  212. SERIALPRINT.println("Failed to read byte\n");
  213. }
  214. if ((statu & 0x8) == 0x8) {
  215. return 1;
  216. } else {
  217. return 0;
  218. }
  219. } else {
  220. SERIALPRINT.println("This function only support for DHT10");
  221. return 0;
  222. }
  223. }
  224. /** Init sensor,send 0x08,0x00 to register 0xe1.
  225. @ return : 0 if success, non-zero if failed.
  226. **/
  227. int DHT::setSystemCfg(void) {
  228. uint8_t cfg_param[] = {0xe1, 0x08, 0x00};
  229. if (_type == DHT10) {
  230. return i2cWriteBytes(cfg_param, sizeof(cfg_param));
  231. } else {
  232. SERIALPRINT.println("This function only support for DHT10");
  233. return 0;
  234. }
  235. }
  236. /** Read temp & humi result buf from sensor.
  237. total 6 bytes,the first byte for status register,other 5 bytes for temp & humidity data.
  238. @ return : 0 if success, non-zero if failed.
  239. **/
  240. int DHT::readTargetData(uint32_t* data) {
  241. uint8_t statu = 0;
  242. uint8_t bytes[6] = {0};
  243. uint8_t cfg_params[] = {0xac, 0x33, 0x00};
  244. int ret = 0;
  245. if (_type == DHT10) {
  246. if (i2cWriteBytes(cfg_params, sizeof(cfg_params))) {
  247. return -1;
  248. }
  249. delay(75);
  250. // check device busy flag, bit[7]:1 for busy, 0 for idle.
  251. while (statu & 0x80 == 0x80) {
  252. SERIALPRINT.println("Device busy!");
  253. delay(200);
  254. if (i2cReadByte(statu)) {
  255. return -1;
  256. }
  257. }
  258. if (i2cReadBytes(bytes, sizeof(bytes))) {
  259. return -1;
  260. }
  261. data[HUMIDITY_INDEX] = (data[HUMIDITY_INDEX] | bytes[1]) << 8;
  262. data[HUMIDITY_INDEX] = (data[HUMIDITY_INDEX] | bytes[2]) << 8;
  263. data[HUMIDITY_INDEX] = (data[HUMIDITY_INDEX] | bytes[3]);
  264. data[HUMIDITY_INDEX] = data[HUMIDITY_INDEX] >> 4;
  265. data[TEMPRATURE_INDEX] = (data[TEMPRATURE_INDEX] | bytes[3]) << 8;
  266. data[TEMPRATURE_INDEX] = (data[TEMPRATURE_INDEX] | bytes[4]) << 8;
  267. data[TEMPRATURE_INDEX] = (data[TEMPRATURE_INDEX] | bytes[5]);
  268. data[TEMPRATURE_INDEX] &= 0xfffff;
  269. return 0;
  270. } else {
  271. SERIALPRINT.println("This function only support for DHT10");
  272. return 0;
  273. }
  274. }
  275. /** DHT10 Init function.
  276. Reset sensor and wait for calibration complete.
  277. @ return : 0 if success, non-zero if failed.
  278. **/
  279. int DHT::DHT10Init(void) {
  280. int ret = 0;
  281. int cnt = 0;
  282. if (_type == DHT10) {
  283. delay(500);
  284. DHT10Reset();
  285. delay(300);
  286. ret = setSystemCfg();
  287. if (ret) {
  288. SERIALPRINT.println("Failed to set system conf reg \n");
  289. }
  290. //SERIALPRINT.println("Set system cfg OK!");
  291. delay(500);
  292. while (DHT10ReadStatus() == 0) {
  293. SERIALPRINT.println("get status error!");
  294. DHT10Reset();
  295. delay(500);
  296. if (setSystemCfg()) {
  297. SERIALPRINT.println("Failed to set system conf reg \n");
  298. }
  299. delay(500);
  300. cnt++;
  301. if (cnt > 5) {
  302. return -1;
  303. }
  304. }
  305. return 0;
  306. } else {
  307. SERIALPRINT.println("This function only support for DHT10");
  308. return 0;
  309. }
  310. }
  311. /*****************************************************************************/
  312. /*****************************************************************************/
  313. int DHT::i2cReadByte(uint8_t& byte) {
  314. int cnt = 0;
  315. Wire.requestFrom(DEFAULT_IIC_ADDR, 1);
  316. while (1 != Wire.available()) {
  317. cnt++;
  318. if (cnt >= 10) {
  319. return -1;
  320. }
  321. delay(1);
  322. }
  323. byte = Wire.read();
  324. return 0;
  325. }
  326. int DHT::i2cReadBytes(uint8_t* bytes, uint32_t len) {
  327. int cnt = 0;
  328. Wire.requestFrom(DEFAULT_IIC_ADDR, len);
  329. while (len != Wire.available()) {
  330. cnt++;
  331. if (cnt >= 10) {
  332. return -1;
  333. }
  334. delay(1);
  335. }
  336. for (int i = 0; i < len; i++) {
  337. bytes[i] = Wire.read();
  338. }
  339. return 0;
  340. }
  341. int DHT::i2cWriteBytes(uint8_t* bytes, uint32_t len) {
  342. Wire.beginTransmission(DEFAULT_IIC_ADDR);
  343. for (int i = 0; i < len; i++) {
  344. Wire.write(bytes[i]);
  345. }
  346. return Wire.endTransmission();
  347. }
  348. int DHT::i2cWriteByte(uint8_t byte) {
  349. Wire.beginTransmission(DEFAULT_IIC_ADDR);
  350. Wire.write(byte);
  351. return Wire.endTransmission();
  352. }