forked from bertrik/tvocmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtvocmeter.ino
236 lines (194 loc) · 6.4 KB
/
tvocmeter.ino
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <Arduino.h>
#include <ArduinoOTA.h>
#include "OTA_PASSWORD.h"
#include <EEPROM.h>
#include <Wire.h>
#include <SparkFunCCS811.h>
#include <SparkFunBME280.h>
#include <WiFiManager.h>
#include <PubSubClient.h>
#define PIN_CCS811_WAK D5
#define PIN_CCS811_SDA D6
#define PIN_CCS811_SCL D7
#define CCS811_ADDR 0x5A
#define MQTT_HOST "mosquitto.space.revspace.nl"
#define MQTT_PORT 1883
#define MQTT_TOPIC "revspace/sensors/tvoc/%s/%s" // esp_id/subtopic
#define BASELINE_PERIOD_SEC 3600
#define ENV_PERIOD_SEC 10
#define LOG_PERIOD_SEC 10
#define NVDATA_MAGIC 0x1337
struct {
uint16_t baseline;
uint32_t magic;
} nvdata;
typedef struct {
uint32_t total;
uint16_t count;
} averager_t;
static CCS811 ccs811(CCS811_ADDR);
static BME280 bme280;
static char esp_id[16];
static WiFiManager wifiManager;
static WiFiClient wifiClient;
static PubSubClient mqttClient(wifiClient);
static char statustopic[128];
// printf-like output to serial port
static void print(const char *fmt, ...)
{
// format it
char buf[256];
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
// send it to serial
Serial.write(buf);
}
void setup(void)
{
delay(3000);
// greeting
Serial.begin(115200);
print("\nTVOC meter\n");
ArduinoOTA.setHostname("esp-tvoc");
ArduinoOTA.setPassword(OTA_PASSWORD);
ArduinoOTA.begin();
// setup pins
pinMode(PIN_CCS811_WAK, OUTPUT);
digitalWrite(PIN_CCS811_WAK, 1);
// get ESP id
sprintf(esp_id, "%06x", ESP.getChipId());
print("ESP ID: %s\n", esp_id);
// setup I2C
Wire.begin(PIN_CCS811_SDA, PIN_CCS811_SCL);
// setup BME280
bme280.setI2CAddress(0x76);
if (!bme280.beginI2C()) {
while (1) {
print("bme280.begin() returned with an error.\n");
delay(1000);
}
}
// setup CCS811
digitalWrite(PIN_CCS811_WAK, 0);
CCS811Core::status returnCode = ccs811.begin();
if (returnCode != CCS811Core::SENSOR_SUCCESS) {
while (1) {
print("ccs811.begin() returned with an error.\n");
delay(1000);
}
}
// restore CCS811 baseline
EEPROM.begin(sizeof(nvdata));
EEPROM.get(0, nvdata);
if (nvdata.magic == NVDATA_MAGIC) {
print("Restoring base line value %04X\n", nvdata.baseline);
ccs811.setBaseline(nvdata.baseline);
}
// connect to wifi
print("Starting WIFI manager ...\n");
wifiManager.setConfigPortalTimeout(120);
wifiManager.autoConnect("ESP-TVOC");
snprintf(statustopic, sizeof(statustopic), MQTT_TOPIC, esp_id, "status");
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
}
static bool mqtt_send(const char *topic, const char *value, bool retained)
{
bool result = false;
if (!mqttClient.connected()) {
result = mqttClient.connect(esp_id, statustopic, 0, retained, "offline");
if (result) {
result = mqttClient.publish(statustopic, "online", retained);
}
}
if (mqttClient.connected()) {
print("Publishing %s to %s ...", value, topic);
result = mqttClient.publish(topic, value, retained);
print(result ? "OK" : "FAIL");
print("\n");
}
return result;
}
void loop(void)
{
static unsigned long second_log = 0;
static unsigned long second_baseline = 0;
static unsigned long second_env = 0;
static averager_t tvoc_avg = { 0, 0 };
static averager_t eco2_avg = { 0, 0 };
char topic[128];
char message[16];
// read CCS811 if available
if (ccs811.dataAvailable()) {
ccs811.readAlgorithmResults();
uint16_t tvoc = ccs811.getTVOC();
tvoc_avg.total += tvoc;
tvoc_avg.count++;
uint16_t eco2 = ccs811.getCO2();
eco2_avg.total += eco2;
eco2_avg.count++;
}
// save CCS811 baseline every BASELINE_PERIOD_SEC
unsigned long second = millis() / 1000;
if ((second - second_baseline) > BASELINE_PERIOD_SEC) {
second_baseline = second;
nvdata.baseline = ccs811.getBaseline();
nvdata.magic = NVDATA_MAGIC;
print("Saving baseline value %04X\n", nvdata.baseline);
EEPROM.put(0, nvdata);
EEPROM.commit();
}
// update enivironment data every ENV_PERIOD_SEC
if ((second - second_env) >= ENV_PERIOD_SEC) {
second_env = second;
// disable CCS811 WAKE and read environment data
digitalWrite(PIN_CCS811_WAK, 1);
float tempC = bme280.readTempC();
float humidity = bme280.readFloatHumidity();
// enable CCS811 WAKE and write environment data
digitalWrite(PIN_CCS811_WAK, 0);
print("Applying T/RH compensation: T=%.2f, RH=%.2f\n", tempC, humidity);
ccs811.setEnvironmentalData(humidity, tempC);
// log to MQTT
snprintf(topic, sizeof(topic), MQTT_TOPIC, esp_id, "bme280/temperature");
snprintf(message, sizeof(message), "%.2f °C", tempC);
mqtt_send(topic, message, true);
snprintf(topic, sizeof(topic), MQTT_TOPIC, esp_id, "bme280/humidity");
snprintf(message, sizeof(message), "%.2f %%", humidity);
mqtt_send(topic, message, true);
}
// log TVOC and eCO2 over MQTT every LOG_PERIOD_SEC
if ((second - second_log) >= LOG_PERIOD_SEC) {
second_log = second;
// calculate average TVOC
if (tvoc_avg.count > 0) {
uint16_t tvoc = (tvoc_avg.total + (tvoc_avg.count / 2)) / tvoc_avg.count;
tvoc_avg.total = 0;
tvoc_avg.count = 0;
// send over MQTT
snprintf(topic, sizeof(topic), MQTT_TOPIC, esp_id, "ccs811/tvoc");
snprintf(message, sizeof(message), "%u ppb", tvoc);
mqtt_send(topic, message, true);
}
// calculate average eCO2
if (eco2_avg.count > 0) {
uint16_t eco2 = (eco2_avg.total + (eco2_avg.count / 2)) / eco2_avg.count;
eco2_avg.total = 0;
eco2_avg.count = 0;
// send over MQTT
snprintf(topic, sizeof(topic), MQTT_TOPIC, esp_id, "ccs811/eco2");
snprintf(message, sizeof(message), "%u ppm", eco2);
mqtt_send(topic, message, true);
}
}
// keep MQTT alive
mqttClient.loop();
// handle OTA
ArduinoOTA.handle();
// verify network connection and reboot on failure
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Restarting ESP...");
ESP.restart();
}
}