web-server.ino 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Web Server Stick v3
  3. Author: Toby Chui
  4. This firmware load and serve web content
  5. from microSD card.
  6. The following firmware config are recommended
  7. Board: Wemos D1 Mini
  8. CPU clockspeed: 160Mhz
  9. IwIP Varient: v2 Higher Bandwidth
  10. Require external library:
  11. - ESPAsyncTCP (https://github.com/me-no-dev/ESPAsyncTCP)
  12. - ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer)
  13. - ArduinoJson (https://github.com/bblanchon/ArduinoJson)
  14. - ESPping (https://github.com/dvarrel/ESPping)
  15. - Wake On LAN (https://github.com/a7md0/WakeOnLan)
  16. */
  17. //WiFi library
  18. #include <ESP8266WiFi.h>
  19. //SD cards library
  20. #include <SPI.h>
  21. #include <SD.h>
  22. #include <FS.h>
  23. //Web server library
  24. #include <ESPAsyncTCP.h>
  25. #include <ESPAsyncWebServer.h>
  26. #include <ArduinoJson.h>
  27. //Discovery related library
  28. #include <ESP8266mDNS.h>
  29. #include <ESPping.h>
  30. #include <NTPClient.h>
  31. #include <WiFiUdp.h>
  32. #include <WakeOnLan.h>
  33. /* Hardware Configurations */
  34. #define CS_PIN D0
  35. /* SD Card SPI Speed Definations */
  36. #define SDSPI_HALF_SPEED 4000000
  37. #define SDSPI_DEFAULT_SPEED 32000000
  38. #define SDSPI_HIGH_SPEED 64000000
  39. /* Software Global Variables */
  40. AsyncWebServer server(80);
  41. String adminUsername = "";
  42. String adminPassword = "";
  43. String mdnsName = "webstick";
  44. String authSession = ""; //Session key for admin
  45. int SDCardInitSpeed = SDSPI_HIGH_SPEED;
  46. /* Time Keeping */
  47. WiFiUDP ntpUDP;
  48. NTPClient timeClient(ntpUDP, "pool.ntp.org");
  49. /* Wake On Lan */
  50. WakeOnLan WOL(ntpUDP);
  51. /* Debug variables */
  52. /* Function definations */
  53. String loadWiFiInfoFromSD();
  54. void setup() {
  55. // Setup Debug Serial Port
  56. Serial.begin(115200);
  57. //Try Initialize SD card (blocking)
  58. while (!SD.begin(CS_PIN, SDCardInitSpeed)) {
  59. if (SDCardInitSpeed == SDSPI_HIGH_SPEED) {
  60. //Fallback to default speed
  61. SDCardInitSpeed = SDSPI_DEFAULT_SPEED;
  62. Serial.println("SD card initialization failed. Falling back to default speed");
  63. } else if (SDCardInitSpeed == SDSPI_DEFAULT_SPEED) {
  64. //Fallback to half speed (legacy mode)
  65. SDCardInitSpeed = SDSPI_HALF_SPEED;
  66. Serial.println("SD card initialization failed. Falling back to legacy SPI_HALF_SPEED");
  67. }else{
  68. Serial.println("SD card initialization failed. Retrying in 3 seconds...");
  69. }
  70. delay(3000);
  71. }
  72. Serial.println("SD card initialized");
  73. Serial.println("\n\nStorage Info:");
  74. Serial.println("----------------------");
  75. getSDCardTotalSpace();
  76. getSDCardUsedSpace();
  77. Serial.println("----------------------");
  78. Serial.println();
  79. //Connect to wifi based on settings (cfg/wifi.txt)
  80. initWiFiConn();
  81. //Load admin credentials from SD card (cfg/admin.txt)
  82. initAdminCredentials();
  83. //Start mDNS service
  84. initmDNSName();
  85. if (!MDNS.begin(mdnsName)) {
  86. Serial.println("mDNS Error. Skipping.");
  87. } else {
  88. Serial.println("mDNS started. Connect to your webstick using http://" + mdnsName + ".local");
  89. MDNS.addService("http", "tcp", 80);
  90. }
  91. //Start NTP time client
  92. timeClient.begin();
  93. Serial.print("Requesting time from NTP (unix timestamp): ");
  94. timeClient.update();
  95. Serial.println(getTime());
  96. //Wake on Lan Settings
  97. WOL.setRepeat(3, 100);
  98. WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask());
  99. //Initialize database
  100. DBInit();
  101. //Resume login session if any
  102. initLoginSessionKey();
  103. // Start listening to HTTP Requests
  104. initWebServer();
  105. }
  106. void loop() {
  107. MDNS.update();
  108. timeClient.update();
  109. }