supla-device
Loading...
Searching...
No Matches
DHT.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#ifndef SRC_SUPLA_SENSOR_DHT_H_
20#define SRC_SUPLA_SENSOR_DHT_H_
21
22#include <DHT.h>
23
24#include "therm_hygro_meter.h"
25
26namespace Supla {
27namespace Sensor {
28class DHT : public ThermHygroMeter {
29 public:
30 DHT(int pin, int dhtType) : dht(pin, dhtType) {
31 dht.begin();
32 delay(100);
33 retryCountTemp = 0;
34 retryCountHumi = 0;
35 lastValidTemp = TEMPERATURE_NOT_AVAILABLE;
36 lastValidHumi = HUMIDITY_NOT_AVAILABLE;
37 }
38
39 double getTemp() {
40 double value = TEMPERATURE_NOT_AVAILABLE;
41 value = dht.readTemperature();
42 if (isnan(value)) {
43 value = TEMPERATURE_NOT_AVAILABLE;
44 }
45
46 if (value == TEMPERATURE_NOT_AVAILABLE) {
47 retryCountTemp++;
48 if (retryCountTemp > 3) {
49 retryCountTemp = 0;
50 } else {
51 value = lastValidTemp;
52 }
53 } else {
54 retryCountTemp = 0;
55 }
56 lastValidTemp = value;
57
58 return value;
59 }
60
61 double getHumi() {
62 double value = HUMIDITY_NOT_AVAILABLE;
63 value = dht.readHumidity();
64 if (isnan(value)) {
65 value = HUMIDITY_NOT_AVAILABLE;
66 }
67
68 if (value == HUMIDITY_NOT_AVAILABLE) {
69 retryCountHumi++;
70 if (retryCountHumi > 3) {
71 retryCountHumi = 0;
72 } else {
73 value = lastValidHumi;
74 }
75 } else {
76 retryCountHumi = 0;
77 }
78 lastValidHumi = value;
79
80 return value;
81 }
82
83 protected:
84 ::DHT dht;
85 double lastValidTemp;
86 double lastValidHumi;
87 int8_t retryCountTemp;
88 int8_t retryCountHumi;
89};
90
91}; // namespace Sensor
92}; // namespace Supla
93
94#endif // SRC_SUPLA_SENSOR_DHT_H_