I have a sketch that logs a small amount of data (timestamp) to a SPIFFS file on an ESP8266
Wemos D1 mini. Currently I can retrieve the data by uploading a "read SPIFFS file" sketch into the processor and looking at it through the serial monitor.
For ease of access I would like to scan the SPIFF file data either from something like: Terminal Mode in Linux, command prompt on Windows, or a browser. Whatever is quick and simple to
accomplish. I do not need to modify the file contents just read it.
So if someone can point me in the right direction I would appreciate it.
Are you supposed to create a filename assignment at the top to specify
what is being sought? Otherwise I do not see where the file is specified.
This screenshot shows the serial monitor output with my file listed:
But when I go to the browser and enter the address specified it returns:
Error: File not found
URI: /
Method: GET
Arguments: 0
path=
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <FS.h> // Include the SPIFFS library
ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
String getContentType(String filename); // convert the file extension to the MIME type
bool handleFileRead(String path); // send the right file to the client (if it exists)
void setup() {
Serial.begin(115200); // Start the Serial communication to send messages to the computer
delay(10);
Serial.println('\n');
wifiMulti.addAP("ssid_from_AP_1", "your_password_for_AP_1"); // add Wi-Fi networks you want to connect to
wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
Serial.println("Connecting ...");
int i = 0;
while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(250);
Serial.print('.');
}
Serial.println('\n');
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
if (MDNS.begin("esp8266")) { // Start the mDNS responder for esp8266.local
Serial.println("mDNS responder started");
} else {
Serial.println("Error setting up MDNS responder!");
}
SPIFFS.begin(); // Start the SPI Flash Files System
server.onNotFound([]() { // If the client requests any URI
if (!handleFileRead(server.uri())) // send it if it exists
server.send(404, "text/plain", "404: Not Found"); // otherwise, respond with a 404 (Not Found) error
});
server.begin(); // Actually start the server
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
}
String getContentType(String filename) { // convert the file extension to the MIME type
if (filename.endsWith(".html")) return "text/html";
else if (filename.endsWith(".css")) return "text/css";
else if (filename.endsWith(".js")) return "application/javascript";
else if (filename.endsWith(".ico")) return "image/x-icon";
return "text/plain";
}
bool handleFileRead(String path) { // send the right file to the client (if it exists)
Serial.println("handleFileRead: " + path);
if (path.endsWith("/")) path += "index.html"; // If a folder is requested, send the index file
String contentType = getContentType(path); // Get the MIME type
if (SPIFFS.exists(path)) { // If the file exists
File file = SPIFFS.open(path, "r"); // Open it
size_t sent = server.streamFile(file, contentType); // And send it to the client
file.close(); // Then close the file again
return true;
}
Serial.println("\tFile Not Found");
return false; // If the file doesn't exist, return false
}
After unsuccessfully trying to meld the lengthy FSBrowser.ino file with my existing sketch I am going to consider your suggestion. It appears one will need to add the "LittleFS.h" library link to do this. Is there anything beyond that in switching from FS.h to LittleFS.h code wise?
thanks....
If you run this abbreviated version of simple_server, does it compile with no errors and no warnings? If so, what does your serial monitor show when it runs?
This is really weird. While you were posting I actually just ran this file you suggested!!!
And I got a "Hello World" on the browser page! I had found the code on a tutorial looking for leads on this issue. So that part works!!
I thought I remembered reading that somewhere that there was a difference between the two.
I have a "Hello World" being displayed on the browser page now so I am getting closer hopefully.
for what it worth i use this as a sort of template for my esp8266 projects
with asyncserver , its cobbled together from various web examples
in no particular style raw_async_server.ino (9.7 KB)
you can upload download get a dir. and host html scripts from index.html to anything else
you desire