sensor_dht.ino 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #ifdef ENABLE_SENSOR_DHT22
  2. #define SENSOR_DHT_MIN_VALUE -25.0
  3. #define SENSOR_DHT_MAX_VALUE 50.0
  4. #define SENSOR_DHT_MAXDIFFFROMLASTTEMP 2.0
  5. #define SENSOR_DHT_FILTER_FACTOR 10
  6. void DHT_logCurrentData() {
  7. 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);
  8. sendLog(logBuf, LOGLEVEL_INFO);
  9. }
  10. void DHT_measureTempHum() {
  11. // try reading temperature
  12. for (int _i = 0; _i < sensor_DHT_readRetries; _i++) {
  13. sensor_DHT_tmpTemp = dht.readTemperature(); // read temp in celsius
  14. if (!isnan(sensor_DHT_tmpTemp)) {
  15. sendLog(F("DHT: reading Temp OK"), LOGLEVEL_VERBOSE);
  16. _i = sensor_DHT_readRetries;
  17. }
  18. else {
  19. sendLog(F("DHT: reading Temp failed"), LOGLEVEL_DEBUG);
  20. }
  21. }
  22. // try reading humidity
  23. for (int _i = 0; _i < sensor_DHT_readRetries; _i++) {
  24. sensor_DHT_tmpHum = dht.readHumidity();
  25. if (!isnan(sensor_DHT_tmpHum)) {
  26. sendLog(F("DHT: reading Hum OK"), LOGLEVEL_VERBOSE);
  27. _i = sensor_DHT_readRetries;
  28. }
  29. else {
  30. sendLog(F("DHT: reading Hum failed"), LOGLEVEL_DEBUG);
  31. }
  32. }
  33. // Check if any reads failed
  34. if (isnan(sensor_DHT_tmpHum) || isnan(sensor_DHT_tmpTemp)) {
  35. sendLog(F("DHT: Failed to read sensor."), LOGLEVEL_ERROR);
  36. }
  37. else {
  38. sensor_DHT_tmpTemp = sensor_DHT_tmpTemp + confSens.DHT_tempCorrVal;
  39. sensor_DHT_tmpHum = round(sensor_DHT_tmpHum) + confSens.DHT_humCorrVal;
  40. int _tmpHumInt = sensor_DHT_tmpHum;
  41. if (sensor_DHT_tmpTemp <= SENSOR_DHT_MAX_VALUE && sensor_DHT_tmpTemp >= SENSOR_DHT_MIN_VALUE) {
  42. // measurement is in range
  43. sensor_DHT_currTemp_raw = sensor_DHT_tmpTemp;
  44. sensor_DHT_currHum_raw = _tmpHumInt;
  45. if ( sensor_DHT_lastUpdate > 0 && sensor_DHT_tmpTemp <= ( sensor_DHT_currTemp + SENSOR_DHT_MAXDIFFFROMLASTTEMP ) && sensor_DHT_tmpTemp >= ( sensor_DHT_currTemp - SENSOR_DHT_MAXDIFFFROMLASTTEMP ) ) {
  46. // temp has already been measured
  47. // only accept new measurement if it does not differ too much from the last value
  48. // then apply filter:
  49. // Temp = (Temp * (FilterFactor -1) + currentMeasurement) / FilterFactor;
  50. sensor_DHT_currTemp = (sensor_DHT_currTemp * (SENSOR_DHT_FILTER_FACTOR - 1) + sensor_DHT_tmpTemp) / SENSOR_DHT_FILTER_FACTOR; // filter
  51. sensor_DHT_currHum = (sensor_DHT_currHum * (SENSOR_DHT_FILTER_FACTOR - 1) + _tmpHumInt) / SENSOR_DHT_FILTER_FACTOR; // filter
  52. sensor_DHT_lastUpdate = millis();
  53. }
  54. else if ( sensor_DHT_lastUpdate == 0 || (millis() - sensor_DHT_lastUpdate) > sensors_maxMeasurementAge ) {
  55. // this is the first measurement or the last one is older than 5m - always accept measurement
  56. sensor_DHT_currTemp = sensor_DHT_tmpTemp + confSens.DHT_tempCorrVal;
  57. sensor_DHT_currHum = _tmpHumInt + confSens.DHT_humCorrVal;
  58. sensor_DHT_lastUpdate = millis();
  59. }
  60. // skip in all other cases
  61. }
  62. }
  63. }
  64. #endif