-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSHT3x_Impl.hpp
75 lines (63 loc) · 1.61 KB
/
SHT3x_Impl.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef _SHT3x_Impl_
#define _SHT3x_Impl_
#include "SensorInterface.hpp"
#include "Adafruit_SHT31.h"
class SHT3x_Impl : public SensorInterface {
public:
SHT3x_Impl(uint8_t i2caddr);
virtual ~SHT3x_Impl();
virtual float temperature();
virtual float humidity();
virtual SensorInterface::SensorState state();
private:
void readSensor();
Adafruit_SHT31 sht3x;
SensorInterface::SensorState ss;
float h; // humidity in percent
float t; // temperature as Celsius
};
inline SHT3x_Impl::SHT3x_Impl(uint8_t i2caddr)
: sht3x()
, ss(SensorInterface::state())
, t(SensorInterface::temperature())
, h(SensorInterface::humidity()) {
if (! sht3x.begin(i2caddr)) {
Homie.getLogger() << "Couldn't find SHT" << endl;
ss = connect_error;
}
}
inline SHT3x_Impl::~SHT3x_Impl() {
}
inline void SHT3x_Impl::readSensor() {
if(ss == connect_error)
return;
static unsigned long last = 0;
unsigned long now = millis();
if((now - last) > 10000UL || (last == 0)) {
float _t = sht3x.readTemperature();
float _h = sht3x.readHumidity();
// Check if any reads failed and keep old values (to try again).
if (isnan(_h) || isnan(_t)) {
ss = SensorInterface::read_error;
Homie.getLogger() << "Failed to read SHT sensor!" << endl;
} else {
ss = SensorInterface::ok;
t = _t;
h = _h;
last = now;
}
}
}
inline float SHT3x_Impl::temperature() {
readSensor();
return t;
}
inline float SHT3x_Impl::humidity() {
readSensor();
return h;
}
inline SensorInterface::SensorState SHT3x_Impl::state() {
readSensor();
return ss;
}
#endif