web-server.ino 2.7 KB

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