DHT.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. if(_type == DHT10){
  69. float temp[2];
  70. readTempAndHumidity(temp);
  71. if (S) {
  72. temp[1] = convertCtoF(temp[1]);
  73. }
  74. return temp[1];
  75. }
  76. else{
  77. float f;
  78. if (read()) {
  79. switch (_type) {
  80. case DHT11:
  81. f = data[2];
  82. if(data[3]%128<10){
  83. f += data[3]%128/10.0f;
  84. }else if(data[3]%128<100){
  85. f += data[3]%128/100.0f;
  86. }else{
  87. f += data[3]%128/1000.0f;
  88. }
  89. if(data[3]>=128){ // The left-most digit indicate the negative sign.
  90. f = -f;
  91. }
  92. if (S) {
  93. f = convertCtoF(f);
  94. }
  95. return f;
  96. case DHT22:
  97. case DHT21:
  98. f = data[2] & 0x7F;
  99. f *= 256;
  100. f += data[3];
  101. f /= 10;
  102. if (data[2] & 0x80) {
  103. f *= -1;
  104. }
  105. if (S) {
  106. f = convertCtoF(f);
  107. }
  108. return f;
  109. }
  110. }
  111. }
  112. DEBUG_PRINT("Read fail");
  113. return NAN;
  114. }
  115. float DHT::convertCtoF(float c) {
  116. return c * 9 / 5 + 32;
  117. }
  118. float DHT::readHumidity(void) {
  119. if(_type == DHT10){
  120. float temp[2];
  121. readTempAndHumidity(temp);
  122. return temp[0];
  123. }
  124. else{
  125. float f;
  126. if (read()) {
  127. switch (_type) {
  128. case DHT11:
  129. f = data[0];
  130. return f;
  131. case DHT22:
  132. case DHT21:
  133. f = data[0];
  134. f *= 256;
  135. f += data[1];
  136. f /= 10;
  137. return f;
  138. }
  139. }
  140. }
  141. DEBUG_PRINT("Read fail");
  142. return NAN;
  143. }
  144. boolean DHT::read(void) {
  145. uint8_t laststate = HIGH;
  146. uint8_t counter = 0;
  147. uint8_t j = 0, i;
  148. unsigned long currenttime;
  149. // pull the pin high and wait 250 milliseconds
  150. digitalWrite(_pin, HIGH);
  151. delay(250);
  152. currenttime = millis();
  153. if (currenttime < _lastreadtime) {
  154. // ie there was a rollover
  155. _lastreadtime = 0;
  156. }
  157. if (!firstreading && ((currenttime - _lastreadtime) < 2000)) {
  158. return true; // return last correct measurement
  159. //delay(2000 - (currenttime - _lastreadtime));
  160. }
  161. firstreading = false;
  162. /*
  163. DEBUG_PRINT("Currtime: "); DEBUG_PRINT(currenttime);
  164. DEBUG_PRINT(" Lasttime: "); DEBUG_PRINT(_lastreadtime);
  165. */
  166. _lastreadtime = millis();
  167. data[0] = data[1] = data[2] = data[3] = data[4] = 0;
  168. // now pull it low for ~20 milliseconds
  169. pinMode(_pin, OUTPUT);
  170. digitalWrite(_pin, LOW);
  171. delay(20);
  172. //cli();
  173. digitalWrite(_pin, HIGH);
  174. delayMicroseconds(40);
  175. pinMode(_pin, INPUT);
  176. // read in timings
  177. for (i = 0; i < MAXTIMINGS; i++) {
  178. counter = 0;
  179. while (digitalRead(_pin) == laststate) {
  180. counter++;
  181. delayMicroseconds(1);
  182. if (counter == 255) {
  183. break;
  184. }
  185. }
  186. laststate = digitalRead(_pin);
  187. if (counter == 255) {
  188. break;
  189. }
  190. // ignore first 3 transitions
  191. if ((i >= 4) && (i % 2 == 0)) {
  192. // shove each bit into the storage bytes
  193. data[j / 8] <<= 1;
  194. if (counter > _count) {
  195. data[j / 8] |= 1;
  196. }
  197. j++;
  198. }
  199. }
  200. //sei();
  201. /*
  202. DEBUG_PRINTln(j, DEC);
  203. DEBUG_PRINT(data[0], HEX); DEBUG_PRINT(", ");
  204. DEBUG_PRINT(data[1], HEX); DEBUG_PRINT(", ");
  205. DEBUG_PRINT(data[2], HEX); DEBUG_PRINT(", ");
  206. DEBUG_PRINT(data[3], HEX); DEBUG_PRINT(", ");
  207. DEBUG_PRINT(data[4], HEX); DEBUG_PRINT(" =? ");
  208. DEBUG_PRINTln(data[0] + data[1] + data[2] + data[3], HEX);
  209. */
  210. // check we read 40 bits and that the checksum matches
  211. if ((j >= 40) &&
  212. (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))) {
  213. return true;
  214. }
  215. return false;
  216. }
  217. /*****************************************************************************/
  218. /*****************************************************************************/
  219. /** Reset sensor.
  220. @return 0 for calibrated failed,1 for succeed.
  221. **/
  222. int DHT::DHT10Reset(void) {
  223. if (_type == DHT10) {
  224. return i2cWriteByte(RESET_REG_ADDR);
  225. } else {
  226. return 0;
  227. SERIALPRINT.println("This function only support for DHT10");
  228. }
  229. }
  230. /** Read status register.check the calibration flag - bit[3]: 1- calibrated ok ,0 - Not calibrated.
  231. @return 0 for calibrated failed,1 for succeed.
  232. **/
  233. int DHT::DHT10ReadStatus(void) {
  234. int ret = 0;
  235. uint8_t statu = 0;
  236. if (_type == DHT10) {
  237. ret = i2cReadByte(statu);
  238. if (ret) {
  239. SERIALPRINT.println("Failed to read byte\n");
  240. }
  241. if ((statu & 0x8) == 0x8) {
  242. return 1;
  243. } else {
  244. return 0;
  245. }
  246. } else {
  247. SERIALPRINT.println("This function only support for DHT10");
  248. return 0;
  249. }
  250. }
  251. /** Init sensor,send 0x08,0x00 to register 0xe1.
  252. @ return : 0 if success, non-zero if failed.
  253. **/
  254. int DHT::setSystemCfg(void) {
  255. uint8_t cfg_param[] = {0xe1, 0x08, 0x00};
  256. if (_type == DHT10) {
  257. return i2cWriteBytes(cfg_param, sizeof(cfg_param));
  258. } else {
  259. SERIALPRINT.println("This function only support for DHT10");
  260. return 0;
  261. }
  262. }
  263. /** Read temp & humi result buf from sensor.
  264. total 6 bytes,the first byte for status register,other 5 bytes for temp & humidity data.
  265. @ return : 0 if success, non-zero if failed.
  266. **/
  267. int DHT::readTargetData(uint32_t* data) {
  268. uint8_t statu = 0;
  269. uint8_t bytes[6] = {0};
  270. uint8_t cfg_params[] = {0xac, 0x33, 0x00};
  271. int ret = 0;
  272. if (_type == DHT10) {
  273. if (i2cWriteBytes(cfg_params, sizeof(cfg_params))) {
  274. return -1;
  275. }
  276. delay(75);
  277. // check device busy flag, bit[7]:1 for busy, 0 for idle.
  278. while (statu & 0x80 == 0x80) {
  279. SERIALPRINT.println("Device busy!");
  280. delay(200);
  281. if (i2cReadByte(statu)) {
  282. return -1;
  283. }
  284. }
  285. if (i2cReadBytes(bytes, sizeof(bytes))) {
  286. return -1;
  287. }
  288. data[HUMIDITY_INDEX] = (data[HUMIDITY_INDEX] | bytes[1]) << 8;
  289. data[HUMIDITY_INDEX] = (data[HUMIDITY_INDEX] | bytes[2]) << 8;
  290. data[HUMIDITY_INDEX] = (data[HUMIDITY_INDEX] | bytes[3]);
  291. data[HUMIDITY_INDEX] = data[HUMIDITY_INDEX] >> 4;
  292. data[TEMPRATURE_INDEX] = (data[TEMPRATURE_INDEX] | bytes[3]) << 8;
  293. data[TEMPRATURE_INDEX] = (data[TEMPRATURE_INDEX] | bytes[4]) << 8;
  294. data[TEMPRATURE_INDEX] = (data[TEMPRATURE_INDEX] | bytes[5]);
  295. data[TEMPRATURE_INDEX] &= 0xfffff;
  296. return 0;
  297. } else {
  298. SERIALPRINT.println("This function only support for DHT10");
  299. return 0;
  300. }
  301. }
  302. /** DHT10 Init function.
  303. Reset sensor and wait for calibration complete.
  304. @ return : 0 if success, non-zero if failed.
  305. **/
  306. int DHT::DHT10Init(void) {
  307. int ret = 0;
  308. int cnt = 0;
  309. if (_type == DHT10) {
  310. delay(500);
  311. DHT10Reset();
  312. delay(300);
  313. ret = setSystemCfg();
  314. if (ret) {
  315. SERIALPRINT.println("Failed to set system conf reg \n");
  316. }
  317. //SERIALPRINT.println("Set system cfg OK!");
  318. delay(500);
  319. while (DHT10ReadStatus() == 0) {
  320. SERIALPRINT.println("get status error!");
  321. DHT10Reset();
  322. delay(500);
  323. if (setSystemCfg()) {
  324. SERIALPRINT.println("Failed to set system conf reg \n");
  325. }
  326. delay(500);
  327. cnt++;
  328. if (cnt > 5) {
  329. return -1;
  330. }
  331. }
  332. return 0;
  333. } else {
  334. SERIALPRINT.println("This function only support for DHT10");
  335. return 0;
  336. }
  337. }
  338. /*****************************************************************************/
  339. /*****************************************************************************/
  340. int DHT::i2cReadByte(uint8_t& byte) {
  341. int cnt = 0;
  342. Wire.requestFrom(DEFAULT_IIC_ADDR, 1);
  343. while (1 != Wire.available()) {
  344. cnt++;
  345. if (cnt >= 10) {
  346. return -1;
  347. }
  348. delay(1);
  349. }
  350. byte = Wire.read();
  351. return 0;
  352. }
  353. int DHT::i2cReadBytes(uint8_t* bytes, uint32_t len) {
  354. int cnt = 0;
  355. Wire.requestFrom(DEFAULT_IIC_ADDR, len);
  356. while (len != Wire.available()) {
  357. cnt++;
  358. if (cnt >= 10) {
  359. return -1;
  360. }
  361. delay(1);
  362. }
  363. for (int i = 0; i < len; i++) {
  364. bytes[i] = Wire.read();
  365. }
  366. return 0;
  367. }
  368. int DHT::i2cWriteBytes(uint8_t* bytes, uint32_t len) {
  369. Wire.beginTransmission(DEFAULT_IIC_ADDR);
  370. for (int i = 0; i < len; i++) {
  371. Wire.write(bytes[i]);
  372. }
  373. return Wire.endTransmission();
  374. }
  375. int DHT::i2cWriteByte(uint8_t byte) {
  376. Wire.beginTransmission(DEFAULT_IIC_ADDR);
  377. Wire.write(byte);
  378. return Wire.endTransmission();
  379. }