Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MbedJSONValue HTTPClient TFT_fonts SPI_TFT_ILI9341 mbed-rtos picojsontest NTPClient SDFileSystem EthernetInterface DHT Stepper_Motor_X27168
Dht11.cpp@4:1eea5edaffc4, 2019-05-01 (annotated)
- Committer:
- aalbul3
- Date:
- Wed May 01 04:11:18 2019 +0000
- Revision:
- 4:1eea5edaffc4
This is a test
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| aalbul3 | 4:1eea5edaffc4 | 1 | #include "Dht11.h" |
| aalbul3 | 4:1eea5edaffc4 | 2 | |
| aalbul3 | 4:1eea5edaffc4 | 3 | Dht11::Dht11(PinName const &p) : _pin(p) { |
| aalbul3 | 4:1eea5edaffc4 | 4 | // Set creation time so we can make |
| aalbul3 | 4:1eea5edaffc4 | 5 | // sure we pause at least 1 second for |
| aalbul3 | 4:1eea5edaffc4 | 6 | // startup. |
| aalbul3 | 4:1eea5edaffc4 | 7 | _timer.start(); |
| aalbul3 | 4:1eea5edaffc4 | 8 | |
| aalbul3 | 4:1eea5edaffc4 | 9 | _temperature = 0; |
| aalbul3 | 4:1eea5edaffc4 | 10 | _humidity = 0; |
| aalbul3 | 4:1eea5edaffc4 | 11 | } |
| aalbul3 | 4:1eea5edaffc4 | 12 | |
| aalbul3 | 4:1eea5edaffc4 | 13 | int Dht11::read() |
| aalbul3 | 4:1eea5edaffc4 | 14 | { |
| aalbul3 | 4:1eea5edaffc4 | 15 | // BUFFER TO RECEIVE |
| aalbul3 | 4:1eea5edaffc4 | 16 | uint8_t bits[5]; |
| aalbul3 | 4:1eea5edaffc4 | 17 | uint8_t cnt = 7; |
| aalbul3 | 4:1eea5edaffc4 | 18 | uint8_t idx = 0; |
| aalbul3 | 4:1eea5edaffc4 | 19 | |
| aalbul3 | 4:1eea5edaffc4 | 20 | // EMPTY BUFFER |
| aalbul3 | 4:1eea5edaffc4 | 21 | for (int i=0; i< 5; i++) bits[i] = 0; |
| aalbul3 | 4:1eea5edaffc4 | 22 | |
| aalbul3 | 4:1eea5edaffc4 | 23 | // Verify sensor settled after boot |
| aalbul3 | 4:1eea5edaffc4 | 24 | while(_timer.read_ms() < 1500) {} |
| aalbul3 | 4:1eea5edaffc4 | 25 | _timer.stop(); |
| aalbul3 | 4:1eea5edaffc4 | 26 | |
| aalbul3 | 4:1eea5edaffc4 | 27 | // Notify it we are ready to read |
| aalbul3 | 4:1eea5edaffc4 | 28 | _pin.output(); |
| aalbul3 | 4:1eea5edaffc4 | 29 | _pin = 0; |
| aalbul3 | 4:1eea5edaffc4 | 30 | wait_ms(18); |
| aalbul3 | 4:1eea5edaffc4 | 31 | _pin = 1; |
| aalbul3 | 4:1eea5edaffc4 | 32 | wait_us(40); |
| aalbul3 | 4:1eea5edaffc4 | 33 | _pin.input(); |
| aalbul3 | 4:1eea5edaffc4 | 34 | |
| aalbul3 | 4:1eea5edaffc4 | 35 | // ACKNOWLEDGE or TIMEOUT |
| aalbul3 | 4:1eea5edaffc4 | 36 | unsigned int loopCnt = 10000; |
| aalbul3 | 4:1eea5edaffc4 | 37 | while(_pin == 0) |
| aalbul3 | 4:1eea5edaffc4 | 38 | if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; |
| aalbul3 | 4:1eea5edaffc4 | 39 | |
| aalbul3 | 4:1eea5edaffc4 | 40 | loopCnt = 10000; |
| aalbul3 | 4:1eea5edaffc4 | 41 | while(_pin == 1) |
| aalbul3 | 4:1eea5edaffc4 | 42 | if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; |
| aalbul3 | 4:1eea5edaffc4 | 43 | |
| aalbul3 | 4:1eea5edaffc4 | 44 | // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT |
| aalbul3 | 4:1eea5edaffc4 | 45 | for (int i=0; i<40; i++) |
| aalbul3 | 4:1eea5edaffc4 | 46 | { |
| aalbul3 | 4:1eea5edaffc4 | 47 | loopCnt = 10000; |
| aalbul3 | 4:1eea5edaffc4 | 48 | while(_pin == 0) |
| aalbul3 | 4:1eea5edaffc4 | 49 | if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; |
| aalbul3 | 4:1eea5edaffc4 | 50 | |
| aalbul3 | 4:1eea5edaffc4 | 51 | //unsigned long t = micros(); |
| aalbul3 | 4:1eea5edaffc4 | 52 | Timer t; |
| aalbul3 | 4:1eea5edaffc4 | 53 | t. start(); |
| aalbul3 | 4:1eea5edaffc4 | 54 | |
| aalbul3 | 4:1eea5edaffc4 | 55 | loopCnt = 10000; |
| aalbul3 | 4:1eea5edaffc4 | 56 | while(_pin == 1) |
| aalbul3 | 4:1eea5edaffc4 | 57 | if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; |
| aalbul3 | 4:1eea5edaffc4 | 58 | |
| aalbul3 | 4:1eea5edaffc4 | 59 | if (t.read_us() > 40) bits[idx] |= (1 << cnt); |
| aalbul3 | 4:1eea5edaffc4 | 60 | if (cnt == 0) // next byte? |
| aalbul3 | 4:1eea5edaffc4 | 61 | { |
| aalbul3 | 4:1eea5edaffc4 | 62 | cnt = 7; // restart at MSB |
| aalbul3 | 4:1eea5edaffc4 | 63 | idx++; // next byte! |
| aalbul3 | 4:1eea5edaffc4 | 64 | } |
| aalbul3 | 4:1eea5edaffc4 | 65 | else cnt--; |
| aalbul3 | 4:1eea5edaffc4 | 66 | } |
| aalbul3 | 4:1eea5edaffc4 | 67 | |
| aalbul3 | 4:1eea5edaffc4 | 68 | // WRITE TO RIGHT VARS |
| aalbul3 | 4:1eea5edaffc4 | 69 | // as bits[1] and bits[3] are allways zero they are omitted in formulas. |
| aalbul3 | 4:1eea5edaffc4 | 70 | _humidity = bits[0]; |
| aalbul3 | 4:1eea5edaffc4 | 71 | _temperature = bits[2]; |
| aalbul3 | 4:1eea5edaffc4 | 72 | |
| aalbul3 | 4:1eea5edaffc4 | 73 | uint8_t sum = bits[0] + bits[2]; |
| aalbul3 | 4:1eea5edaffc4 | 74 | |
| aalbul3 | 4:1eea5edaffc4 | 75 | if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM; |
| aalbul3 | 4:1eea5edaffc4 | 76 | return DHTLIB_OK; |
| aalbul3 | 4:1eea5edaffc4 | 77 | } |
| aalbul3 | 4:1eea5edaffc4 | 78 | |
| aalbul3 | 4:1eea5edaffc4 | 79 | float Dht11::getFahrenheit() { |
| aalbul3 | 4:1eea5edaffc4 | 80 | return((_temperature * 1.8) + 32); |
| aalbul3 | 4:1eea5edaffc4 | 81 | } |
| aalbul3 | 4:1eea5edaffc4 | 82 | |
| aalbul3 | 4:1eea5edaffc4 | 83 | int Dht11::getCelsius() { |
| aalbul3 | 4:1eea5edaffc4 | 84 | return(_temperature); |
| aalbul3 | 4:1eea5edaffc4 | 85 | } |
| aalbul3 | 4:1eea5edaffc4 | 86 | |
| aalbul3 | 4:1eea5edaffc4 | 87 | int Dht11::getHumidity() { |
| aalbul3 | 4:1eea5edaffc4 | 88 | return(_humidity); |
| aalbul3 | 4:1eea5edaffc4 | 89 | } |