40 _temperatureSampleIndex(0), _numTemperatureSamples(0),
41 _humiditySampleIndex(0), _numHumiditySamples(0)
50 _bit = digitalPinToBitMask(pin);
51 _port = digitalPinToPort(pin);
55 _maxCycles = microsecondsToClockCycles(1000);
78 _numSamples = numSamples;
80 _temperatureSamples = (int16_t *)malloc(numSamples *
sizeof(int16_t));
81 _humiditySamples = (int16_t *)malloc(numSamples *
sizeof(int16_t));
85 pinMode(_pin, INPUT_PULLUP);
103 if ((millis() - _lastMeasurementTimestamp) < 2000) {
123 int16_t temperature = ~0;
126 if (_statusLastMeasurement !=
true) {
131 temperature = ((_data[2] & 0x7F) << 8) | _data[3];
132 if (_data[2] & 0x80) {
137 if ((_temperatureSamples != NULL) && (temperature != ~0)) {
139 _temperatureSamples[_temperatureSampleIndex++ % _numSamples] = temperature;
142 if (_numTemperatureSamples < _numSamples) {
143 _numTemperatureSamples++;
148 for (uint8_t i = 0; i < _numTemperatureSamples; i++) {
149 temperature += _temperatureSamples[i];
151 temperature /= _numTemperatureSamples;
166 int16_t humidity = ~0;
169 if (_statusLastMeasurement !=
true) {
174 humidity = (_data[0] << 8) | _data[1];
177 if ((_humiditySamples != NULL) && (humidity != ~0)) {
179 _humiditySamples[_humiditySampleIndex++ % _numSamples] = humidity;
182 if (_numHumiditySamples < _numSamples) {
183 _numHumiditySamples++;
188 for (uint8_t i = 0; i < _numHumiditySamples; i++) {
189 humidity += _humiditySamples[i];
191 humidity /= _numHumiditySamples;
215 _lastMeasurementTimestamp = millis();
219 _statusLastMeasurement =
true;
222 if (generateStart() !=
true) {
225 _statusLastMeasurement =
false;
229 if (_statusLastMeasurement) {
230 if (readBytes() !=
true) {
233 _statusLastMeasurement =
false;
238 if (_statusLastMeasurement) {
239 if (((_data[0] + _data[1] + _data[2] + _data[3]) & 0xFF) != _data[4]) {
242 _statusLastMeasurement =
false;
246 return _statusLastMeasurement;
259 bool DHT22::generateStart()
262 digitalWrite(_pin, HIGH);
266 pinMode(_pin, OUTPUT);
267 digitalWrite(_pin, LOW);
271 pinMode(_pin, INPUT_PULLUP);
272 delayMicroseconds(30);
275 if (measurePulseWidth(LOW) == 0) {
280 if (measurePulseWidth(HIGH) == 0) {
298 bool DHT22::readBytes()
305 cycles[i] = measurePulseWidth(LOW);
306 cycles[i + 1] = measurePulseWidth(HIGH);
313 memset(_data, 0,
sizeof(_data));
317 uint32_t lowCycles = cycles[2 * i];
318 uint32_t highCycles = cycles[(2 * i) + 1];
321 if ((lowCycles == 0) || (highCycles == 0)) {
326 int byteIndex = i / 8;
329 _data[byteIndex] <<= 1;
330 if (highCycles > lowCycles) {
331 _data[byteIndex] |= 1;
346 uint32_t DHT22::measurePulseWidth(uint8_t level)
351 while ((*portInputRegister(_port) & _bit) == (level ? _bit : 0)) {
352 if (count++ >= _maxCycles) {
359 while (digitalRead(_pin) == level) {
360 if (count++ >= _maxCycles) {
int16_t readHumidity()
Read humidity from sensor.
DHT22 (AM2302/AM2303) Humidity and Temperature sensor library for Arduino.
void begin(uint8_t numSamples=0)
Initialize sensor.
#define DHT22_NUM_DATA_BITS
#define DEBUG_PRINTLN(...)
Debug print configuration.
#define DHT22_MIN_READ_INTERVAL
Enable debug prints to Serial.
int16_t readTemperature()
Read temperature from sensor.
bool available()
Check if new temperature or humidity read is allowed.
DHT22(uint8_t pin)
Constructor DHT22 sensor.
bool readSensorData()
Read data from sensor.