forked from airgradienthq/arduino
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathC02_PM1_PM2_PM10_SHT_OLED_WIFI.ino
338 lines (299 loc) · 9.51 KB
/
C02_PM1_PM2_PM10_SHT_OLED_WIFI.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
327
328
329
330
331
332
333
334
335
336
337
338
/**
* This sketch connects an AirGradient DIY sensor to a WiFi network, and runs a
* tiny HTTP server to serve air quality metrics to Prometheus.
*/
/*
PM1 and PM10 reporting for Plantower PMS5003 PM2.5 sensor enabled.
Workaround for glitchy CO2 and PM sensors reporting included.
For using this .ino you have to install improved AirGradient libraries, which supports PM1 and PM10 reporting:
https://github.com/d3vilh/airgradient-improved
*/
#include <AirGradient.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include <Wire.h>
#include "SSD1306Wire.h"
AirGradient ag = AirGradient();
// Config ----------------------------------------------------------------------
// Optional.
const char* deviceId = "livingroom";
// set to 'F' to switch display from Celcius to Fahrenheit
char temp_display = 'C';
// Hardware options for AirGradient DIY sensor.
const bool hasPM1 = true;
const bool hasPM2 = true;
const bool hasPM10 = true;
const bool hasCO2 = true;
const bool hasSHT = true;
// WiFi and IP connection info.
const char* ssid = "CY-Capsule";
const char* password = "GagaZush";
const int port = 9926;
// Uncomment the line below to configure a static IP address.
// #define staticip
#ifdef staticip
IPAddress static_ip(192, 168, 88, 6);
IPAddress gateway(192, 168, 88, 1);
IPAddress subnet(255, 255, 255, 0);
#endif
// The frequency of measurement updates.
const int updateFrequency = 5000;
// For housekeeping.
long lastUpdate;
int counter = 0;
int stat_prev_pm1 = 0;
int stat_prev_pm2 = 0;
int stat_prev_pm10 = 0;
int stat_prev_co = 0;
// Config End ------------------------------------------------------------------
SSD1306Wire display(0x3c, SDA, SCL);
ESP8266WebServer server(port);
void setup() {
Serial.begin(9600);
// Init Display.
display.init();
display.flipScreenVertically();
showTextRectangle("Init", String(ESP.getChipId(),HEX),true);
// Enable enabled sensors.
if (hasPM1) ag.PMS_Init();
if (hasCO2) ag.CO2_Init();
if (hasSHT) ag.TMP_RH_Init(0x44);
// Set static IP address if configured.
#ifdef staticip
WiFi.config(static_ip,gateway,subnet);
#endif
// Set WiFi mode to client (without this it may try to act as an AP).
WiFi.mode(WIFI_STA);
// Configure Hostname
if ((deviceId != NULL) && (deviceId[0] == '\0')) {
Serial.printf("No Device ID is Defined, Defaulting to board defaults");
}
else {
wifi_station_set_hostname(deviceId);
WiFi.setHostname(deviceId);
}
// Setup and wait for WiFi.
WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
showTextRectangle("Trying to", "connect...", true);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("MAC address: ");
Serial.println(WiFi.macAddress());
Serial.print("Hostname: ");
Serial.println(WiFi.hostname());
server.on("/", HandleRoot);
server.on("/metrics", HandleRoot);
server.onNotFound(HandleNotFound);
server.begin();
Serial.println("HTTP server started at ip " + WiFi.localIP().toString() + ":" + String(port));
showTextRectangle("Listening To", WiFi.localIP().toString() + ":" + String(port),true);
}
void loop() {
long t = millis();
server.handleClient();
updateScreen(t);
}
String GenerateMetrics() {
String message = "";
String idString = "{id=\"" + String(deviceId) + "\",mac=\"" + WiFi.macAddress().c_str() + "\"}";
if (hasPM1) {
int statf_pm1 = 0;
int stat = ag.getPM1_Raw();
if (stat > 0 && stat <= 10000) {
statf_pm1 = stat;
stat_prev_pm1 = statf_pm1; // saving not glitchy value
} else {
statf_pm1 = stat_prev_pm1; // using previous not glitchy value if curent value is glitchy
}
message += "# HELP pm01 Particulate Matter PM1 value\n";
message += "# TYPE pm01 gauge\n";
message += "pm01";
message += idString;
message += String(statf_pm1);
message += "\n";
}
if (hasPM2) {
int statf_pm2 = 0;
int stat = ag.getPM2_Raw();
if (stat > 0 && stat <= 10000) {
statf_pm2 = stat;
stat_prev_pm2 = statf_pm2; // saving not glitchy value
} else {
statf_pm2 = stat_prev_pm2; // using previous not glitchy value if curent value is glitchy
}
message += "# HELP pm02 Particulate Matter PM2.5 value\n";
message += "# TYPE pm02 gauge\n";
message += "pm02";
message += idString;
message += String(statf_pm2);
message += "\n";
}
if (hasPM10) {
int statf_pm10 = 0;
int stat = ag.getPM10_Raw();
if (stat > 0 && stat <= 10000) {
statf_pm10 = stat;
stat_prev_pm10 = statf_pm10; // saving not glitchy value
} else {
statf_pm10 = stat_prev_pm10; // using previous not glitchy value if curent value is glitchy
}
message += "# HELP pm10 Particulate Matter PM10 value\n";
message += "# TYPE pm10 gauge\n";
message += "pm10";
message += idString;
message += String(statf_pm10);
message += "\n";
}
if (hasCO2) {
int statf_co = 0;
int stat = ag.getCO2_Raw();
if (stat >= 0 && stat <= 10000) {
statf_co = stat;
stat_prev_co = statf_co; // saving not glitchy value
} else {
statf_co = stat_prev_co; // using previous not glitchy value if curent value is glitchy
}
message += "# HELP rco2 CO2 value, in ppm\n";
message += "# TYPE rco2 gauge\n";
message += "rco2";
message += idString;
message += String(statf_co);
message += "\n";
}
if (hasSHT) {
TMP_RH stat = ag.periodicFetchData();
message += "# HELP atmp Temperature, in degrees Celsius\n";
message += "# TYPE atmp gauge\n";
message += "atmp";
message += idString;
// Dirty Temp adjust (-4 degrees)
message += String(stat.t - 4);
message += "\n";
message += "# HELP rhum Relative humidity, in percent\n";
message += "# TYPE rhum gauge\n";
message += "rhum";
message += idString;
message += String(stat.rh);
message += "\n";
}
return message;
delay(5000);
}
void HandleRoot() {
server.send(200, "text/plain", GenerateMetrics() );
}
void HandleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/html", message);
}
// DISPLAY
void showTextRectangle(String ln1, String ln2, boolean small) {
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
if (small) {
display.setFont(ArialMT_Plain_16);
} else {
display.setFont(ArialMT_Plain_24);
}
display.drawString(32, 16, ln1);
display.drawString(32, 36, ln2);
display.display();
}
void updateScreen(long now) {
if ((now - lastUpdate) > updateFrequency) {
// Take a measurement at a fixed interval.
switch (counter) {
case 0:
if (hasPM1) {
int statf_pm1 = 0;
int stat = ag.getPM1_Raw();
if (stat > 0 && stat <= 10000) {
statf_pm1 = stat;
stat_prev_pm1 = statf_pm1; // saving not glitchy value
} else {
statf_pm1 = stat_prev_pm1; // using previous not glitchy value if curent value is glitchy
}
showTextRectangle("PM1",String(statf_pm1),false);
}
break;
case 1:
if (hasPM2) {
int statf_pm2 = 0;
int stat = ag.getPM2_Raw();
if (stat > 0 && stat <= 10000) {
statf_pm2 = stat;
stat_prev_pm2 = statf_pm2; // saving not glitchy value
} else {
statf_pm2 = stat_prev_pm2; // using previous not glitchy value if curent value is glitchy
}
showTextRectangle("PM2.5",String(statf_pm2),false);
}
break;
case 2:
if (hasPM10) {
int statf_pm10 = 0;
int stat = ag.getPM10_Raw();
if (stat > 0 && stat <= 10000) {
statf_pm10 = stat;
stat_prev_pm10 = statf_pm10; // saving not glitchy value
} else {
statf_pm10 = stat_prev_pm10; // using previous not glitchy value if curent value is glitchy
}
showTextRectangle("PM10",String(statf_pm10),false);
}
break;
case 3:
if (hasCO2) {
int statf_co = 0;
int stat = ag.getCO2_Raw();
if (stat >= 0 && stat <= 10000) {
statf_co = stat;
stat_prev_co = statf_co; // saving not glitchy value
} else {
statf_co = stat_prev_co; // using previous not glitchy value if curent value is glitchy
}
showTextRectangle("CO2", String(statf_co), false);
}
break;
case 4:
if (hasSHT) {
TMP_RH stat = ag.periodicFetchData();
if (temp_display == 'F' || temp_display == 'f') {
showTextRectangle("TMP", String((stat.t * 9 / 5) + 32, 1) + "F", false);
} else {
showTextRectangle("TMP", String(stat.t - 4, 1) + "C", false); // dirty temp adjust
}
}
break;
case 5:
if (hasSHT) {
TMP_RH stat = ag.periodicFetchData();
showTextRectangle("HUM", String(stat.rh) + "%", false);
}
break;
}
counter++;
if (counter > 5) counter = 0;
lastUpdate = millis();
delay(5000);
}
}