1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #ifdef ENABLE_SENSOR_DHT22
- #define SENSOR_DHT_MIN_VALUE -25.0
- #define SENSOR_DHT_MAX_VALUE 50.0
- #define SENSOR_DHT_MAXDIFFFROMLASTTEMP 2.0
- #define SENSOR_DHT_FILTER_FACTOR 10
- 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() {
- // try reading temperature
- for (int _i = 0; _i < sensor_DHT_readRetries; _i++) {
- sensor_DHT_tmpTemp = dht.readTemperature(); // read temp in celsius
- if (!isnan(sensor_DHT_tmpTemp)) {
- sendLog(F("DHT: reading Temp OK"), LOGLEVEL_VERBOSE);
- _i = sensor_DHT_readRetries;
- }
- else {
- sendLog(F("DHT: reading Temp failed"), LOGLEVEL_DEBUG);
- }
- }
- // try reading humidity
- for (int _i = 0; _i < sensor_DHT_readRetries; _i++) {
- sensor_DHT_tmpHum = dht.readHumidity();
- if (!isnan(sensor_DHT_tmpHum)) {
- sendLog(F("DHT: reading Hum OK"), LOGLEVEL_VERBOSE);
- _i = sensor_DHT_readRetries;
- }
- else {
- sendLog(F("DHT: reading Hum failed"), LOGLEVEL_DEBUG);
- }
- }
- // Check if any reads failed
- if (isnan(sensor_DHT_tmpHum) || isnan(sensor_DHT_tmpTemp)) {
- 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 <= SENSOR_DHT_MAX_VALUE && sensor_DHT_tmpTemp >= SENSOR_DHT_MIN_VALUE) {
- // 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 + SENSOR_DHT_MAXDIFFFROMLASTTEMP ) && sensor_DHT_tmpTemp >= ( sensor_DHT_currTemp - SENSOR_DHT_MAXDIFFFROMLASTTEMP ) ) {
- // temp has already been measured
- // only accept new measurement if it does not differ too much from the last value
- // then apply filter:
- // Temp = (Temp * (FilterFactor -1) + currentMeasurement) / FilterFactor;
- sensor_DHT_currTemp = (sensor_DHT_currTemp * (SENSOR_DHT_FILTER_FACTOR - 1) + sensor_DHT_tmpTemp) / SENSOR_DHT_FILTER_FACTOR; // filter
- sensor_DHT_currHum = (sensor_DHT_currHum * (SENSOR_DHT_FILTER_FACTOR - 1) + _tmpHumInt) / SENSOR_DHT_FILTER_FACTOR; // 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 - always accept 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
- }
- }
- }
- #endif
|