return values: DHTLIB_OK DHTLIB_ERROR_CHECKSUM DHTLIB_ERROR_TIMEOUT
| 66 | // DHTLIB_ERROR_CHECKSUM |
| 67 | // DHTLIB_ERROR_TIMEOUT |
| 68 | int dht::read(uint8_t pin) |
| 69 | { |
| 70 | // READ VALUES |
| 71 | int rv = _readSensor(pin, DHTLIB_DHT_WAKEUP); |
| 72 | if (rv != DHTLIB_OK) |
| 73 | { |
| 74 | humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered? |
| 75 | temperature = DHTLIB_INVALID_VALUE; // invalid value |
| 76 | return rv; // propagate error value |
| 77 | } |
| 78 | |
| 79 | // CONVERT AND STORE |
| 80 | humidity = word(bits[0], bits[1]) * 0.1; |
| 81 | temperature = word(bits[2] & 0x7F, bits[3]) * 0.1; |
| 82 | if (bits[2] & 0x80) // negative temperature |
| 83 | { |
| 84 | temperature = -temperature; |
| 85 | } |
| 86 | |
| 87 | // TEST CHECKSUM |
| 88 | uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3]; |
| 89 | if (bits[4] != sum) |
| 90 | { |
| 91 | return DHTLIB_ERROR_CHECKSUM; |
| 92 | } |
| 93 | return DHTLIB_OK; |
| 94 | } |
| 95 | |
| 96 | ///////////////////////////////////////////////////// |
| 97 | // |