internal.ino 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. WiFi Setup Functions
  3. To setup WiFi on your Web-stick
  4. put a file in your SD card with filename /cfg/wifi.txt
  5. In the text file, write two lines which containing the
  6. WiFi ssid and password, seperated by a linux new linx \n
  7. as follows:
  8. [WIFI SSID]
  9. [WiFi Password]
  10. */
  11. String loadWiFiInfoFromSD() {
  12. if (SD.exists("/cfg/wifi.txt")) {
  13. String fileContent = "";
  14. File configFile = SD.open("/cfg/wifi.txt", FILE_READ);
  15. if (configFile) {
  16. while (configFile.available()) {
  17. fileContent += char(configFile.read());
  18. }
  19. configFile.close();
  20. }
  21. return fileContent;
  22. }
  23. return "";
  24. }
  25. void splitWiFiFileContent(const String& fileContent, String& ssid, String& password) {
  26. int newlineIndex = fileContent.indexOf('\n');
  27. if (newlineIndex != -1) {
  28. ssid = fileContent.substring(0, newlineIndex);
  29. password = fileContent.substring(newlineIndex + 1);
  30. }
  31. }
  32. void initWiFiConn() {
  33. String wifiFileContent = loadWiFiInfoFromSD();
  34. if (wifiFileContent.equals("")) {
  35. while (1) {
  36. Serial.println("WiFi info is not set. Please create a file in the SD card named /cfg/wifi.txt with first line as WiFi SSID and 2nd line as Password");
  37. delay(3000);
  38. }
  39. }
  40. // Split the contents into SSID and password
  41. String ssid, password;
  42. splitWiFiFileContent(wifiFileContent, ssid, password);
  43. ssid.trim();
  44. password.trim();
  45. WiFi.begin(ssid, password);
  46. Serial.print("Connecting to WiFi...");
  47. while (WiFi.status() != WL_CONNECTED) {
  48. Serial.print(".");
  49. delay(1000);
  50. }
  51. Serial.println(".");
  52. Serial.println("WiFi Connected: ");
  53. Serial.println("\n\nNetwork Configuration:");
  54. Serial.println("----------------------");
  55. Serial.print(" SSID: "); Serial.println(WiFi.SSID());
  56. Serial.print(" Wifi Status: "); Serial.println(WiFi.status());
  57. Serial.print("Wifi Strength: "); Serial.print(WiFi.RSSI()); Serial.println(" dBm");
  58. Serial.print(" MAC: "); Serial.println(WiFi.macAddress());
  59. Serial.print(" IP: "); Serial.println(WiFi.localIP());
  60. Serial.print(" Subnet: "); Serial.println(WiFi.subnetMask());
  61. Serial.print(" Gateway: "); Serial.println(WiFi.gatewayIP());
  62. Serial.print(" DNS 1: "); Serial.println(WiFi.dnsIP(0));
  63. Serial.print(" DNS 2: "); Serial.println(WiFi.dnsIP(1));
  64. Serial.print(" DNS 3: "); Serial.println(WiFi.dnsIP(2));
  65. Serial.println("----------------------");
  66. Serial.println();
  67. }
  68. /*
  69. Admin Credential Setup
  70. For management portal functions, you will
  71. need to setup the admin account in order to
  72. use it. Place a text file at cfg/admin.txt
  73. with the following information
  74. [admin_username]
  75. [admin_password]
  76. Currently only 1 admin user is supported
  77. */
  78. String loadAdminCredFromSD() {
  79. if (SD.exists("/cfg/admin.txt")) {
  80. String fileContent = "";
  81. File configFile = SD.open("/cfg/admin.txt", FILE_READ);
  82. if (configFile) {
  83. while (configFile.available()) {
  84. fileContent += char(configFile.read());
  85. }
  86. configFile.close();
  87. }
  88. return fileContent;
  89. }
  90. return "";
  91. }
  92. void splitAdminCreds(const String& fileContent, String& username, String& password) {
  93. int newlineIndex = fileContent.indexOf('\n');
  94. if (newlineIndex != -1) {
  95. username = fileContent.substring(0, newlineIndex);
  96. password = fileContent.substring(newlineIndex + 1);
  97. }
  98. }
  99. void initAdminCredentials() {
  100. String adminCredentials = loadAdminCredFromSD();
  101. if (adminCredentials.equals("")) {
  102. //Disable authentications on API calls
  103. return;
  104. }
  105. // Split the contents into username and password
  106. splitWiFiFileContent(adminCredentials, adminUsername, adminPassword);
  107. adminUsername.trim();
  108. adminPassword.trim();
  109. Serial.println("Admin user loaded: " + adminUsername);
  110. }
  111. void initmDNSName() {
  112. if (SD.exists("/cfg/mdns.txt")) {
  113. String fileContent = "";
  114. File configFile = SD.open("/cfg/mdns.txt", FILE_READ);
  115. if (configFile) {
  116. while (configFile.available()) {
  117. fileContent += char(configFile.read());
  118. }
  119. configFile.close();
  120. }
  121. fileContent.trim();
  122. mdnsName = fileContent;
  123. }
  124. }
  125. //Load the previous login session key from database
  126. //for resuming login session after poweroff
  127. void initLoginSessionKey() {
  128. String serverCookie = DBRead("auth", "cookie");
  129. if (serverCookie != "") {
  130. authSession = serverCookie;
  131. }
  132. }
  133. /*
  134. MIME Utils
  135. */
  136. String getMime(const String& path) {
  137. String _contentType = "text/plain";
  138. if (path.endsWith(".html")) _contentType = "text/html";
  139. else if (path.endsWith(".htm")) _contentType = "text/html";
  140. else if (path.endsWith(".css")) _contentType = "text/css";
  141. else if (path.endsWith(".json")) _contentType = "text/json";
  142. else if (path.endsWith(".js")) _contentType = "application/javascript";
  143. else if (path.endsWith(".png")) _contentType = "image/png";
  144. else if (path.endsWith(".gif")) _contentType = "image/gif";
  145. else if (path.endsWith(".jpg")) _contentType = "image/jpeg";
  146. else if (path.endsWith(".ico")) _contentType = "image/x-icon";
  147. else if (path.endsWith(".svg")) _contentType = "image/svg+xml";
  148. else if (path.endsWith(".eot")) _contentType = "font/eot";
  149. else if (path.endsWith(".woff")) _contentType = "font/woff";
  150. else if (path.endsWith(".woff2")) _contentType = "font/woff2";
  151. else if (path.endsWith(".ttf")) _contentType = "font/ttf";
  152. else if (path.endsWith(".xml")) _contentType = "text/xml";
  153. else if (path.endsWith(".pdf")) _contentType = "application/pdf";
  154. else if (path.endsWith(".zip")) _contentType = "application/zip";
  155. else if (path.endsWith(".gz")) _contentType = "application/x-gzip";
  156. else if (path.endsWith(".mp3")) _contentType = "audio/mpeg";
  157. else if (path.endsWith(".mp4")) _contentType = "video/mp4";
  158. else if (path.endsWith(".aac")) _contentType = "audio/aac";
  159. else if (path.endsWith(".ogg")) _contentType = "audio/ogg";
  160. else if (path.endsWith(".wav")) _contentType = "audio/wav";
  161. else if (path.endsWith(".m4v")) _contentType = "video/x-m4v";
  162. else if (path.endsWith(".webm")) _contentType = "video/webm";
  163. else _contentType = "text/plain";
  164. return _contentType;
  165. }
  166. /*
  167. Get ESP Info
  168. */
  169. void printESPInfo() {
  170. Serial.println(ESP.getBootMode());
  171. Serial.print("ESP.getSdkVersion(); ");
  172. Serial.println(ESP.getSdkVersion());
  173. Serial.print("ESP.getBootVersion(); ");
  174. Serial.println(ESP.getBootVersion());
  175. Serial.print("ESP.getChipId(); ");
  176. Serial.println(ESP.getChipId());
  177. Serial.print("ESP.getFlashChipSize(); ");
  178. Serial.println(ESP.getFlashChipSize());
  179. Serial.print("ESP.getFlashChipRealSize(); ");
  180. Serial.println(ESP.getFlashChipRealSize());
  181. Serial.print("ESP.getFlashChipSizeByChipId(); ");
  182. Serial.println(ESP.getFlashChipSizeByChipId());
  183. Serial.print("ESP.getFlashChipId(); ");
  184. Serial.println(ESP.getFlashChipId());
  185. }
  186. /*
  187. Get SD card info
  188. */
  189. uint32_t getTotalUsedSpace(const String& directory) {
  190. uint32_t totalUsedSpace = 0;
  191. File root = SD.open(directory);
  192. if (root) {
  193. while (true) {
  194. File entry = root.openNextFile();
  195. if (!entry) {
  196. // No more files
  197. break;
  198. }
  199. if (entry.isDirectory()) {
  200. // Recursive call for subdirectory
  201. totalUsedSpace += getTotalUsedSpace(directory + "/" + entry.name());
  202. } else {
  203. //Serial.print(entry.name());
  204. //Serial.print(" | Size: ");
  205. //Serial.println(humanReadableSize(entry.size()));
  206. totalUsedSpace += entry.size();
  207. }
  208. entry.close();
  209. }
  210. root.close();
  211. }
  212. return totalUsedSpace;
  213. }
  214. //Return the total used space on SD card
  215. long getSDCardUsedSpace() {
  216. uint32_t totalUsedSpace = getTotalUsedSpace("/");
  217. Serial.println("Total used space: " + humanReadableSize(totalUsedSpace));
  218. return totalUsedSpace;
  219. }
  220. //Get the total SD card size
  221. long getSDCardTotalSpace() {
  222. Serial.println("SD card size: " + humanReadableSize(SD.size64()));
  223. return SD.size64();
  224. }
  225. String humanReadableSize(const int bytes) {
  226. if (bytes < 1024) return String(bytes) + " B";
  227. else if (bytes < (1024 * 1024)) return String(bytes / 1024.0) + " KB";
  228. else if (bytes < (1024 * 1024 * 1024)) return String(bytes / 1024.0 / 1024.0) + " MB";
  229. else return String(bytes / 1024.0 / 1024.0 / 1024.0) + " GB";
  230. }