DHT.cpp 8.7 KB

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