-
Notifications
You must be signed in to change notification settings - Fork 0
/
airq_bme680.h
220 lines (196 loc) · 6.83 KB
/
airq_bme680.h
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// airq_bme680.h
// adapted from:
// https://github.com/adafruit/Adafruit_BME680/blob/master/examples/bme680test/bme680test.ino
#pragma once
#include "scheduler.h"
#include "sensors.h"
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
#include "home_assistant.h"
namespace ustd {
class AirQualityBme680 {
public:
String AIRQUALITY_VERSION = "0.1.0";
Scheduler *pSched;
int tID;
String name;
// uint8_t i2caddr;
double kOhmsVal = 0.0, temperatureVal = 0.0, humidityVal = 0.0, pressureVal = 0.0;
time_t startTime = 0;
bool bStartup = false;
bool bActive = false;
String errmsg = "";
ustd::sensorprocessor kOhmsGas = ustd::sensorprocessor(4, 30, 1.0);
ustd::sensorprocessor temperature = ustd::sensorprocessor(4, 30, 0.1);
ustd::sensorprocessor humidity = ustd::sensorprocessor(4, 30, 0.1);
ustd::sensorprocessor pressure = ustd::sensorprocessor(4, 30, 0.01);
Adafruit_BME680 *pAirQuality;
#ifdef __ESP__
HomeAssistant *pHA;
#endif
AirQualityBme680(String name) : name(name) {
pAirQuality = new Adafruit_BME680(); // seems to be on i2c port 0x77 always
}
~AirQualityBme680() {
}
double getTemperature() {
return temperatureVal;
}
double getHumidity() {
return humidityVal;
}
double getPressure() {
return pressureVal;
}
double getkOhmsGas() {
return kOhmsVal;
}
void begin(Scheduler *_pSched) {
pSched = _pSched;
if (!pAirQuality->begin()) {
errmsg = "Could not find a valid BME680 sensor, check wiring!";
#ifdef USE_SERIAL_DBG
Serial.println(errmsg);
#endif
} else {
// Set up oversampling and filter initialization
pAirQuality->setTemperatureOversampling(BME680_OS_8X);
pAirQuality->setHumidityOversampling(BME680_OS_2X);
pAirQuality->setPressureOversampling(BME680_OS_4X);
pAirQuality->setIIRFilterSize(BME680_FILTER_SIZE_3);
pAirQuality->setGasHeater(320, 150); // 320*C for 150 ms
bActive = true;
}
auto ft = [=]() { this->loop(); };
tID = pSched->add(ft, name, 2000000); // every 2sec
auto fnall = [=](String topic, String msg, String originator) {
this->subsMsg(topic, msg, originator);
};
pSched->subscribe(tID, name + "/sensor/#", fnall);
}
#ifdef __ESP__
void registerHomeAssistant(String homeAssistantFriendlyName, String projectName = "",
String homeAssistantDiscoveryPrefix = "homeassistant") {
pHA = new HomeAssistant(name, tID, homeAssistantFriendlyName, projectName,
AIRQUALITY_VERSION, homeAssistantDiscoveryPrefix);
pHA->addSensor("temperature", "Temperature", "\\u00B0C", "temperature", "mdi:thermometer");
pHA->addSensor("humidity", "Humidity", "%", "humidity", "mdi:water-percent");
pHA->addSensor("pressure", "Pressure", "hPa", "pressure", "mdi:altimeter");
pHA->addSensor("kohmsgas", "k\\u2126-Gas", "k\\u2126", "None", "mdi:air-filter");
pHA->begin(pSched);
}
#endif
void publishTemperature() {
if (bActive && !bStartup) {
char buf[32];
pSched->publish(name + "/sensor/result", "OK");
sprintf(buf, "%5.1f", temperatureVal);
pSched->publish(name + "/sensor/temperature", buf);
}
}
void publishHumidity() {
if (bActive && !bStartup) {
char buf[32];
pSched->publish(name + "/sensor/result", "OK");
sprintf(buf, "%5.1f", humidityVal);
pSched->publish(name + "/sensor/humidity", buf);
}
}
void publishPressure() {
if (bActive && !bStartup) {
char buf[32];
pSched->publish(name + "/sensor/result", "OK");
sprintf(buf, "%5.1f", pressureVal);
pSched->publish(name + "/sensor/pressure", buf);
}
}
void publishkOhmsGas() {
if (bActive && !bStartup) {
char buf[32];
pSched->publish(name + "/sensor/result", "OK");
sprintf(buf, "%5.1f", kOhmsVal);
pSched->publish(name + "/sensor/kohmsgas", buf);
}
}
void loop() {
#ifdef USE_SERIAL_DBG
Serial.println("BME680 enter loop");
#endif
if (startTime < 100000)
startTime = time(NULL); // NTP data available.
if (bActive) {
//#if defined(__ESP__) && !defined(__ESP32__)
// ESP.wdtDisable();
//#endif
if (pAirQuality->performReading()) {
//#if defined(__ESP__) && !defined(__ESP32__)
// ESP.wdtEnable(WDTO_8S);
//#endif
double t, h, p, k;
#ifdef USE_SERIAL_DBG
Serial.println("AirQualityBme680 sensor data available");
#endif
t = pAirQuality->temperature;
h = pAirQuality->humidity;
p = pAirQuality->pressure / 100.0;
k = pAirQuality->gas_resistance / 1000.0;
if (temperature.filter(&t)) {
temperatureVal = t;
publishTemperature();
}
if (humidity.filter(&h)) {
humidityVal = h;
publishHumidity();
}
if (pressure.filter(&p)) {
pressureVal = p;
publishPressure();
}
if (kOhmsGas.filter(&k)) {
kOhmsVal = k;
publishkOhmsGas();
}
#ifdef USE_SERIAL_DBG
Serial.println(t);
Serial.println(h);
Serial.println(p);
Serial.println(k);
#endif
} else {
//#if defined(__ESP__) && !defined(__ESP32__)
// ESP.wdtEnable(WDTO_8S);
//#endif
#ifdef USE_SERIAL_DBG
Serial.println("AirQualityBme680 sensor no data available");
#endif
}
} else {
#ifdef USE_SERIAL_DBG
Serial.println("AirQualityBme680 sensor not active.");
#endif
if (errmsg != "") {
pSched->publish(name + "/sensor/result", errmsg);
errmsg = "";
}
}
#ifdef USE_SERIAL_DBG
Serial.println("BME680 exit loop.");
#endif
}
void subsMsg(String topic, String msg, String originator) {
if (topic == name + "/sensor/temperature/get") {
publishTemperature();
}
if (topic == name + "/sensor/humidity/get") {
publishHumidity();
}
if (topic == name + "/sensor/pressure/get") {
publishPressure();
}
if (topic == name + "/sensor/kohmsgas/get") {
publishkOhmsGas();
}
};
}; // AirQuality
} // namespace ustd