forked from melkati/CO2-Gadget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCO2_Gadget_WIFI.h
368 lines (341 loc) · 11.9 KB
/
CO2_Gadget_WIFI.h
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// clang-format off
/*****************************************************************************************************/
/********* *********/
/********* SETUP WIFI *********/
/********* *********/
/*****************************************************************************************************/
// clang-format on
#if !defined WIFI_SSID_CREDENTIALS || !defined WIFI_PW_CREDENTIALS
#include "credentials.h"
#endif
WiFiClient espClient;
AsyncWebServer server(80);
void onWifiSettingsChanged(std::string ssid, std::string password) {
Serial.print("-->[WiFi] WifiSetup: SSID = ");
Serial.print(ssid.c_str());
Serial.print(", Password = ");
Serial.println(password.c_str());
WiFi.begin(ssid.c_str(), password.c_str());
}
void printWiFiStatus() { // Print wifi status on serial monitor
// Get current status
// WL_CONNECTED: assigned when connected to a WiFi network;
// WL_NO_SHIELD: assigned when no WiFi shield is present;
// WL_IDLE_STATUS: it is a temporary status assigned when WiFi.begin() is called and remains active until the number of attempts expires (resulting in WL_CONNECT_FAILED) or a connection is established (resulting in WL_CONNECTED);
// WL_NO_SSID_AVAIL: assigned when no SSID are available;
// WL_SCAN_COMPLETED: assigned when the scan networks is completed;
// WL_CONNECT_FAILED: assigned when the connection fails for all the attempts;
// WL_CONNECTION_LOST: assigned when the connection is lost;
// WL_DISCONNECTED: assigned when disconnected from a network;
Serial.print("-->[WiFi-Status] ");
switch (WiFi.status()) {
case WL_CONNECTED:
Serial.println("WiFi connected");
break;
case WL_NO_SHIELD:
Serial.println("No WiFi HW detected");
break;
case WL_IDLE_STATUS:
Serial.println("Attempting...");
break;
case WL_NO_SSID_AVAIL:
Serial.println("No SSID available");
break;
case WL_SCAN_COMPLETED:
Serial.println("Networks scan completed");
break;
case WL_CONNECT_FAILED:
Serial.println("Connect failed");
break;
case WL_CONNECTION_LOST:
Serial.println("Connection lost");
break;
case WL_DISCONNECTED:
Serial.println("Disconnected");
break;
default:
Serial.println("Unknown status");
break;
}
// Print the SSID of the network you're attached to:
Serial.print("-->[WiFi] SSID: ");
Serial.println(WiFi.SSID());
// Print your WiFi shield's IP address:
Serial.print("-->[WiFi] IP Address: ");
Serial.println(WiFi.localIP());
// Print your WiFi shield's MAC address:
Serial.print("-->[WiFi] MAC Address: ");
Serial.println(WiFi.macAddress());
// Print the received signal strength:
Serial.print("-->[WiFi] Signal strength (RSSI):");
Serial.print(WiFi.RSSI());
Serial.println(" dBm");
/*
// Print authentication used:
Serial.print("Encryption type: ");
switch (WiFi.encryptionType()) {
case WIFI_AUTH_OPEN:
Serial.println("Open WiFi");
break;
case WIFI_AUTH_WEP:
Serial.println("WEP");
break;
case WIFI_AUTH_WPA_PSK:
Serial.println("WPA-PSK");
break;
case WIFI_AUTH_WPA2_PSK:
Serial.println("WPA2-PSK");
break;
case WIFI_AUTH_WPA_WPA2_PSK:
Serial.println("WPA-WPA2-PSK");
break;
case WIFI_AUTH_WPA2_ENTERPRISE:
Serial.println("WPA2-Enterprise");
break;
default:
Serial.println("Unknown encryption type");
break;
}
*/
}
void WiFiEvent(WiFiEvent_t event) {
Serial.printf("-->[WiFi-event] event: %d - ", event);
switch (event) {
case SYSTEM_EVENT_WIFI_READY:
Serial.println("WiFi interface ready");
break;
case SYSTEM_EVENT_SCAN_DONE:
Serial.println("Completed scan for access points");
break;
case SYSTEM_EVENT_STA_START:
Serial.println("WiFi client started");
break;
case SYSTEM_EVENT_STA_STOP:
Serial.println("WiFi clients stopped");
break;
case SYSTEM_EVENT_STA_CONNECTED:
Serial.println("Connected to access point");
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("Disconnected from WiFi access point");
break;
case SYSTEM_EVENT_STA_AUTHMODE_CHANGE:
Serial.println("Authentication mode of access point has changed");
break;
case SYSTEM_EVENT_STA_GOT_IP:
Serial.print("Obtained IP address: ");
Serial.println(WiFi.localIP());
break;
case SYSTEM_EVENT_STA_LOST_IP:
Serial.println("Lost IP address and IP address is reset to 0");
break;
case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
Serial.println("WiFi Protected Setup (WPS): succeeded in enrollee mode");
break;
case SYSTEM_EVENT_STA_WPS_ER_FAILED:
Serial.println("WiFi Protected Setup (WPS): failed in enrollee mode");
break;
case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
Serial.println("WiFi Protected Setup (WPS): timeout in enrollee mode");
break;
case SYSTEM_EVENT_STA_WPS_ER_PIN:
Serial.println("WiFi Protected Setup (WPS): pin code in enrollee mode");
break;
case SYSTEM_EVENT_AP_START:
Serial.println("WiFi access point started");
break;
case SYSTEM_EVENT_AP_STOP:
Serial.println("WiFi access point stopped");
break;
case SYSTEM_EVENT_AP_STACONNECTED:
Serial.println("Client connected");
break;
case SYSTEM_EVENT_AP_STADISCONNECTED:
Serial.println("Client disconnected");
break;
case SYSTEM_EVENT_AP_STAIPASSIGNED:
Serial.println("Assigned IP address to client");
break;
case SYSTEM_EVENT_AP_PROBEREQRECVED:
Serial.println("Received probe request");
break;
case SYSTEM_EVENT_GOT_IP6:
Serial.println("IPv6 is preferred");
break;
case SYSTEM_EVENT_ETH_START:
Serial.println("Ethernet started");
break;
case SYSTEM_EVENT_ETH_STOP:
Serial.println("Ethernet stopped");
break;
case SYSTEM_EVENT_ETH_CONNECTED:
Serial.println("Ethernet connected");
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
Serial.println("Ethernet disconnected");
break;
case SYSTEM_EVENT_ETH_GOT_IP:
Serial.println("Obtained IP address");
break;
default: break;
}
}
#ifdef SUPPORT_MDNS
void initMDNS() {
/*use mdns for host name resolution*/
if (!MDNS.begin(hostName.c_str())) { // http://esp32.local
Serial.println("-->[WiFi] Error setting up MDNS responder!");
while (1) {
delay(1000);
}
}
Serial.print("-->[WiFi] mDNS responder started. CO2 Gadget web interface at: http://");
Serial.print(hostName);
Serial.println(".local");
MDNS.addService("http", "tcp", 80);
}
#endif
void disableWiFi() {
WiFi.disconnect(true); // Disconnect from the network
WiFi.mode(WIFI_OFF); // Switch WiFi off
Serial.println("-->[WiFi] WiFi dissabled!");
}
// Replaces placeholder with actual values
String processor(const String& var) {
//Serial.println(var);
if (var == "CO2") {
return String(co2);
}
else if (var == "TEMPERATURE") {
return String(temp);
}
else if (var == "HUMIDITY") {
return String(hum);
}
return String();
}
void serialPrintMACAddress() {
byte mac[6];
WiFi.macAddress(mac);
Serial.print("-->[WiFi] MAC: ");
Serial.print(mac[5],HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.println(mac[0],HEX);
}
bool checkStringIsNumerical(String myString) {
uint16_t Numbers = 0;
for (uint16_t i = 0; i < myString.length(); i++) {
if (myString[i] == '0' || myString[i] == '1' || myString[i] == '2' ||
myString[i] == '3' || myString[i] == '4' || myString[i] == '5' ||
myString[i] == '6' || myString[i] == '7' || myString[i] == '8' ||
myString[i] == '9') {
Numbers++;
}
}
if (Numbers == myString.length()) {
return true;
} else {
return false;
}
}
const char* PARAM_INPUT_1 = "MeasurementInterval";
void initWifi() {
uint16_t connectionRetries = 0;
uint16_t maxConnectionRetries = 30;
if (activeWIFI) {
displayNotification("Init WiFi", notifyInfo);
WiFi.disconnect(true); // disconnect form wifi to set new wifi connection
WiFi.mode(WIFI_STA);
WiFi.onEvent(WiFiEvent);
// WiFi.setSleep(true);
// WiFi.setSleep(WIFI_PS_NONE);
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
Serial.printf("-->[WiFi] Setting hostname %s: %d\n", hostName.c_str(),
WiFi.setHostname(hostName.c_str()));
WiFi.begin(wifiSSID.c_str(), wifiPass.c_str());
Serial.print("-->[WiFi] Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
++connectionRetries;
if (connectionRetries == maxConnectionRetries) {
activeWIFI = false;
Serial.printf(
"\n[WiFi] Not possible to connect to WiFi after %d "
"tries.\nDisabling WiFi.\n",
connectionRetries);
Serial.print("-->[WiFi] wifiSSID: #");
Serial.print(wifiSSID);
Serial.println("#");
#ifndef WIFI_PRIVACY
Serial.print("-->[WiFi] wifiPass: #");
Serial.print(wifiPass);
Serial.println("#");
#endif
return;
}
Serial.print(".");
delay(500);
}
Serial.println("");
serialPrintMACAddress();
Serial.print("-->[WiFi] WiFi connected - IP = ");
Serial.println(WiFi.localIP());
#ifdef SUPPORT_MDNS
mDNSName = WiFi.getHostname();
initMDNS();
#endif
/*server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/html", MAIN_page);
AsyncWebServerResponse *response = request->beginResponse_P(200,
"text/html", MAIN_page, processor); response->addHeader("Server","ESP Async
Web Server"); request->send(response);
});*/
SPIFFS.begin();
server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html");
server.on("/readCO2", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", String(co2));
});
server.on("/readTemperature", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", String(temp));
});
server.on("/readHumidity", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", String(hum));
});
server.on("/settings", HTTP_GET, [](AsyncWebServerRequest *request) {
String inputString;
// <CO2-GADGET_IP>/settings?MeasurementInterval=100
if (request->hasParam(PARAM_INPUT_1)) {
inputString = request->getParam(PARAM_INPUT_1)->value();
if (checkStringIsNumerical(inputString)) {
Serial.printf("-->[WiFi] Received /settings command MeasurementInterval with parameter %s\n", inputString);
measurementInterval = inputString.toInt();
}
};
request->send(200, "text/plain", "OK. Setting MeasurementInterval to " + inputString + ", please re-calibrate your sensor.");
});
server.onNotFound([](AsyncWebServerRequest *request) {
request->send(400, "text/plain", "Not found");
});
#ifdef SUPPORT_OTA
AsyncElegantOTA.begin(&server); // Start ElegantOTA
Serial.println("-->[WiFi] OTA ready");
#endif
server.begin();
Serial.println("-->[WiFi] HTTP server started");
printWiFiStatus();
} else {
disableWiFi();
}
}
void OTALoop() {
#ifdef SUPPORT_OTA
AsyncElegantOTA.loop();
#endif
}