SPIFFS file data retrieval keeping it simple .... Terminal Mode, command prompt, or browser ? - Solved

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.

chapters 9 and 10
https://tttapa.github.io/ESP8266/Chap01%20-%20ESP8266.html

ps to be future proof , forget spiffs use littlefs

1 Like

Great link!!! Thanks.

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
}

edit - added screenshot

assuming the file exists in your partition , and is render-able by your browser
wino
i get this for a csv file called log.csv

1 Like

If you use ESPAsyncWebServer it's as simple as adding...

  server.on("/showMe", HTTP_GET, [](AsyncWebServerRequest * request) {
    request->send(LittleFS, "/myFile.txt", "text/plain");
  });

...to your code.

Then when you point your browser to...

192.168.1.abc/showMe

...it shows you your file in your browser.

Here's an example from one of my projects:

image

image

Aha!!! Are you saying you add a forward slash and the filename after the processor address in the browser line?

That could be the issue. I need to try this. :+1:

edit -
Update -

That was indeed the issue!!! :+1:

Your link led me to FSBrowser and that is what ended up being the simple solution.
Thank you very much!!!

Dave,

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....

Yes, you would need to change your code to use the Async web server library. There are plenty of tutorials out there on how to use that library.

PS: you can use SPIFFS with the Async, but as previously mentioned, LittleFS is the way to be "future proof."

I just finished installing it and encountered a:

This 192.168.42.34 page can’t be found
No webpage was found for the web address: http://192.168.42.34/showMe
HTTP ERROR 404

If I type just the address I get a "Not found"

I included LittleFS.h when using the SimpleServer.ino sketch.

Any ideas?

The FSBrowser ino file worked properly but all that code is a bear to try and incorporate into my sketch.

I double verified the addressing on the serial monitor.

I tried using SPIFFS [FS.h] but the compiler generated an error when substituting "FS" for "LittleFS"
in the line:

server.on("/showMe", HTTP_GET, [](AsyncWebServerRequest * request) {
    request->send(LittleFS, "/myFile.txt", "text/plain");
  });

The processor file was generated with a SPIFFS write command Would that cause a problem when trying to read it with LittleFS?

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?

#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#endif
#include <ESPAsyncWebServer.h>

AsyncWebServer server(80);

const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

const char* PARAM_MESSAGE = "message";

void notFound(AsyncWebServerRequest *request) {
    request->send(404, "text/plain", "Not found");
}

void setup() {

    Serial.begin(115200);
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    if (WiFi.waitForConnectResult() != WL_CONNECTED) {
        Serial.printf("WiFi Failed!\n");
        return;
    }

    Serial.print("My IP Address is: ");
    Serial.println(WiFi.localIP());

    server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
        request->send(200, "text/plain", "Hello, world");
    });

    server.onNotFound(notFound);

    server.begin();
}

void loop() {
}

yes

if you change from spiffs to littlefs the partition will most likely be reformatted
and the file will need to be reloaded

Dave,

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!!

#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#endif
#include <ESPAsyncWebSrv.h>
#include <LittleFS.h>
#include <FS.h>
 
const char* ssid = "hug2g842673_EXT";
const char* password =  "nurse23south";
 
AsyncWebServer server(80);
 
void setup(){
  Serial.begin(115200);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
 
  Serial.println(WiFi.localIP());
 
  server.on("/hello", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(200, "text/plain", "Hello World");
  });
 
  server.begin();
}
 
void loop(){}


The serial monitor shows:

Connecting To Wifi
Connecting To Wifi
192.168.42.34

you can check the files with this called from setup

void startFS() { // Start the FS
  LittleFS.begin();                             // Start the  Flash File System 
    Serial.println("FS started. Contents:");
    {
      Dir dir = LittleFS.openDir("/");
      while (dir.next()) {                      // List the file system contents
       String fileName = dir.fileName();
       size_t fileSize = dir.fileSize();
              Serial.printf("\tFS File: %s, size: %s\r\n", fileName.c_str(), formatBytes(fileSize).c_str());
      }
        Serial.printf("\n");
    }
}

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.

Ok, so then as @racpi said, if you want to use LittleFS, you will need to reload the data file(s) in the data subfolder below your sketch folder)...

image

...or recreate it or them in code.

Read about all this here:

https://arduino-esp8266.readthedocs.io/en/latest/filesystem.html#

Ok. Thanks for that. I need to backup and get a grasp on reloading the file data to the subfolder and go ahead and properly switch to LittleFS.

:+1:

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