2018-02-26 00:11:31 +00:00
|
|
|
#include <Arduino.h>
|
2018-11-11 16:36:41 +00:00
|
|
|
|
2018-11-11 16:44:29 +00:00
|
|
|
#if defined(ESP8266)
|
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <ESPAsyncTCP.h>
|
|
|
|
#elif defined(ESP_PLATFORM)
|
|
|
|
#include <WiFi.h>
|
|
|
|
#include <AsyncTCP.h>
|
|
|
|
#include <SPIFFS.h>
|
|
|
|
#endif
|
2018-11-11 16:36:41 +00:00
|
|
|
|
2018-11-11 16:44:29 +00:00
|
|
|
#include <FS.h>
|
2019-05-18 18:35:27 +00:00
|
|
|
|
|
|
|
#include <SecurityManager.h>
|
2018-02-26 00:11:31 +00:00
|
|
|
#include <WiFiSettingsService.h>
|
|
|
|
#include <APSettingsService.h>
|
|
|
|
#include <NTPSettingsService.h>
|
|
|
|
#include <OTASettingsService.h>
|
2019-05-18 18:35:27 +00:00
|
|
|
#include <AuthenticationService.h>
|
|
|
|
#include <WiFiScanner.h>
|
|
|
|
#include <WiFiStatus.h>
|
|
|
|
#include <NTPStatus.h>
|
2018-02-26 00:11:31 +00:00
|
|
|
#include <APStatus.h>
|
2019-05-26 23:04:29 +00:00
|
|
|
#include <SystemStatus.h>
|
2018-02-26 00:11:31 +00:00
|
|
|
|
|
|
|
#define SERIAL_BAUD_RATE 115200
|
|
|
|
|
|
|
|
AsyncWebServer server(80);
|
|
|
|
|
2019-04-30 22:49:28 +00:00
|
|
|
SecurityManager securityManager = SecurityManager(&server, &SPIFFS);
|
|
|
|
|
2018-02-26 00:11:31 +00:00
|
|
|
WiFiSettingsService wifiSettingsService = WiFiSettingsService(&server, &SPIFFS);
|
|
|
|
APSettingsService apSettingsService = APSettingsService(&server, &SPIFFS);
|
|
|
|
NTPSettingsService ntpSettingsService = NTPSettingsService(&server, &SPIFFS);
|
|
|
|
OTASettingsService otaSettingsService = OTASettingsService(&server, &SPIFFS);
|
2019-05-18 18:35:27 +00:00
|
|
|
AuthenticationService authenticationService = AuthenticationService(&server, &securityManager);
|
2018-03-11 08:37:38 +00:00
|
|
|
|
|
|
|
WiFiScanner wifiScanner = WiFiScanner(&server);
|
|
|
|
WiFiStatus wifiStatus = WiFiStatus(&server);
|
2018-02-26 00:11:31 +00:00
|
|
|
NTPStatus ntpStatus = NTPStatus(&server);
|
|
|
|
APStatus apStatus = APStatus(&server);
|
2019-05-26 23:04:29 +00:00
|
|
|
SystemStatus systemStatus = SystemStatus(&server);
|
2018-02-26 00:11:31 +00:00
|
|
|
|
|
|
|
void setup() {
|
|
|
|
// Disable wifi config persistance
|
|
|
|
WiFi.persistent(false);
|
|
|
|
|
|
|
|
Serial.begin(SERIAL_BAUD_RATE);
|
|
|
|
SPIFFS.begin();
|
|
|
|
|
2019-04-30 22:49:28 +00:00
|
|
|
// start security manager
|
|
|
|
securityManager.begin();
|
|
|
|
|
2018-02-26 00:11:31 +00:00
|
|
|
// start services
|
|
|
|
ntpSettingsService.begin();
|
|
|
|
otaSettingsService.begin();
|
|
|
|
apSettingsService.begin();
|
|
|
|
wifiSettingsService.begin();
|
|
|
|
|
|
|
|
// Serving static resources from /www/
|
|
|
|
server.serveStatic("/js/", SPIFFS, "/www/js/");
|
|
|
|
server.serveStatic("/css/", SPIFFS, "/www/css/");
|
|
|
|
server.serveStatic("/fonts/", SPIFFS, "/www/fonts/");
|
|
|
|
server.serveStatic("/app/", SPIFFS, "/www/app/");
|
2018-11-11 17:53:58 +00:00
|
|
|
server.serveStatic("/favicon.ico", SPIFFS, "/www/favicon.ico");
|
2018-02-26 00:11:31 +00:00
|
|
|
|
|
|
|
// Serving all other get requests with "/www/index.htm"
|
2018-02-26 22:55:58 +00:00
|
|
|
// OPTIONS get a straight up 200 response
|
2018-02-26 00:11:31 +00:00
|
|
|
server.onNotFound([](AsyncWebServerRequest *request) {
|
|
|
|
if (request->method() == HTTP_GET) {
|
|
|
|
request->send(SPIFFS, "/www/index.html");
|
2018-02-26 22:55:58 +00:00
|
|
|
} else if (request->method() == HTTP_OPTIONS) {
|
|
|
|
request->send(200);
|
2018-02-26 00:11:31 +00:00
|
|
|
} else {
|
|
|
|
request->send(404);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-02-26 22:55:58 +00:00
|
|
|
// Disable CORS if required
|
|
|
|
#if defined(ENABLE_CORS)
|
2019-05-19 16:51:57 +00:00
|
|
|
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", CORS_ORIGIN);
|
|
|
|
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "Accept, Content-Type, Authorization");
|
|
|
|
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Credentials", "true");
|
2018-02-26 22:55:58 +00:00
|
|
|
#endif
|
|
|
|
|
2018-02-26 00:11:31 +00:00
|
|
|
server.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
apSettingsService.loop();
|
|
|
|
ntpSettingsService.loop();
|
|
|
|
otaSettingsService.loop();
|
|
|
|
}
|