Fix crash on startup when not configured - ESP32

This commit is contained in:
Rick Watson 2019-06-07 20:08:46 +01:00
parent 142235c355
commit cf2a20fbd4

View File

@ -10,7 +10,7 @@
#endif #endif
#include <FS.h> #include <FS.h>
#include <esp_wifi.h>
#include <SecuritySettingsService.h> #include <SecuritySettingsService.h>
#include <WiFiSettingsService.h> #include <WiFiSettingsService.h>
#include <APSettingsService.h> #include <APSettingsService.h>
@ -41,49 +41,58 @@ APStatus apStatus = APStatus(&server, &securitySettingsService);
SystemStatus systemStatus = SystemStatus(&server, &securitySettingsService);; SystemStatus systemStatus = SystemStatus(&server, &securitySettingsService);;
void setup() { void setup() {
// Disable wifi config persistance and auto reconnect // Disable wifi config persistance and auto reconnect
WiFi.persistent(false); WiFi.persistent(false);
WiFi.setAutoReconnect(false); WiFi.setAutoReconnect(false);
#if defined(ESP_PLATFORM)
// Init the wifi driver on ESP32
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_err_t err = esp_wifi_init(&cfg);
if(err){
log_e("esp_wifi_init %d", err);
}
#endif
Serial.begin(SERIAL_BAUD_RATE); Serial.begin(SERIAL_BAUD_RATE);
SPIFFS.begin(); SPIFFS.begin();
// Start security settings service first
securitySettingsService.begin();
// start security settings service first // Start services
securitySettingsService.begin(); ntpSettingsService.begin();
otaSettingsService.begin();
apSettingsService.begin();
wifiSettingsService.begin();
// start services // Serving static resources from /www/
ntpSettingsService.begin(); server.serveStatic("/js/", SPIFFS, "/www/js/");
otaSettingsService.begin(); server.serveStatic("/css/", SPIFFS, "/www/css/");
apSettingsService.begin(); server.serveStatic("/fonts/", SPIFFS, "/www/fonts/");
wifiSettingsService.begin(); server.serveStatic("/app/", SPIFFS, "/www/app/");
server.serveStatic("/favicon.ico", SPIFFS, "/www/favicon.ico");
// Serving static resources from /www/ // Serving all other get requests with "/www/index.htm"
server.serveStatic("/js/", SPIFFS, "/www/js/"); // OPTIONS get a straight up 200 response
server.serveStatic("/css/", SPIFFS, "/www/css/"); server.onNotFound([](AsyncWebServerRequest *request) {
server.serveStatic("/fonts/", SPIFFS, "/www/fonts/"); if (request->method() == HTTP_GET) {
server.serveStatic("/app/", SPIFFS, "/www/app/"); request->send(SPIFFS, "/www/index.html");
server.serveStatic("/favicon.ico", SPIFFS, "/www/favicon.ico"); } else if (request->method() == HTTP_OPTIONS) {
request->send(200);
} else {
request->send(404);
}
});
// Serving all other get requests with "/www/index.htm" // Disable CORS if required
// OPTIONS get a straight up 200 response #if defined(ENABLE_CORS)
server.onNotFound([](AsyncWebServerRequest *request) { DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", CORS_ORIGIN);
if (request->method() == HTTP_GET) { DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "Accept, Content-Type, Authorization");
request->send(SPIFFS, "/www/index.html"); DefaultHeaders::Instance().addHeader("Access-Control-Allow-Credentials", "true");
} else if (request->method() == HTTP_OPTIONS) { #endif
request->send(200);
} else {
request->send(404);
}
});
// Disable CORS if required server.begin();
#if defined(ENABLE_CORS)
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");
#endif
server.begin();
} }
void loop() { void loop() {