54 template <
typename TPin>
112 if (!waitForFallingEdge(c_timeout * 2))
return false;
113 if (!waitForRisingEdge())
return false;
119 if (!waitForFallingEdge())
return false;
120 for (uint8_t byteIndex = 0; byteIndex < 5; byteIndex++) {
122 for (uint8_t bitIndex = 0; bitIndex < 8; bitIndex++) {
123 if (!waitForRisingEdge())
return false;
126 if (!waitForFallingEdge())
return false;
128 bits[byteIndex] = byte;
138 m_temperature =
Sensors::word(bits[2] & 0x7F, bits[3]) / 10.0;
142 m_valid = ((uint8_t)(bits[0] + bits[1] + bits[2] + bits[3]) == bits[4]);
147 static const uint16_t c_timeout = F_CPU / 40000;
150 bool m_valid =
false;
151 float m_temperature = 0.0F;
152 float m_humidity = 0.0F;
154 bool waitForEdge(
bool edge, uint16_t timeout = c_timeout) {
156 while (m_pin.isSet() != edge)
157 if (++cycle >= timeout)
return false;
161 bool waitForFallingEdge(uint16_t timeout = c_timeout)
163 return waitForEdge(
false, timeout);
166 bool waitForRisingEdge(uint16_t timeout = c_timeout)
168 return waitForEdge(
true, timeout);
void bitWrite(T &value, uint8_t bit, bool bitValue)
Writes the bit in the given value.
Definition: utils.h:106
Decodes data from DHT22 / AM2302 Temperature and humidity sensors.
Definition: dht22.h:55
Bit manipulation utilities.
Namespace containing all symbols of the AVR C++ utilities library.
Definition: adc.h:48
bool bitRead(T &value, uint8_t bit)
Returns the bit in the given value.
Definition: utils.h:92
float temperature() const
Retrieves the cached temperature value.
Definition: dht22.h:74
float humidity() const
Retrieves the cached humidity value.
Definition: dht22.h:85
bool isValid() const
Determines if the last sensor access was valid.
Definition: dht22.h:65
bool read()
Reads the temperature and humidity from the sensor and updates the cached values. ...
Definition: dht22.h:95
uint16_t word(uint8_t highByte, uint8_t lowByte)
Converts two bytes to a word.
Definition: utils.h:162
Wrapper for accessing digital I/O ports.