removed silly included mbed library..
Fork of DHT11 by
Dht11.cpp@0:c1da310d3e8a, 2015-02-15 (annotated)
- Committer:
- fossum_13
- Date:
- Sun Feb 15 02:09:00 2015 +0000
- Revision:
- 0:c1da310d3e8a
- Child:
- 1:5da6f6de3e42
Initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fossum_13 | 0:c1da310d3e8a | 1 | #include "Dht11.h" |
fossum_13 | 0:c1da310d3e8a | 2 | |
fossum_13 | 0:c1da310d3e8a | 3 | Dht11::Dht11(PinName 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 | 0:c1da310d3e8a | 8 | } |
fossum_13 | 0:c1da310d3e8a | 9 | |
fossum_13 | 0:c1da310d3e8a | 10 | // Return values: |
fossum_13 | 0:c1da310d3e8a | 11 | // DHTLIB_OK |
fossum_13 | 0:c1da310d3e8a | 12 | // DHTLIB_ERROR_CHECKSUM |
fossum_13 | 0:c1da310d3e8a | 13 | // DHTLIB_ERROR_TIMEOUT |
fossum_13 | 0:c1da310d3e8a | 14 | int Dht11::read() |
fossum_13 | 0:c1da310d3e8a | 15 | { |
fossum_13 | 0:c1da310d3e8a | 16 | // BUFFER TO RECEIVE |
fossum_13 | 0:c1da310d3e8a | 17 | uint8_t bits[5]; |
fossum_13 | 0:c1da310d3e8a | 18 | uint8_t cnt = 7; |
fossum_13 | 0:c1da310d3e8a | 19 | uint8_t idx = 0; |
fossum_13 | 0:c1da310d3e8a | 20 | |
fossum_13 | 0:c1da310d3e8a | 21 | // EMPTY BUFFER |
fossum_13 | 0:c1da310d3e8a | 22 | for (int i=0; i< 5; i++) bits[i] = 0; |
fossum_13 | 0:c1da310d3e8a | 23 | |
fossum_13 | 0:c1da310d3e8a | 24 | // Verify sensor settled after boot |
fossum_13 | 0:c1da310d3e8a | 25 | while(_timer.read_ms() < 1500) {} |
fossum_13 | 0:c1da310d3e8a | 26 | _timer.stop(); |
fossum_13 | 0:c1da310d3e8a | 27 | |
fossum_13 | 0:c1da310d3e8a | 28 | // Notify it we are ready to read |
fossum_13 | 0:c1da310d3e8a | 29 | _pin.output(); |
fossum_13 | 0:c1da310d3e8a | 30 | _pin = 0; |
fossum_13 | 0:c1da310d3e8a | 31 | wait_ms(18); |
fossum_13 | 0:c1da310d3e8a | 32 | _pin = 1; |
fossum_13 | 0:c1da310d3e8a | 33 | wait_us(40); |
fossum_13 | 0:c1da310d3e8a | 34 | _pin.input(); |
fossum_13 | 0:c1da310d3e8a | 35 | |
fossum_13 | 0:c1da310d3e8a | 36 | // ACKNOWLEDGE or TIMEOUT |
fossum_13 | 0:c1da310d3e8a | 37 | unsigned int loopCnt = 10000; |
fossum_13 | 0:c1da310d3e8a | 38 | while(_pin == 0) |
fossum_13 | 0:c1da310d3e8a | 39 | if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; |
fossum_13 | 0:c1da310d3e8a | 40 | |
fossum_13 | 0:c1da310d3e8a | 41 | loopCnt = 10000; |
fossum_13 | 0:c1da310d3e8a | 42 | while(_pin == 1) |
fossum_13 | 0:c1da310d3e8a | 43 | if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; |
fossum_13 | 0:c1da310d3e8a | 44 | |
fossum_13 | 0:c1da310d3e8a | 45 | // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT |
fossum_13 | 0:c1da310d3e8a | 46 | for (int i=0; i<40; i++) |
fossum_13 | 0:c1da310d3e8a | 47 | { |
fossum_13 | 0:c1da310d3e8a | 48 | loopCnt = 10000; |
fossum_13 | 0:c1da310d3e8a | 49 | while(_pin == 0) |
fossum_13 | 0:c1da310d3e8a | 50 | if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; |
fossum_13 | 0:c1da310d3e8a | 51 | |
fossum_13 | 0:c1da310d3e8a | 52 | //unsigned long t = micros(); |
fossum_13 | 0:c1da310d3e8a | 53 | Timer t; |
fossum_13 | 0:c1da310d3e8a | 54 | t. start(); |
fossum_13 | 0:c1da310d3e8a | 55 | |
fossum_13 | 0:c1da310d3e8a | 56 | loopCnt = 10000; |
fossum_13 | 0:c1da310d3e8a | 57 | while(_pin == 1) |
fossum_13 | 0:c1da310d3e8a | 58 | if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; |
fossum_13 | 0:c1da310d3e8a | 59 | |
fossum_13 | 0:c1da310d3e8a | 60 | if (t.read_us() > 40) bits[idx] |= (1 << cnt); |
fossum_13 | 0:c1da310d3e8a | 61 | if (cnt == 0) // next byte? |
fossum_13 | 0:c1da310d3e8a | 62 | { |
fossum_13 | 0:c1da310d3e8a | 63 | cnt = 7; // restart at MSB |
fossum_13 | 0:c1da310d3e8a | 64 | idx++; // next byte! |
fossum_13 | 0:c1da310d3e8a | 65 | } |
fossum_13 | 0:c1da310d3e8a | 66 | else cnt--; |
fossum_13 | 0:c1da310d3e8a | 67 | } |
fossum_13 | 0:c1da310d3e8a | 68 | |
fossum_13 | 0:c1da310d3e8a | 69 | // WRITE TO RIGHT VARS |
fossum_13 | 0:c1da310d3e8a | 70 | // as bits[1] and bits[3] are allways zero they are omitted in formulas. |
fossum_13 | 0:c1da310d3e8a | 71 | _humidity = bits[0]; |
fossum_13 | 0:c1da310d3e8a | 72 | _temperature = bits[2]; |
fossum_13 | 0:c1da310d3e8a | 73 | |
fossum_13 | 0:c1da310d3e8a | 74 | uint8_t sum = bits[0] + bits[2]; |
fossum_13 | 0:c1da310d3e8a | 75 | |
fossum_13 | 0:c1da310d3e8a | 76 | if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM; |
fossum_13 | 0:c1da310d3e8a | 77 | return DHTLIB_OK; |
fossum_13 | 0:c1da310d3e8a | 78 | } |
fossum_13 | 0:c1da310d3e8a | 79 | |
fossum_13 | 0:c1da310d3e8a | 80 | int Dht11::temperature() { |
fossum_13 | 0:c1da310d3e8a | 81 | return(_temperature); |
fossum_13 | 0:c1da310d3e8a | 82 | } |
fossum_13 | 0:c1da310d3e8a | 83 | |
fossum_13 | 0:c1da310d3e8a | 84 | int Dht11::humidity() { |
fossum_13 | 0:c1da310d3e8a | 85 | return(_humidity); |
fossum_13 | 0:c1da310d3e8a | 86 | } |