A minimal library for the DHT11.
Fork of DHT11 by
Dht22.cpp@2:983fdd709bf7, 2017-11-09 (annotated)
- Committer:
- amithy
- Date:
- Thu Nov 09 22:14:56 2017 +0000
- Revision:
- 2:983fdd709bf7
- Parent:
- Dht11.cpp@1:5da6f6de3e42
test export
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
amithy | 2:983fdd709bf7 | 1 | #include "Dht22.h" |
fossum_13 | 0:c1da310d3e8a | 2 | |
amithy | 2:983fdd709bf7 | 3 | Dht22::Dht22(PinName const &p) : _pin(p) { |
fossum_13 | 0:c1da310d3e8a | 4 | // Set creation time so we can make |
fossum_13 | 0:c1da310d3e8a | 5 | // sure we pause at least 1 second for |
fossum_13 | 0:c1da310d3e8a | 6 | // startup. |
fossum_13 | 0:c1da310d3e8a | 7 | _timer.start(); |
fossum_13 | 1:5da6f6de3e42 | 8 | |
fossum_13 | 1:5da6f6de3e42 | 9 | _temperature = 0; |
fossum_13 | 1:5da6f6de3e42 | 10 | _humidity = 0; |
fossum_13 | 0:c1da310d3e8a | 11 | } |
fossum_13 | 0:c1da310d3e8a | 12 | |
amithy | 2:983fdd709bf7 | 13 | int Dht22::read() |
fossum_13 | 0:c1da310d3e8a | 14 | { |
fossum_13 | 0:c1da310d3e8a | 15 | // BUFFER TO RECEIVE |
fossum_13 | 0:c1da310d3e8a | 16 | uint8_t bits[5]; |
fossum_13 | 0:c1da310d3e8a | 17 | uint8_t cnt = 7; |
fossum_13 | 0:c1da310d3e8a | 18 | uint8_t idx = 0; |
fossum_13 | 0:c1da310d3e8a | 19 | |
fossum_13 | 0:c1da310d3e8a | 20 | // EMPTY BUFFER |
fossum_13 | 0:c1da310d3e8a | 21 | for (int i=0; i< 5; i++) bits[i] = 0; |
fossum_13 | 0:c1da310d3e8a | 22 | |
fossum_13 | 0:c1da310d3e8a | 23 | // Verify sensor settled after boot |
fossum_13 | 0:c1da310d3e8a | 24 | while(_timer.read_ms() < 1500) {} |
fossum_13 | 0:c1da310d3e8a | 25 | _timer.stop(); |
fossum_13 | 0:c1da310d3e8a | 26 | |
fossum_13 | 0:c1da310d3e8a | 27 | // Notify it we are ready to read |
fossum_13 | 0:c1da310d3e8a | 28 | _pin.output(); |
fossum_13 | 0:c1da310d3e8a | 29 | _pin = 0; |
fossum_13 | 0:c1da310d3e8a | 30 | wait_ms(18); |
fossum_13 | 0:c1da310d3e8a | 31 | _pin = 1; |
fossum_13 | 0:c1da310d3e8a | 32 | wait_us(40); |
fossum_13 | 0:c1da310d3e8a | 33 | _pin.input(); |
fossum_13 | 0:c1da310d3e8a | 34 | |
fossum_13 | 0:c1da310d3e8a | 35 | // ACKNOWLEDGE or TIMEOUT |
fossum_13 | 0:c1da310d3e8a | 36 | unsigned int loopCnt = 10000; |
fossum_13 | 0:c1da310d3e8a | 37 | while(_pin == 0) |
fossum_13 | 0:c1da310d3e8a | 38 | if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; |
fossum_13 | 0:c1da310d3e8a | 39 | |
fossum_13 | 0:c1da310d3e8a | 40 | loopCnt = 10000; |
fossum_13 | 0:c1da310d3e8a | 41 | while(_pin == 1) |
fossum_13 | 0:c1da310d3e8a | 42 | if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; |
fossum_13 | 0:c1da310d3e8a | 43 | |
fossum_13 | 0:c1da310d3e8a | 44 | // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT |
fossum_13 | 0:c1da310d3e8a | 45 | for (int i=0; i<40; i++) |
fossum_13 | 0:c1da310d3e8a | 46 | { |
fossum_13 | 0:c1da310d3e8a | 47 | loopCnt = 10000; |
fossum_13 | 0:c1da310d3e8a | 48 | while(_pin == 0) |
fossum_13 | 0:c1da310d3e8a | 49 | if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; |
fossum_13 | 0:c1da310d3e8a | 50 | |
fossum_13 | 0:c1da310d3e8a | 51 | //unsigned long t = micros(); |
fossum_13 | 0:c1da310d3e8a | 52 | Timer t; |
fossum_13 | 0:c1da310d3e8a | 53 | t. start(); |
fossum_13 | 0:c1da310d3e8a | 54 | |
fossum_13 | 0:c1da310d3e8a | 55 | loopCnt = 10000; |
fossum_13 | 0:c1da310d3e8a | 56 | while(_pin == 1) |
fossum_13 | 0:c1da310d3e8a | 57 | if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; |
fossum_13 | 0:c1da310d3e8a | 58 | |
fossum_13 | 0:c1da310d3e8a | 59 | if (t.read_us() > 40) bits[idx] |= (1 << cnt); |
fossum_13 | 0:c1da310d3e8a | 60 | if (cnt == 0) // next byte? |
fossum_13 | 0:c1da310d3e8a | 61 | { |
fossum_13 | 0:c1da310d3e8a | 62 | cnt = 7; // restart at MSB |
fossum_13 | 0:c1da310d3e8a | 63 | idx++; // next byte! |
fossum_13 | 0:c1da310d3e8a | 64 | } |
fossum_13 | 0:c1da310d3e8a | 65 | else cnt--; |
fossum_13 | 0:c1da310d3e8a | 66 | } |
fossum_13 | 0:c1da310d3e8a | 67 | |
amithy | 2:983fdd709bf7 | 68 | // // WRITE TO RIGHT VARS - DHT22 |
amithy | 2:983fdd709bf7 | 69 | // // as bits[1] and bits[3] are allways zero they are omitted in formulas. |
amithy | 2:983fdd709bf7 | 70 | // _humidity = bits[0]; |
amithy | 2:983fdd709bf7 | 71 | // _temperature = bits[2]; |
amithy | 2:983fdd709bf7 | 72 | // |
amithy | 2:983fdd709bf7 | 73 | // uint8_t sum = bits[0] + bits[2]; |
amithy | 2:983fdd709bf7 | 74 | // |
amithy | 2:983fdd709bf7 | 75 | // if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM; |
amithy | 2:983fdd709bf7 | 76 | |
amithy | 2:983fdd709bf7 | 77 | |
amithy | 2:983fdd709bf7 | 78 | uint8_t dht22_check_sum; |
amithy | 2:983fdd709bf7 | 79 | dht22_check_sum = bits[0] + bits[1] + bits[2] + bits[3]; |
amithy | 2:983fdd709bf7 | 80 | //printf("dht22 check sum: %d",dht22_check_sum); |
amithy | 2:983fdd709bf7 | 81 | dht22_check_sum= dht22_check_sum%256; |
amithy | 2:983fdd709bf7 | 82 | //printf("dht22 check sum: %d",dht22_check_sum); |
amithy | 2:983fdd709bf7 | 83 | |
amithy | 2:983fdd709bf7 | 84 | if (dht22_check_sum == bits[4]) { |
amithy | 2:983fdd709bf7 | 85 | _humidity = bits[0]*256 + bits[1]; |
amithy | 2:983fdd709bf7 | 86 | |
amithy | 2:983fdd709bf7 | 87 | _temperature = bits[2]*256 + bits[3]; |
amithy | 2:983fdd709bf7 | 88 | //printf("all good \n"); |
amithy | 2:983fdd709bf7 | 89 | return DHTLIB_OK; |
amithy | 2:983fdd709bf7 | 90 | } |
amithy | 2:983fdd709bf7 | 91 | return DHTLIB_ERROR_CHECKSUM; |
fossum_13 | 0:c1da310d3e8a | 92 | } |
fossum_13 | 0:c1da310d3e8a | 93 | |
amithy | 2:983fdd709bf7 | 94 | float Dht22::getFahrenheit() { |
fossum_13 | 1:5da6f6de3e42 | 95 | return((_temperature * 1.8) + 32); |
fossum_13 | 1:5da6f6de3e42 | 96 | } |
fossum_13 | 1:5da6f6de3e42 | 97 | |
amithy | 2:983fdd709bf7 | 98 | int Dht22::getCelsius() { |
fossum_13 | 0:c1da310d3e8a | 99 | return(_temperature); |
fossum_13 | 0:c1da310d3e8a | 100 | } |
fossum_13 | 0:c1da310d3e8a | 101 | |
amithy | 2:983fdd709bf7 | 102 | int Dht22::getHumidity() { |
fossum_13 | 0:c1da310d3e8a | 103 | return(_humidity); |
fossum_13 | 0:c1da310d3e8a | 104 | } |