-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
319 lines (268 loc) · 11.2 KB
/
main.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
#include <ArduinoOTA.h>
#include <WiFi.h>
#include <BH1750.h>
#include <Adafruit_VEML6075.h>
#include <WeatherCalculations.h>
#include <Adafruit_BME680.h>
#include <PMS7003-SOLDERED.h>
#include <ThingSpeak.h>
unsigned long two_second_jobs = 0, ten_second_jobs = 0, five_minute_jobs = 0;
const uint8_t pollutants_TX = 18;
const uint8_t pollutants_RX = 19;
double temperature = 0, humidity = 0, seaLevelPressure = 0, gas = 0, heatIndex = 0, dewPoint = 0, lightIntensity = 0, UVindex = 0;
uint16_t PM01 = 0, PM25 = 0, PM10 = 0, AQI = 0;
Weather::Comfort comfort = Weather::comfortable;
std::map<Weather::Comfort, String> comfortLevel = {
{ Weather::uncomfortable, "Uncomfortable" },
{ Weather::comfortable, "Comfortable" },
{ Weather::small_discomfort, "Some discomfort" },
{ Weather::medium_discomfort, "Hot feeling" },
{ Weather::great_discomfort, "Great discomfort" },
{ Weather::dangerous, "Dangerous" }
};
double pressureData[288];
Weather::PressureTrend pressureTrend = Weather::steady; //Stable pressure
const double pressureDifference = 0.5; //Determines if the pressure is steady, falling or rising. If the pressure now is by 1.5 hPa greater or less than 3 hours ago, change is recorded, otherwise it is considered to be steady.
const uint8_t month = 6; //Set the correct month! TO BE ADDED A REAL TIME CLOCK! Currently it's December
const double altitude = XXX; //Set your altitude, necessary for barometric pressure calculation
const char* SSID = "XXXXXXXXXXXX"; //Replace with your own WiFI SSID
const char* password = "XXXXXXXXXXXX"; //Replace with your WiFi network's password
const unsigned long channelID = XXXXXXXXXXXX; //Replace with your ThingSpeak channel's ID
const char* APIkey = "XXXXXXXXXXXX"; //Replace with your ThingSpeak API key
const char* OTApassword = "admin"; //Recommended to change OTA password, default is "admin"
const char* WXStationHostname = "esp32-wxstation"; //Hostname for ESP32, change if you want
WiFiServer server(80);
WiFiClient client;
Adafruit_BME680 bme;
PMS7003 pollutants(pollutants_RX, pollutants_TX);
Adafruit_VEML6075 uv = Adafruit_VEML6075();
BH1750 light;
Weather weather;
void setup() {
Serial.println("Booting");
bme.begin();
/* Default settings from datasheet. */
// Set up oversampling and filter initialization
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150); // 320*C for 150 ms
pollutants.begin();
uv.begin();
light.begin();
WiFi.disconnect(true);
WiFi.onEvent(WiFiEvent);
WiFi.begin(SSID, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
ArduinoOTA.setPassword(OTApassword);
ArduinoOTA.setHostname(WXStationHostname);
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
ThingSpeak.begin(client);
}
void loop() {
ArduinoOTA.handle();
if (millis() - ten_second_jobs >= 10000) {
pollutants.read();
PM01 = pollutants.pm01;
PM25 = pollutants.pm25;
PM10 = pollutants.pm10;
AQI = weather.getAQI(PM25, PM10);
ten_second_jobs = millis();
}
if (millis() - two_second_jobs >= 2000) {
bme.performReading();
temperature = bme.temperature;
humidity = bme.humidity;
seaLevelPressure = weather.getSeaLevelPressure(bme.pressure * 0.01, altitude);
gas = bme.gas_resistance * 0.001; //Multiply by 1e-3 to get in kOhms
dewPoint = weather.getDewPoint(temperature, humidity);
if (temperature >= 25.0) heatIndex = weather.getHeatIndex(temperature, humidity);
else heatIndex = temperature;
comfort = weather.getComfort(heatIndex);
UVindex = uv.readUVI();
lightIntensity = light.readLightLevel();
humidity = rounding(humidity, 1);
temperature = rounding(temperature, 1);
seaLevelPressure = rounding(seaLevelPressure, 1);
gas = rounding(gas, 1);
dewPoint = rounding(dewPoint, 1);
heatIndex = rounding(heatIndex, 1);
UVindex = rounding(UVindex, 1);
lightIntensity = rounding(lightIntensity, 1);
two_second_jobs = millis();
}
WiFiClient client = server.available();
if (client) {
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n') {
if (currentLine.length() == 0) {
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-type:text/html"));
client.println(F("Connection: close"));
client.println();
//Here goes the HTML code
client.println(F("<!DOCTYPE html><html>"));
client.println(F("<head><meta charset=\"UTF-8\" name=\"viewport\" content=\"width=device-width, initial-scale=1\"><meta http-equiv=\"refresh\" content=\"60\"><title>ESP32 Weather Station</title></head>"));
client.println(F("<body><center><h1>ESP32 Weather Station</h1></center><center>"));
client.print(F("<p>Temperature: "));
client.print(temperature, 1);
client.println(F(" °C</p>"));
client.print(F("<p>Humidity: "));
client.print(humidity, 1);
client.println(F(" %</p>"));
client.print(F("<p>Pressure: "));
client.print(seaLevelPressure, 1);
client.println(F(" hPa</p>"));
client.print(F("<p>Pressure Trend: "));
if (pressureTrend == Weather::rising) {
client.print(F("Rising"));
} else if (pressureTrend == Weather::falling) {
client.print(F("Falling"));
} else if (pressureTrend == Weather::steady) {
client.print(F("Steady"));
} else {
client.print(F("Invalid!"));
}
client.println(F("</p>"));
client.print(F("<p>Pressure 3 Hours Ago: "));
client.print(pressureData[35], 1);
client.println(F(" hPa</p>"));
client.print(F("<p>Pressure 6 Hours Ago: "));
client.print(pressureData[71], 1);
client.println(F(" hPa</p>"));
client.print(F("<p>Pressure 12 Hours Ago: "));
client.print(pressureData[143], 1);
client.println(F(" hPa</p>"));
client.print(F("<p>Pressure 24 Hours Ago: "));
client.print(pressureData[287], 1);
client.println(F(" hPa</p>"));
client.print(F("<p>Dew Point: "));
client.print(dewPoint, 1);
client.println(F(" °C</p>"));
client.print(F("<p>Heat Index: "));
client.print(heatIndex, 1);
client.println(F(" °C</p>"));
client.print(F("<p>UV Index: "));
client.println(UVindex, 1);
client.println(F("</p>"));
client.print(F("<p>Light Intensity: "));
client.print(lightIntensity, 1);
client.println(F(" lux</p>"));
client.print(F("<p>Gas: "));
client.print(gas, 1);
client.println(F(" kOhms</p>"));
client.print(F("<p>PM10: "));
client.print(PM10);
client.println(F(" ug/m3</p>"));
client.print(F("<p>PM2.5: "));
client.print(PM25);
client.println(F(" ug/m3</p>"));
client.print(F("<p>PM1: "));
client.print(PM01);
client.println(F(" ug/m3</p>"));
client.print(F("<p>AQI: "));
client.print(AQI);
client.println(F("</p>"));
client.print(F("<p>Comfort Level: "));
//client.println(comfortLevel[comfort - 1]);
client.println(comfortLevel[comfort]);
client.println(F("</p>"));
client.print(F("<p>12-hour Forecast: "));
client.println(weather.getForecast(seaLevelPressure, month, Weather::NOW, pressureTrend));
client.println(F("</p>"));
client.println(F("</body></html>"));
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
client.stop();
}
if (millis() - five_minute_jobs >= 300000) { //5 minute update interval
rightShiftArray(pressureData, sizeof(pressureData) / sizeof(pressureData[0]));
pressureData[0] = seaLevelPressure;
if ((pressureData[0] - pressureData[35] >= pressureDifference) && (pressureData[35] != 0.0) && (pressureData[0] != 0.0)) {
pressureTrend = Weather::rising;
} else if ((pressureData[0] - pressureData[35] <= -1 * pressureDifference) && (pressureData[35] != 0.0) && (pressureData[0] != 0.0)) {
pressureTrend = Weather::falling;
} else {
pressureTrend = Weather::steady;
}
ThingSpeak.setField(1, (float)temperature);
ThingSpeak.setField(2, (float)humidity);
ThingSpeak.setField(3, (float)seaLevelPressure);
ThingSpeak.setField(4, (float)dewPoint);
ThingSpeak.setField(5, (float)heatIndex);
ThingSpeak.setField(6, (float)UVindex);
ThingSpeak.setField(7, (float)lightIntensity);
ThingSpeak.setField(8, (float)AQI);
ThingSpeak.setStatus(String(weather.getForecast(seaLevelPressure, month, Weather::NOW, pressureTrend))); //Weather::NOW is accessing WindDirection map inside WeatherCalculations library
//NOW is abbreviation from "NO Wind"
ThingSpeak.writeFields(channelID, APIkey);
five_minute_jobs = millis();
}
}
void WiFiEvent(WiFiEvent_t event) {
switch (event) {
case WIFI_EVENT_STA_DISCONNECTED:
wifiOnDisconnect();
break;
case WIFI_EVENT_STA_CONNECTED:
WiFi.enableIPv6(); //Enable IPv6 support for ESP32
break;
default:
break;
}
}
void wifiOnDisconnect() {
delay(1000);
WiFi.begin(SSID, password);
}
void rightShiftArray(double array[], const int size) {
for (int i = size - 1; i > 0; i--) {
array[i] = array[i - 1];
}
}
double rounding(const double number, const int decimals) {
double factor = pow(10, decimals);
return round(number * factor) / factor;
}