SPIFFS/LittleFS with ESP8266

Hey everyone ! Im new to PlatformIO and I want a bit of guidance . I have searched the internet and read a lot of posts here aswell , about SPIFFS/LittleFS, but cant seem to manage to make my program work .
Code :

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>


AsyncWebServer server(80);

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

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

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


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


server.begin();
}

So im trying to build a web server which will take data from an Arduino UNO . I want all my web stuff ( html, css , js etc ) to be in a separate folder so I can have my project more clean .

Also, im not 100% sure that I’ve chose the right board ( I have a nodeMCU esp8266 )

PlatformIO.ini :

platform = espressif8266
board = esp12e
framework = arduino

What’s exactly not working here?

Why is there no loop() in the program?

Also the platformio.ini is missing its first [env:esp12e] line?

1 Like

The loop() is empty , im not doing anything there atm .
I made another project and I searched for * nodemcu esp8266 * and I chose NodeMCU 1.0 (ESP-12E Module) .
Now my ini. file is :

[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino

What im trying to make is to access my index.html file using SPIFFS and LittleFS ( because as I read about them , this is what they do ) . But I dont know how to do that .

In addition, you are not including or starting SPIFFS or LittleFS! Once you’ve done all that, you’ll also need to do the Upload FS thingy to load the data subfolder containing your index.html onto your device.

i.e. for SPIFFS, I #include <FS.h>, create the following object globally,

FS *filesystem = &SPIFFS;

And then somewhere before it is first used, (i.e. put it in setup()), call:

  filesystem->begin();

Also, I do not think

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

is valid, as the third parameter of request->send() should be a string.

The syntax given in the ESPAsyncWebServer documentation example is request->send(SPIFFS, "/index.htm");

I actually have subfolders in my setup - html, css, images, scripts, etc… and use the serveStatic method instead… i.e.

server.serveStatic("/", SPIFFS, "/");
server.serveStatic("/", SPIFFS, "/html/").setDefaultFile("index.html");
server.serveStatic("/images/", SPIFFS, "/images/");
server.serveStatic("/css/", SPIFFS, "/css/");
server.serveStatic("/scripts/", SPIFFS, "/scripts/");

Hope that helps

1 Like

How does this work then when you don’t have

void loop(void){
  server.handleClient();                    // Listen for HTTP requests from clients
}

See example platform-espressif8266/examples/arduino-webserver at develop · platformio/platform-espressif8266 · GitHub

It’s actually working, thanks !

What method do you recommend me to use ?
A little background of what im trying to do : an Arduino reading and sending data from * n * sensors to ESP2866 through a serial communication , and the esp sending the data further to a web server to display it .

I think I need to use that when I have more than 1 page to handle . Like the main page + a setting page or something . For now , I was just trying to make my one page working . I’ll come back to you with that later . Thanks for taking your time to read my post !!

Also , when I upload my code I get some warnings like : ‘SPIFFS’ is deprecated (declared at C:\Users\uic85816.platformio\packages\framework-arduinoespressif8266\cores\esp8266/FS.h:269): SPIFFS has been deprecated. Please consider moving to LittleFS or other filesystems. [-Wdeprecated-declarations] .

What should I change so I can use LittleFS ?

Huh? Wrong webserver… he’s using ESPAsyncWebServer :wink:

1 Like

The method I showed - as it’s the most succinct, and probably optimised for the task of returning files. But I fibbed, I use a slightly different call (I changed it for simplicity), but do the same thing - have an index.html file served from FS - and want to show ‘live’ values.

I actually use

server.serveStatic("/", SPIFFS, "/html/").setTemplateProcessor(templateProcessor).setDefaultFile("index.html");

which lets index.html be pulled from the SPIFFS FS /html/index.html whenever / or /index.html is requested. Bit I omitted is setTemplateProcessor, which lets you modify a file in fly, i.e. filling in placeholders. In this instance, I’m simply putting in the time when the text TIME appears in the index.html (and only checking for that one place-holder) - but it should give you some ideas still :wink:

String templateProcessor(const String &var)
{
  if (var == "TIME")
  {
    char uptimeStr[24];

    gettimeofday(&tv, &tz);
    tnow = time(nullptr);
    struct tm *timeinfo = localtime(&tnow);

    sprintf(uptimeStr, "%02i:%02i %02i/%02i/%04i", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_mday, timeinfo->tm_mon, (timeinfo->tm_year - 100) + 2000);

    return String(uptimeStr);
  }
  return String();
}

I’m not sure yet as I haven’t done it myself. I would not worry about it until support is officially dropped (it’s only a warning to not base new code on it at present :wink: ), probably when 3.0.0. of the ESP8266Arduino BSP releases sometime in the future.

As far as I know, it is just a matter of replacing any reference of SPIFFS to LittleFS, and, including #include <LittleFS.h> and the code should compile fine again. You also need to add board_build.filesystem = littlefs to your platformio.ini so PlatformIO knows what file sytem format to use when doing the Upload File System image task.

1 Like