|
@@ -1,23 +1,23 @@
|
|
|
/*
|
|
|
- *
|
|
|
- * Web Server Stick v3
|
|
|
- * Author: Toby Chui
|
|
|
- *
|
|
|
- * This firmware load and serve web content
|
|
|
- * from microSD card.
|
|
|
- *
|
|
|
- * The following firmware config are recommended
|
|
|
- * Board: Wemos D1 Mini
|
|
|
- * CPU clockspeed: 160Mhz
|
|
|
- * IwIP Varient: v2 Higher Bandwidth
|
|
|
- *
|
|
|
- * Require external library:
|
|
|
- * - ESPAsyncTCP (https://github.com/me-no-dev/ESPAsyncTCP)
|
|
|
- * - ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer)
|
|
|
- * - ArduinoJson (https://github.com/bblanchon/ArduinoJson)
|
|
|
- * - ESPping (https://github.com/dvarrel/ESPping)
|
|
|
- * - Wake On LAN (https://github.com/a7md0/WakeOnLan)
|
|
|
- */
|
|
|
+
|
|
|
+ Web Server Stick v3
|
|
|
+ Author: Toby Chui
|
|
|
+
|
|
|
+ This firmware load and serve web content
|
|
|
+ from microSD card.
|
|
|
+
|
|
|
+ The following firmware config are recommended
|
|
|
+ Board: Wemos D1 Mini
|
|
|
+ CPU clockspeed: 160Mhz
|
|
|
+ IwIP Varient: v2 Higher Bandwidth
|
|
|
+
|
|
|
+ Require external library:
|
|
|
+ - ESPAsyncTCP (https://github.com/me-no-dev/ESPAsyncTCP)
|
|
|
+ - ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer)
|
|
|
+ - ArduinoJson (https://github.com/bblanchon/ArduinoJson)
|
|
|
+ - ESPping (https://github.com/dvarrel/ESPping)
|
|
|
+ - Wake On LAN (https://github.com/a7md0/WakeOnLan)
|
|
|
+*/
|
|
|
|
|
|
//WiFi library
|
|
|
#include <ESP8266WiFi.h>
|
|
@@ -43,12 +43,18 @@
|
|
|
/* Hardware Configurations */
|
|
|
#define CS_PIN D0
|
|
|
|
|
|
+/* SD Card SPI Speed Definations */
|
|
|
+#define SDSPI_HALF_SPEED 4000000
|
|
|
+#define SDSPI_DEFAULT_SPEED 32000000
|
|
|
+#define SDSPI_HIGH_SPEED 64000000
|
|
|
+
|
|
|
/* Software Global Variables */
|
|
|
AsyncWebServer server(80);
|
|
|
String adminUsername = "";
|
|
|
String adminPassword = "";
|
|
|
String mdnsName = "webstick";
|
|
|
String authSession = ""; //Session key for admin
|
|
|
+int SDCardInitSpeed = SDSPI_HIGH_SPEED;
|
|
|
|
|
|
/* Time Keeping */
|
|
|
WiFiUDP ntpUDP;
|
|
@@ -66,10 +72,20 @@ String loadWiFiInfoFromSD();
|
|
|
void setup() {
|
|
|
// Setup Debug Serial Port
|
|
|
Serial.begin(9600);
|
|
|
-
|
|
|
+
|
|
|
//Try Initialize SD card (blocking)
|
|
|
- while (!SD.begin(CS_PIN, 32000000)){
|
|
|
- Serial.println("SD card initialization failed. Retrying in 3 seconds...");
|
|
|
+ while (!SD.begin(CS_PIN, SDCardInitSpeed)) {
|
|
|
+ if (SDCardInitSpeed == SDSPI_HIGH_SPEED) {
|
|
|
+ //Fallback to default speed
|
|
|
+ SDCardInitSpeed = SDSPI_DEFAULT_SPEED;
|
|
|
+ Serial.println("SD card initialization failed. Falling back to default speed");
|
|
|
+ } else if (SDCardInitSpeed == SDSPI_DEFAULT_SPEED) {
|
|
|
+ //Fallback to half speed (legacy mode)
|
|
|
+ SDCardInitSpeed = SDSPI_HALF_SPEED;
|
|
|
+ Serial.println("SD card initialization failed. Falling back to legacy SPI_HALF_SPEED");
|
|
|
+ }else{
|
|
|
+ Serial.println("SD card initialization failed. Retrying in 3 seconds...");
|
|
|
+ }
|
|
|
delay(3000);
|
|
|
}
|
|
|
Serial.println("SD card initialized");
|
|
@@ -85,14 +101,14 @@ void setup() {
|
|
|
|
|
|
//Load admin credentials from SD card (cfg/admin.txt)
|
|
|
initAdminCredentials();
|
|
|
-
|
|
|
+
|
|
|
//Start mDNS service
|
|
|
initmDNSName();
|
|
|
- if (!MDNS.begin(mdnsName)){
|
|
|
- Serial.println("mDNS Error. Skipping.");
|
|
|
- }else{
|
|
|
- Serial.println("mDNS started. Connect to your webstick using http://" + mdnsName + ".local");
|
|
|
- MDNS.addService("http", "tcp", 80);
|
|
|
+ if (!MDNS.begin(mdnsName)) {
|
|
|
+ Serial.println("mDNS Error. Skipping.");
|
|
|
+ } else {
|
|
|
+ Serial.println("mDNS started. Connect to your webstick using http://" + mdnsName + ".local");
|
|
|
+ MDNS.addService("http", "tcp", 80);
|
|
|
}
|
|
|
|
|
|
//Start NTP time client
|
|
@@ -110,13 +126,13 @@ void setup() {
|
|
|
|
|
|
//Resume login session if any
|
|
|
initLoginSessionKey();
|
|
|
-
|
|
|
+
|
|
|
// Start listening to HTTP Requests
|
|
|
initWebServer();
|
|
|
}
|
|
|
-
|
|
|
|
|
|
-void loop(){
|
|
|
+
|
|
|
+void loop() {
|
|
|
MDNS.update();
|
|
|
timeClient.update();
|
|
|
}
|