#ifdef ENABLE_SENSOR_DHT22

void DHT_readTemp() {
  //delay(DHTreadDelay);
  sensor_DHT_tmpTemp = dht.readTemperature();  // Read temperature as Celsius (the default)
}
void DHT_readHum() {
  //delay(DHTreadDelay);
  sensor_DHT_tmpHum = dht.readHumidity();
}

void DHT_logCurrentData() {
  sprintf_P(logBuf, "DHT: temp=%2.1f, hum=%u, tempRaw=%2.1f, humRaw=%u", sensor_DHT_currTemp, sensor_DHT_currHum, sensor_DHT_currTemp_raw, sensor_DHT_currHum_raw);
  sendLog(logBuf, LOGLEVEL_INFO);
}

void DHT_measureTempHum() {

  for (int _i = 0; _i < sensor_DHT_readRetries; _i++) {
    DHT_readTemp();
    if (!isnan(sensor_DHT_tmpTemp)) {
      sendLog(F("DHT: reading Temp OK"), LOGLEVEL_VERBOSE);
      _i = sensor_DHT_readRetries;
    }
    else {
      sendLog(F("DHT: reading Temp failed - retrying.."), LOGLEVEL_INFO);
    }
  }
//
  for (int _i = 0; _i < sensor_DHT_readRetries; _i++) {
    DHT_readHum();
    if (!isnan(sensor_DHT_tmpHum)) {
      sendLog(F("DHT: reading Hum OK"), LOGLEVEL_VERBOSE);
      _i = sensor_DHT_readRetries;
    }
    else {
      sendLog(F("DHT: reading Hum failed - retrying.."), LOGLEVEL_INFO);
    }
  }

  // Check if any reads failed
  if (isnan(sensor_DHT_tmpHum) || isnan(sensor_DHT_tmpTemp)) {
    //Serial.println("Failed to read from DHT sensor!");
    sendLog(F("DHT: Failed to read sensor."), LOGLEVEL_ERROR);
  }
  else {
    sensor_DHT_tmpTemp = sensor_DHT_tmpTemp + confSens.DHT_tempCorrVal;
    sensor_DHT_tmpHum = round(sensor_DHT_tmpHum) + confSens.DHT_humCorrVal;
    int _tmpHumInt = sensor_DHT_tmpHum;
    if (sensor_DHT_tmpTemp < 50.0 && sensor_DHT_tmpTemp > -20.0) {
      // measurement is in range
      sensor_DHT_currTemp_raw = sensor_DHT_tmpTemp;
      sensor_DHT_currHum_raw = _tmpHumInt;

      if ( sensor_DHT_lastUpdate > 0 && sensor_DHT_tmpTemp <= ( sensor_DHT_currTemp + 2.0 ) && sensor_DHT_tmpTemp >= ( sensor_DHT_currTemp - 2.0 ) ) {
        // temp has already been measured - only accept new measurement if it does not differ much from the last value
        //Temp = (Temp * (FilterFaktor -1) + AktuellerMesswert) / FilterFaktor;
        //temperature = tmpTemp;
        sensor_DHT_currTemp = (sensor_DHT_currTemp * 9 + sensor_DHT_tmpTemp) / 10; // filter
        sensor_DHT_currHum = (sensor_DHT_currHum * 9 + _tmpHumInt) / 10; // filter
        sensor_DHT_lastUpdate = millis();
      }
      else if ( sensor_DHT_lastUpdate == 0 || (millis() - sensor_DHT_lastUpdate) > sensors_maxMeasurementAge ) {
        // this is the first measurement or the last one is older than 5m - then accept this measurement
        sensor_DHT_currTemp = sensor_DHT_tmpTemp + confSens.DHT_tempCorrVal;
        sensor_DHT_currHum = _tmpHumInt + confSens.DHT_humCorrVal;
        sensor_DHT_lastUpdate = millis();

      }
      // skip in all other cases

      //#ifdef DEBUG_VERBOSE
      //      Serial.print("lastTempUpdate: ");
      //      long lastTempUpdateDelta = millis() - lastTempUpdate;
      //      Serial.print(lastTempUpdateDelta / 1000);
      //      Serial.println("s ago");
      //#endif
    }
  }
}

#endif
