Advertisement
KenFalco

ESP8266-SPIFFS-Web-server

Aug 17th, 2019
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <ESP8266WebServer.h>
  2. #include <ESP8266mDNS.h>
  3. #include <WiFiClient.h>
  4. #include <ESP8266WiFi.h>
  5. #include <FS.h>
  6.  
  7. ESP8266WebServer server(80); //default porta 80, se diversa nel browser ip:port_number
  8.  
  9. const char* ssid = "----------";
  10. const char* password = "----------";
  11.  
  12. const String rootPath = "/index.html"; //main web page
  13.  
  14. void handleRoot();
  15. String getContentType(String path);
  16. void handleNotFound();
  17.  
  18. void setup() {
  19.   delay(1000);
  20.   //-----Debug Serial-------
  21.   Serial.begin(115200);
  22.   Serial.print("Connection in progress..\n");
  23.  
  24.   //-----SPIFFS begin-------
  25.   SPIFFS.begin();
  26.  
  27.   //-----Start WiFi and connection------
  28.   WiFi.begin(ssid, password);
  29.  
  30.   while (WiFi.status() != WL_CONNECTED) {
  31.     Serial.print("wait connection..\n");
  32.     delay(500);
  33.   }
  34.  
  35.   Serial.print("IP address on my LAN: ");
  36.   Serial.print(WiFi.localIP());
  37.   Serial.print("\n");
  38.  
  39.   if (MDNS.begin("esp8266")) {
  40.     Serial.print("MDNS responder started.\n");
  41.   }
  42.  
  43.   //-----handle address from browser-------
  44.  
  45.   server.on("/", handleRoot); //Pagina principale e gestione path non trovato
  46.   server.onNotFound(handleNotFound);
  47.  
  48.   //-----Server begin------
  49.   server.begin();
  50.   delay(500);
  51.   Serial.print("HTTP Server started!\n");
  52.   Serial.print("--->End Setup\n\n");
  53. }
  54.  
  55. void loop() {
  56.   server.handleClient();
  57. }
  58.  
  59. void handleRoot() {
  60.   Serial.print("handleRoot() at path: " + rootPath + "\n");
  61.   String contentType = getContentType(rootPath);
  62.   if (SPIFFS.exists(rootPath)) {
  63.     File file = SPIFFS.open(rootPath, "r");
  64.     server.streamFile(file, contentType);
  65.     file.close();
  66.   }
  67. }
  68.  
  69. String getContentType(String path) {
  70.   if (path.endsWith(".htm")) return "text/html";
  71.   if (path.endsWith(".html")) return "text/html";
  72.   if (path.endsWith(".css")) return "text/css";
  73.   if (path.endsWith(".js")) return "application/javascript";
  74.   if (path.endsWith(".png")) return "image/png";
  75.   if (path.endsWith(".gif")) return "image/gif";
  76.   if (path.endsWith(".jpg")) return "image/jpeg";
  77.   if (path.endsWith(".ico")) return "image/x-ico";
  78.   if (path.endsWith(".xml")) return "text/xml";
  79.   if (path.endsWith(".pdf")) return "application/x-pdf";
  80.   if (path.endsWith(".zip")) return "application/x-zip";
  81.   if (path.endsWith(".gz")) return "application/x-gzip";
  82.   return "text/plain";
  83. }
  84.  
  85. void handleNotFound() {
  86.   server.send(404, "text/plain", "ERROR 404 page non found!");
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement