-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBME280_Impl.hpp
100 lines (84 loc) · 2.37 KB
/
BME280_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef _BME280_IMPL_
#define _BME280_IMPL_
#include "SensorInterface.hpp"
#include <Wire.h>
#include <Adafruit_BME280.h>
class BME280_Impl : public SensorInterface {
public:
BME280_Impl(uint8_t i2caddr);
virtual ~BME280_Impl();
virtual float temperature();
virtual float humidity();
virtual float pressure();
virtual SensorInterface::SensorState state();
private:
void readSensor();
Adafruit_BME280 bme;
SensorInterface::SensorState ss;
float h; // humidity in percent
float t; // temperature as Celsius
float p; // pressure in hPa
};
inline BME280_Impl::BME280_Impl(uint8_t i2caddr)
: bme()
, ss(SensorInterface::state())
, t(SensorInterface::temperature())
, h(SensorInterface::humidity())
, p(SensorInterface::pressure()) {
if (!bme.begin(i2caddr)) {
Homie.getLogger() << "Couldn't find BME280" << endl;
ss = connect_error;
}
else
{
// weather monitoring
bme.setSampling(Adafruit_BME280::MODE_FORCED,
Adafruit_BME280::SAMPLING_X1, // temperature
Adafruit_BME280::SAMPLING_X1, // pressure
Adafruit_BME280::SAMPLING_X1, // humidity
Adafruit_BME280::FILTER_OFF );
}
}
inline BME280_Impl::~BME280_Impl() {
}
inline void BME280_Impl::readSensor() {
if(ss == connect_error)
return;
static unsigned long last = 0;
unsigned long now = millis();
if((now - last) > 60000UL || (last == 0)) { // suggested: 1 reading per minute
// Only needed in forced mode! In normal mode, you can remove the next line.
bme.takeForcedMeasurement(); // has no effect in normal mode
float _t = bme.readTemperature();
float _h = bme.readHumidity();
float _p = bme.readPressure() / 100.0F;
// Check if any reads failed and keep old values (to try again).
if (isnan(_h) || isnan(_t) || isnan(_p)) {
ss = SensorInterface::read_error;
Homie.getLogger() << "Failed to read BME280 sensor!" << endl;
} else {
ss = SensorInterface::ok;
t = _t;
h = _h;
p = _p;
last = now;
}
}
}
inline float BME280_Impl::temperature() {
readSensor();
return t;
}
inline float BME280_Impl::humidity() {
readSensor();
return h;
}
inline float BME280_Impl::pressure() {
readSensor();
return p;
}
inline SensorInterface::SensorState BME280_Impl::state() {
readSensor();
return ss;
}
#endif