// return values: // DHTLIB_OK // DHTLIB_ERROR_CHECKSUM // DHTLIB_ERROR_TIMEOUT int DHT2pin::read() { // READ VALUES int rv = _readSensor(DHTLIB_DHT_WAKEUP); if (rv != DHTLIB_OK) { humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered? temperature = DHTLIB_INVALID_VALUE; // invalid value return rv; // propagate error value } // CONVERT AND STORE humidity = word(bits[0], bits[1]) * 0.1; temperature = word(bits[2] & 0x7F, bits[3]) * 0.1; if (bits[2] & 0x80) // negative temperature { temperature = -temperature; } // TEST CHECKSUM uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3]; if (bits[4] != sum) { return DHTLIB_ERROR_CHECKSUM; } return DHTLIB_OK; }
int8_t dht::read(uint8_t pin) { // READ VALUES int8_t result = _readSensor(pin, DHTLIB_DHT_WAKEUP, DHTLIB_DHT_LEADING_ZEROS); // these bits are always zero, masking them reduces errors. bits[0] &= 0x03; bits[2] &= 0x83; // CONVERT AND STORE humidity = (bits[0]*256 + bits[1]) * 0.1; temperature = ((bits[2] & 0x7F)*256 + bits[3]) * 0.1; if (bits[2] & 0x80) // negative temperature { temperature = -temperature; } // TEST CHECKSUM uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3]; if (bits[4] != sum) { return DHTLIB_ERROR_CHECKSUM; } return result; }
// return values: // DHTLIB_OK // DHTLIB_ERROR_CHECKSUM // DHTLIB_ERROR_TIMEOUT int dht::read11(uint8_t pin) { // READ VALUES //if (_disableIRQ) noInterrupts(); int rv = _readSensor(pin, 18); //if (_disableIRQ) interrupts(); if (rv != 0) { //humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered? temperature = -99; // invalid value return rv; } // CONVERT AND STORE //humidity = bits[0] + bits[1] * 0.1; temperature = (bits[2] & 0x7F) + bits[3] * 0.1; if (bits[2] & 0x80) // negative temperature { temperature = -temperature; } // TEST CHECKSUM // sum = bits[0] + bits[1] + bits[2] + bits[3]; //if (bits[4] != sum) //{ // return DHTLIB_ERROR_CHECKSUM; //} //return DHTLIB_OK; }
// return values: // DHTLIB_OK // DHTLIB_ERROR_CHECKSUM // DHTLIB_ERROR_TIMEOUT int DHT2pin::read11() { // READ VALUES int rv = _readSensor(DHTLIB_DHT11_WAKEUP); if (rv != DHTLIB_OK) { humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered? temperature = DHTLIB_INVALID_VALUE; // invalid value return rv; } // CONVERT AND STORE humidity = bits[0]; // bits[1] == 0; temperature = bits[2]; // bits[3] == 0; // TEST CHECKSUM // bits[1] && bits[3] both 0 uint8_t sum = bits[0] + bits[2]; if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM; return DHTLIB_OK; }
int8_t dht::read11(uint8_t pin) { // READ VALUES int8_t result = _readSensor(pin, DHTLIB_DHT11_WAKEUP, DHTLIB_DHT11_LEADING_ZEROS); // these bits are always zero, masking them reduces errors. bits[0] &= 0x7F; bits[2] &= 0x7F; // CONVERT AND STORE humidity = bits[0]; // bits[1] == 0; temperature = bits[2]; // bits[3] == 0; // TEST CHECKSUM // bits[1] && bits[3] both 0 uint8_t sum = bits[0] + bits[2]; if (bits[4] != sum) { return DHTLIB_ERROR_CHECKSUM; } return result; }