web-server.ino 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. *
  3. * Web Server Stick
  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. */
  19. //WiFi library
  20. #include <ESP8266WiFi.h>
  21. //SD cards library
  22. #include <SPI.h>
  23. #include <SD.h>
  24. #include <FS.h>
  25. //Web server library
  26. #include <ESPAsyncTCP.h>
  27. #include <ESPAsyncWebServer.h>
  28. #include <ArduinoJson.h>
  29. //Discovery related library
  30. #include <ESP8266mDNS.h>
  31. #include <NTPClient.h>
  32. #include <WiFiUdp.h>
  33. /* Hardware Configurations */
  34. #define CS_PIN D0
  35. /* Software Global Variables */
  36. AsyncWebServer server(80);
  37. String adminUsername = "";
  38. String adminPassword = "";
  39. String mdnsName = "webstick";
  40. String authSession = "";
  41. /* Time Keeping */
  42. WiFiUDP ntpUDP;
  43. NTPClient timeClient(ntpUDP, "pool.ntp.org");
  44. /* Debug variables */
  45. /* Function definations */
  46. String loadWiFiInfoFromSD();
  47. void setup() {
  48. // Setup Debug Serial Port
  49. Serial.begin(9600);
  50. //Try Initialize SD card (blocking)
  51. while (!SD.begin(CS_PIN, 32000000)){
  52. Serial.println("SD card initialization failed. Retrying in 3 seconds...");
  53. delay(3000);
  54. }
  55. Serial.println("SD card initialized");
  56. Serial.println("\n\nStorage Info:");
  57. Serial.println("----------------------");
  58. getSDCardTotalSpace();
  59. getSDCardUsedSpace();
  60. Serial.println("----------------------");
  61. Serial.println();
  62. //Connect to wifi based on settings (cfg/wifi.txt)
  63. initWiFiConn();
  64. //Load admin credentials from SD card (cfg/admin.txt)
  65. initAdminCredentials();
  66. //Start mDNS service
  67. initmDNSName();
  68. if (!MDNS.begin(mdnsName)){
  69. Serial.println("mDNS Error. Skipping.");
  70. }else{
  71. Serial.println("mDNS started. Connect to your webstick using http://" + mdnsName + ".local");
  72. MDNS.addService("http", "tcp", 80);
  73. }
  74. //Start NTP time client
  75. timeClient.begin();
  76. Serial.print("Requesting time from NTP (unix timestamp): ");
  77. timeClient.update();
  78. Serial.println(getTime());
  79. //Initialize database
  80. DBInit();
  81. //Resume login session if any
  82. initLoginSessionKey();
  83. // Start listening to HTTP Requests
  84. initWebServer();
  85. }
  86. void loop(){
  87. MDNS.update();
  88. timeClient.update();
  89. }