-
Notifications
You must be signed in to change notification settings - Fork 11
/
esp8266-co2monitor.ino
326 lines (257 loc) · 7.24 KB
/
esp8266-co2monitor.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include "dep/pubsubclient-2.7/src/PubSubClient.cpp"
#include "dep/WiFiManager-0.15.0/WiFiManager.cpp"
#include "settings.h"
#define IDX_CMD 0
#define IDX_MSB 1
#define IDX_LSB 2
#define IDX_CHECKSUM 3
#define IDX_END 4
#define CMD_TEMPERATURE 0x42
#define CMD_CO2_MEASUREMENT 0x50
#ifdef USE_HA_AUTODISCOVERY
#define FIRMWARE_PREFIX "esp8266-co2monitor"
char MQTT_TOPIC_LAST_WILL[128];
char MQTT_TOPIC_CO2_MEASUREMENT[128];
char MQTT_TOPIC_TEMPERATURE_MEASUREMENT[128];
#endif
WiFiClient wifiClient;
PubSubClient mqttClient;
uint8_t bitIndex = 0;
uint8_t byteIndex = 0;
uint8_t clkValue = LOW;
uint8_t lastClkValue = LOW;
uint8_t tmp = 0;
unsigned long currentMillis = 0;
unsigned long lastMillis = 0;
unsigned long lastUpdateMs = 0;
uint8_t mqttRetryCounter = 0;
uint16_t co2Measurement = 0;
float smoothCo2Measurement = 0.0;
float temperature = 0;
byte bits[8];
byte bytes[5] = {0};
char sprintfHelper[16] = {0};
char hostname[16];
void setup() {
Serial.begin(115200);
Serial.println("\n");
Serial.println("Hello from esp8266-co2monitor");
// Power up wait
delay(2000);
WiFiManager wifiManager;
int32_t chipid = ESP.getChipId();
Serial.print("MQTT_MAX_PACKET_SIZE: ");
Serial.println(MQTT_MAX_PACKET_SIZE);
#ifdef HOSTNAME
hostname = HOSTNAME;
#else
snprintf(hostname, 24, "CO2MONITOR-%X", chipid);
#endif
#ifdef USE_HA_AUTODISCOVERY
snprintf(MQTT_TOPIC_LAST_WILL, 127, "%s/%s/presence", FIRMWARE_PREFIX, hostname);
snprintf(MQTT_TOPIC_CO2_MEASUREMENT, 127, "%s/%s/%s_%s/state", FIRMWARE_PREFIX, hostname, hostname, "co2");
snprintf(MQTT_TOPIC_TEMPERATURE_MEASUREMENT, 127, "%s/%s/%s_%s/state", FIRMWARE_PREFIX, hostname, hostname, "temp");
#endif
#ifdef CONF_WIFI_PASSWORD
wifiManager.autoConnect(hostname, CONF_WIFI_PASSWORD);
#else
wifiManager.autoConnect(hostname);
#endif
WiFi.hostname(hostname);
mqttClient.setClient(wifiClient);
mqttClient.setServer(MQTT_HOST, 1883);
ArduinoOTA.setHostname(hostname);
ArduinoOTA.setPassword(OTA_PASSWORD);
ArduinoOTA.begin();
pinMode(PIN_CLK, INPUT);
pinMode(PIN_DATA, INPUT);
attachInterrupt(PIN_CLK, onClock, RISING);
Serial.print("Hostname: ");
Serial.println(hostname);
Serial.println("-- Current GPIO Configuration --");
Serial.print("PIN_CLK: ");
Serial.println(PIN_CLK);
Serial.print("PIN_DATA: ");
Serial.println(PIN_DATA);
mqttConnect();
}
ICACHE_RAM_ATTR void onClock() {
lastMillis = millis();
bits[bitIndex++] = (digitalRead(PIN_DATA) == HIGH) ? 1 : 0;
// Transform bits to byte
if (bitIndex >= 8) {
tmp = 0;
for (uint8_t i = 0; i < 8; i++) {
tmp |= (bits[i] << (7 - i));
}
bytes[byteIndex++] = tmp;
bitIndex = 0;
}
if (byteIndex >= 5) {
byteIndex = 0;
decodeDataPackage(bytes);
}
}
void mqttConnect() {
while (!mqttClient.connected()) {
bool mqttConnected = false;
if (MQTT_USERNAME && MQTT_PASSWORD) {
mqttConnected = mqttClient.connect(hostname, MQTT_USERNAME, MQTT_PASSWORD, MQTT_TOPIC_LAST_WILL, 1, true, MQTT_LAST_WILL_PAYLOAD_DISCONNECTED);
} else {
mqttConnected = mqttClient.connect(hostname, MQTT_TOPIC_LAST_WILL, 1, true, MQTT_LAST_WILL_PAYLOAD_DISCONNECTED);
}
if (mqttConnected) {
Serial.println("Connected to MQTT Broker");
mqttClient.publish(MQTT_TOPIC_LAST_WILL, MQTT_LAST_WILL_PAYLOAD_CONNECTED, true);
mqttRetryCounter = 0;
#ifdef USE_HA_AUTODISCOVERY
setupHAAutodiscovery();
#endif
} else {
Serial.println("Failed to connect to MQTT Broker");
if (mqttRetryCounter++ > MQTT_MAX_CONNECT_RETRY) {
Serial.println("Restarting uC");
ESP.restart();
}
delay(2000);
}
}
}
#ifdef USE_HA_AUTODISCOVERY
#define AUTOCONFIG_PAYLOAD_TPL_TEMP "{\
\"stat_t\":\"%s/%s/%s_temp/state\",\
\"unique_id\":\"%s_temp\",\
\"name\":\"%s Temp\",\
\"unit_of_meas\":\"°C\",\
\"dev_cla\":\"temperature\",\
\"dev\": {\
\"identifiers\":\"%s\",\
\"name\":\"%s\",\
\"manufacturer\":\"RADIANT INNOVATION INC\",\
\"model\":\"ZyAura ZGm05\"\
}\
}"
#define AUTOCONFIG_PAYLOAD_TPL_CO2 "{\
\"stat_t\":\"%s/%s/%s_co2/state\",\
\"unique_id\":\"%s_co2\",\
\"name\":\"%s CO2\",\
\"unit_of_meas\":\"ppm\",\
\"dev\": {\
\"identifiers\":\"%s\",\
\"name\":\"%s\",\
\"manufacturer\":\"RADIANT INNOVATION INC\",\
\"model\":\"ZyAura ZGm05\"\
}\
}"
void setupHAAutodiscovery() {
char autoconfig_topic_co2[128];
char autoconfig_payload_co2[1024];
char autoconfig_topic_temp[128];
char autoconfig_payload_temp[1024];
snprintf(
autoconfig_topic_co2,
127,
"%s/sensor/%s/%s_co2/config",
HA_DISCOVERY_PREFIX,
hostname,
hostname
);
snprintf(
autoconfig_topic_temp,
127,
"%s/sensor/%s/%s_temp/config",
HA_DISCOVERY_PREFIX,
hostname,
hostname
);
snprintf(
autoconfig_payload_co2,
1023,
AUTOCONFIG_PAYLOAD_TPL_CO2,
FIRMWARE_PREFIX,
hostname,
hostname,
hostname,
hostname,
hostname,
hostname
);
snprintf(
autoconfig_payload_temp,
1023,
AUTOCONFIG_PAYLOAD_TPL_TEMP,
FIRMWARE_PREFIX,
hostname,
hostname,
hostname,
hostname,
hostname,
hostname
);
Serial.println(autoconfig_topic_co2);
Serial.println(autoconfig_payload_co2);
if(
mqttClient.publish(autoconfig_topic_co2, autoconfig_payload_co2, true) &&
mqttClient.publish(autoconfig_topic_temp, autoconfig_payload_temp, true)
){
Serial.println("Autoconf publish successful");
} else {
Serial.println("Autoconf publish failed. Is MQTT_MAX_PACKET_SIZE large enough?");
}
}
#endif
void loop() {
currentMillis = millis();
// Over 50ms no bits? Reset!
if (currentMillis - lastMillis > 50) {
bitIndex = 0;
byteIndex = 0;
}
long updateInterval = PUBLISH_INTERVAL_SLOW_MS;
// If the change is above a specific threshold, we update faster!
float percentChange = abs(((float) co2Measurement / smoothCo2Measurement) - 1.0);
if (percentChange > 0.05) {
updateInterval = PUBLISH_INTERVAL_FAST_MS;
}
if (currentMillis - lastUpdateMs > updateInterval) {
lastUpdateMs = millis();
if (co2Measurement > 0) {
sprintf(sprintfHelper, "%d", co2Measurement);
mqttClient.publish(MQTT_TOPIC_CO2_MEASUREMENT, sprintfHelper, true);
}
if (temperature > 0) {
dtostrf(temperature, 4, 2, sprintfHelper);
mqttClient.publish(MQTT_TOPIC_TEMPERATURE_MEASUREMENT, sprintfHelper, true);
}
}
mqttConnect();
mqttClient.loop();
ArduinoOTA.handle();
}
bool decodeDataPackage(byte data[5]) {
if (data[IDX_END] != 0x0D) {
return false;
}
uint8_t checksum = data[IDX_CMD] + data[IDX_MSB] + data[IDX_LSB];
if (data[IDX_CHECKSUM] != checksum) {
return false;
}
switch (data[IDX_CMD]) {
case CMD_CO2_MEASUREMENT:
co2Measurement = (data[IDX_MSB] << 8) | data[IDX_LSB];
// Exponential smoothing
smoothCo2Measurement = EXP_SMOOTH_ALPHA * (float) co2Measurement + (1.0 - EXP_SMOOTH_ALPHA) * smoothCo2Measurement;
Serial.print("CO2: ");
Serial.println(co2Measurement);
break;
case CMD_TEMPERATURE:
temperature = ((data[IDX_MSB] << 8) | data[IDX_LSB]) / 16.0 - 273.15;
Serial.print("Temp: ");
Serial.println(temperature);
break;
}
}