A Minimal SPIFFS server
/* Create a WiFi access point and provide a web server on it.This program uses SPIFFS to hold files
create a website and upload to SPIFFS The root is 'index.htm'*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <FS.h>
const char *ssid = "ESP";
const char *password = ""; //no password
ESP8266WebServer server(80);
//-----------------------------------------------------------------------------------------
void setup() {
Serial.begin(115200);
SPIFFS.begin();
Serial.println();
Serial.print("Configuring access point... ");
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("Access Point IP address: ");
Serial.println(myIP);
server.onNotFound(handleOther);
server.begin();
Serial.println("HTTP server started");
}
//-----------------------------------------------------------------------------------------
// the clever bit
bool loadFromSpiffs(String path)
{
String dataType = "text/plain";
if (path.endsWith("/")) path += "index.htm"; //this is where index.htm is created
if (path.endsWith(".src")) path = path.substring(0, path.lastIndexOf("."));
else if (path.endsWith(".htm")) dataType = "text/html";
else if (path.endsWith(".css")) dataType = "text/css";
else if (path.endsWith(".js")) dataType = "application/javascript";
else if (path.endsWith(".png")) dataType = "image/png";
else if (path.endsWith(".gif")) dataType = "image/gif";
else if (path.endsWith(".jpg")) dataType = "image/jpeg";
else if (path.endsWith(".ico")) dataType = "image/x-icon";
else if (path.endsWith(".xml")) dataType = "text/xml";
else if (path.endsWith(".pdf")) dataType = "application/pdf";
else if (path.endsWith(".zip")) dataType = "application/zip";
File dataFile = SPIFFS.open(path.c_str(), "r"); //open file to read
if (!dataFile) //unsuccesful open?
{
Serial.print("Don't know this as a command and it's not a file in SPIFFS : ");
Serial.println(path);
return false;
}
if (server.streamFile(dataFile, dataType) != dataFile.size())
{} //a lot happening here...
dataFile.close();
return true;
}
//-----------------------------------------------------------------------------------------
void handleOther()
{
Serial.println(server.uri()); // let's see what we are asked for
if (loadFromSpiffs(server.uri()))
return; //gotcha - it's a file in SPIFFS
String message = "Not Found\n\n"; //or not...
message += "URI: "; //make a 404 response
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++)
{
message += " NAME:" + server.argName(i) + "\n VALUE:" + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
Serial.println(message);
}
//-----------------------------------------------------------------------------------------
void loop() {
server.handleClient();
}
//-----------------------------------------------------------------------------------------