-
Notifications
You must be signed in to change notification settings - Fork 1
/
AQMS.ino
342 lines (281 loc) · 8.24 KB
/
AQMS.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
339
340
341
342
/**
* Air Quality Monitoring Station
* Copyright (c) 2018 fu-hsi
* MIT License
*/
// ESP
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#include <ESP8266HTTPClient.h>
// File system (Log)
#include <FS.h>
// Sensors
#include <PMS.h>
#include <Adafruit_SHT31.h>
//#include <Adafruit_BMP280.h>
#include <Adafruit_BME280.h>
// Don't touch this ;-)
// #define FU_PROFILE
#ifdef FU_PROFILE
#include "Settings.h"
#else
// Debug (D4)
#define DBG Serial1
// Webservice
static const char* WS_UA = "AQMS/2.0";
static const char* WS_TOKEN = "YOUR TOKEN";
static const char* WS_URL = "YOUR SERVER SCRIPT";
// Wireless credentials
static const char* WL_SSID = "YOUR SSID";
static const char* WL_PASS = "YOUR PASSWORD";
static const uint8_t WL_CHANNEL = 0;
// OTA credentials
static const char* BE_USERNAME = "admin";
static const char* BE_PASSWORD = "admin";
// PMS configuration (INTERVAL & DELAY can't be equal!!!)
static const uint32_t PMS_READ_INTERVAL = 2.5 * 60 * 1000;
static const uint32_t PMS_READ_DELAY = 30000;
// Takes N samples and counts the average
static const uint8_t PMS_READ_SAMPLES = 2;
#endif // FU_PROFILE
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;
PMS pms(Serial);
Adafruit_SHT31 sht;
//Adafruit_BMP280 bm;
Adafruit_BME280 bm;
uint32_t timerInterval = PMS_READ_DELAY;
// ESP8266 CheckFlashConfig by Markus Sattler
void flashInfo() {
uint32_t realSize = ESP.getFlashChipRealSize();
uint32_t ideSize = ESP.getFlashChipSize();
FlashMode_t ideMode = ESP.getFlashChipMode();
DBG.printf("Flash real id: %08X\n", ESP.getFlashChipId());
DBG.printf("Flash real size: %u bytes\n\n", realSize);
DBG.printf("Flash ide size: %u bytes\n", ideSize);
DBG.printf("Flash ide speed: %u Hz\n", ESP.getFlashChipSpeed());
DBG.printf("Flash ide mode: %s\n", (ideMode == FM_QIO ? "QIO" : ideMode == FM_QOUT ? "QOUT" : ideMode == FM_DIO ? "DIO" : ideMode == FM_DOUT ? "DOUT" : "UNKNOWN"));
if (ideSize != realSize) {
DBG.println("Flash Chip configuration wrong!\n");
} else {
DBG.println("Flash Chip configuration ok.\n");
}
}
void setup(void) {
DBG.begin(9600);
Wire.begin(5, 4); // D1, D2
Serial.begin(PMS::BAUD_RATE);
DBG.println();
DBG.println(F("Booting Sketch..."));
// DBG.setDebugOutput(true);
// flashInfo();
if (!sht.begin(0x44)) { // 0x45 for the alternative address
DBG.println(F("SHT31 error."));
}
if (!bm.begin(0x76)) {
DBG.println(F("BME/P280 error."));
}
if (!SPIFFS.begin()) {
DBG.println(F("Failed to mount file system."));
}
pms.wakeUp();
pms.passiveMode();
DBG.print(F("Connecting"));
WiFi.mode(WIFI_STA);
WiFi.begin(WL_SSID, WL_PASS, WL_CHANNEL);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
DBG.print(".");
}
DBG.println();
DBG.print(F("Connected, IP address: "));
DBG.println(WiFi.localIP());
// WiFi.printDiag(DBG);
httpServer.on("/", []() {
httpServer.send(200, "text/html; charset=utf-8", F("<!DOCTYPE html>\
<html>\
<head>\
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\
<meta charset=\"utf-8\">\
<title>Air Quality Monitoring Station</title>\
</head>\
<body>\
<h1>Air Quality Monitoring Station</h1>\
<ul>\
<li><a href=\"/update\">Firmware update</a></li>\
<li><a href=\"/status\">Status</a></li>\
<li><a href=\"/log\">Log</a></li>\
<li><a href=\"/clear\">Clear Log</a></li>\
<li><a href=\"/stop\">Server stop</a></li>\
<li><a href=\"/restart\">Restart</a></li>\
</ul>\
</body>\
</html>"));
});
httpServer.on("/status", []() {
httpServer.send(200, "text/html", "Connected to: " + WiFi.SSID());
});
httpServer.on("/log", []() {
if (File fp = SPIFFS.open("/data.txt", "r"))
{
httpServer.streamFile(fp, "text/plain");
fp.close();
}
else
{
httpServer.send(200, "text/html", "Failed to open file for reading.");
}
});
httpServer.on("/clear", []() {
SPIFFS.remove("/data.txt");
httpServer.send(200, "text/html", "Log cleared.");
});
httpServer.on("/stop", []() {
httpServer.send(200, "text/html", "Server stopped.");
httpServer.stop();
});
httpServer.on("/restart", []() {
httpServer.sendHeader("Refresh", "15; url=/");
httpServer.send(302, "text/html", "Rebooting...");
delay(1000);
ESP.restart();
});
httpUpdater.setup(&httpServer, BE_USERNAME, BE_PASSWORD);
httpServer.begin();
DBG.printf("HTTPUpdateServer ready! Open http://%s in your browser\n", WiFi.localIP().toString().c_str());
}
void loop(void) {
static uint32_t timerLast = 0;
httpServer.handleClient();
uint32_t timerNow = millis();
if (timerNow - timerLast >= timerInterval) {
timerLast = timerNow;
timerCallback();
timerInterval = timerInterval == PMS_READ_DELAY ? PMS_READ_INTERVAL : PMS_READ_DELAY;
}
}
void timerCallback() {
if (timerInterval == PMS_READ_DELAY)
{
DBG.println("Going to sleep");
collectAndSendData();
}
else
{
pms.wakeUp();
DBG.println("Waking up");
}
}
void collectAndSendData()
{
static int id = 0;
String postData = "id=";
postData.concat(id++);
PMS::DATA currData;
PMS::DATA avgData;
memset(&currData, 0, sizeof(currData));
memset(&avgData, 0, sizeof(avgData));
int rawVoltage = analogRead(A0);
#ifdef FU_PROFILE
float voltage = rawVoltage * (4.2 / 1024);
#else
// 180k, max 5.0V
// float voltage = rawVoltage * (5.0 / 1024);
// 100k, max 4.2V
float voltage = rawVoltage * (4.2 / 1024);
#endif // FU_PROFILE
postData.concat("&rv=");
postData.concat(rawVoltage);
postData.concat("&v=");
postData.concat(voltage);
byte samplesTaken = 0;
for (byte sampleIdx = 0; sampleIdx < PMS_READ_SAMPLES; sampleIdx++)
{
pms.requestRead();
if (pms.readUntil(currData, 2000))
{
samplesTaken++;
avgData.PM_AE_UG_1_0 += currData.PM_AE_UG_1_0;
avgData.PM_AE_UG_2_5 += currData.PM_AE_UG_2_5;
avgData.PM_AE_UG_10_0 += currData.PM_AE_UG_10_0;
}
delay(1000);
}
if (samplesTaken > 0)
{
avgData.PM_AE_UG_1_0 /= samplesTaken;
avgData.PM_AE_UG_2_5 /= samplesTaken;
avgData.PM_AE_UG_10_0 /= samplesTaken;
postData.concat("&pm1=");
postData.concat(avgData.PM_AE_UG_1_0);
postData.concat("&pm25=");
postData.concat(avgData.PM_AE_UG_2_5);
postData.concat("&pm10=");
postData.concat(avgData.PM_AE_UG_10_0);
}
pms.sleep();
float temperature = sht.readTemperature();
if (!isnan(temperature))
{
postData.concat("&temperature=");
postData.concat(temperature);
}
float humidity = sht.readHumidity();
if (!isnan(humidity))
{
postData.concat("&humidity=");
postData.concat(humidity);
}
float pressure = bm.readPressure();
if (!isnan(pressure))
{
postData.concat("&pressure=");
postData.concat(pressure / 100.0);
}
float bm_temp = bm.readTemperature();
if (!isnan(bm_temp))
{
postData.concat("&bm280_temp=");
postData.concat(bm_temp);
}
float bm_hum = bm.readHumidity();
if (!isnan(bm_hum))
{
postData.concat("&bm280_hum=");
postData.concat(bm_hum);
}
if (postData.length())
{
saveToFile(postData);
if (WiFi.isConnected()) {
HTTPClient http;
http.begin(WS_URL);
http.setUserAgent(WS_UA);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("X-TOKEN", WS_TOKEN);
int httpCode = http.POST(postData);
http.end();
}
else
{
DBG.println("Error in WiFi connection");
}
}
}
bool saveToFile(const String& data)
{
DBG.println(data);
if (File fp = SPIFFS.open("/data.txt", "a"))
{
fp.println(data);
fp.close();
return true;
}
else
{
DBG.println("Failed to open file for writing.");
return false;
}
}