Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #include <WiFiClient.h>
- #include <ESP8266WiFi.h>
- #include <FS.h>
- ESP8266WebServer server(80); //default porta 80, se diversa nel browser ip:port_number
- const char* ssid = "----------";
- const char* password = "----------";
- const String rootPath = "/index.html"; //main web page
- void handleRoot();
- String getContentType(String path);
- void handleNotFound();
- void setup() {
- delay(1000);
- //-----Debug Serial-------
- Serial.begin(115200);
- Serial.print("Connection in progress..\n");
- //-----SPIFFS begin-------
- SPIFFS.begin();
- //-----Start WiFi and connection------
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print("wait connection..\n");
- delay(500);
- }
- Serial.print("IP address on my LAN: ");
- Serial.print(WiFi.localIP());
- Serial.print("\n");
- if (MDNS.begin("esp8266")) {
- Serial.print("MDNS responder started.\n");
- }
- //-----handle address from browser-------
- server.on("/", handleRoot); //Pagina principale e gestione path non trovato
- server.onNotFound(handleNotFound);
- //-----Server begin------
- server.begin();
- delay(500);
- Serial.print("HTTP Server started!\n");
- Serial.print("--->End Setup\n\n");
- }
- void loop() {
- server.handleClient();
- }
- void handleRoot() {
- Serial.print("handleRoot() at path: " + rootPath + "\n");
- String contentType = getContentType(rootPath);
- if (SPIFFS.exists(rootPath)) {
- File file = SPIFFS.open(rootPath, "r");
- server.streamFile(file, contentType);
- file.close();
- }
- }
- String getContentType(String path) {
- if (path.endsWith(".htm")) return "text/html";
- if (path.endsWith(".html")) return "text/html";
- if (path.endsWith(".css")) return "text/css";
- if (path.endsWith(".js")) return "application/javascript";
- if (path.endsWith(".png")) return "image/png";
- if (path.endsWith(".gif")) return "image/gif";
- if (path.endsWith(".jpg")) return "image/jpeg";
- if (path.endsWith(".ico")) return "image/x-ico";
- if (path.endsWith(".xml")) return "text/xml";
- if (path.endsWith(".pdf")) return "application/x-pdf";
- if (path.endsWith(".zip")) return "application/x-zip";
- if (path.endsWith(".gz")) return "application/x-gzip";
- return "text/plain";
- }
- void handleNotFound() {
- server.send(404, "text/plain", "ERROR 404 page non found!");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement