DHT11

Dependencies:   mbed

Fork of DHT11 by Eric Fossum

Committer:
anhquan
Date:
Thu Jul 12 14:04:04 2018 +0000
Revision:
2:7056c0ba31c8
Parent:
1:5da6f6de3e42
Read data from DHT11 sensor !!! (KL46z)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fossum_13 0:c1da310d3e8a 1 #include "Dht11.h"
fossum_13 0:c1da310d3e8a 2
fossum_13 1:5da6f6de3e42 3 Dht11::Dht11(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
fossum_13 0:c1da310d3e8a 13 int Dht11::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
fossum_13 0:c1da310d3e8a 68 // WRITE TO RIGHT VARS
fossum_13 0:c1da310d3e8a 69 // as bits[1] and bits[3] are allways zero they are omitted in formulas.
fossum_13 0:c1da310d3e8a 70 _humidity = bits[0];
fossum_13 0:c1da310d3e8a 71 _temperature = bits[2];
fossum_13 0:c1da310d3e8a 72
fossum_13 0:c1da310d3e8a 73 uint8_t sum = bits[0] + bits[2];
fossum_13 0:c1da310d3e8a 74
fossum_13 0:c1da310d3e8a 75 if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
fossum_13 0:c1da310d3e8a 76 return DHTLIB_OK;
fossum_13 0:c1da310d3e8a 77 }
fossum_13 0:c1da310d3e8a 78
fossum_13 1:5da6f6de3e42 79 float Dht11::getFahrenheit() {
fossum_13 1:5da6f6de3e42 80 return((_temperature * 1.8) + 32);
fossum_13 1:5da6f6de3e42 81 }
fossum_13 1:5da6f6de3e42 82
fossum_13 1:5da6f6de3e42 83 int Dht11::getCelsius() {
fossum_13 0:c1da310d3e8a 84 return(_temperature);
fossum_13 0:c1da310d3e8a 85 }
fossum_13 0:c1da310d3e8a 86
fossum_13 1:5da6f6de3e42 87 int Dht11::getHumidity() {
fossum_13 0:c1da310d3e8a 88 return(_humidity);
fossum_13 0:c1da310d3e8a 89 }