ESP32. Access SPIFFS files in FTP with the ESP8266FtpServer library

esp32 ftp arduino spiffs esp8266ftpserver library
Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
Share on whatsapp
drapeau france

There are several libraries allowing access to the SPIFFS file system stored on the flash memory of an ESP32 in FTP. The ESP8266FtpServer.h library developed by David Paiva (nailbuster on GitHub) very easy to use, allows you to add a small FTP server to the Arduino code of the ESP32 project. It also works on ESP8266. It can be used to retrieve data files or remotely update the HTML interface files, the data file of a data acquisition.

 

The SPIFFS file system for ESP32 is used to store files on the flash memory module. We can separate the code of the HTML interface (HTML file, CSS style sheet, javascript code) as well as data or parameter files. We will use the SPIFFS.h library to access, create or modify the files from the Arduino code of the project.

Limitations of the SPIFFS File System

Before you begin, remember that the SPIFFS file system is very limited.

  • There is no tree management. Files are stored flat in the memory area
  • It is possible to use the / character in the file name. We can create a pseudo tree structure
  • The file name is limited to 31 useful characters including the file extension (4 characters)
  • Avoid using SPIFFS beyond 128MB of storage
  • Writing time is very variable

Add the ESP8266FtpServer.h library to an ESP8266 project

The ESP8266FtpServer.h library is still under development. It is an adaptation of the Arduino library from Gallegojm. It can do us a lot of service in two situations.

It is not available from the library manager of the Arduino IDE. It will therefore be necessary to manually recover the files and add them to the ESP32 project.

Go to the project’s GitHub page here then download the project code

Ajouter la librairie ESP8266FtpServer à un projet ESP8266

Unzip the ZIP archive then move the ESP8266FtpServer.cpp and ESP8266FtpServer.h files at the root of the project at the same level as the ino file of the project.

esp32 arduino esp8266ftpserver librarie installation

Example of an FTP file server on ESP32

Create a new sketch on the Arduino IDE and paste the following code.

Modify the following parameters:

  • WiFi username and password
  • Username and password of the FTP server
#ifdef ESP8266
  #include <ESP8266WiFi.h> 
#elif defined ESP32
  #include <WiFi.h>
  #include "SPIFFS.h"
#endif
#include "ESP8266FtpServer.h"

const char* ssid = "enter_your_ssid";
const char* password = "enter_your_password";
const char* ftp_user = "esp32";
const char* ftp_password = "esp32";

FtpServer ftpSrv;  
void setup()
{
  Serial.begin(112500);

  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  Serial.println(F("Inizializing FS..."));
  // FTP Setup, ensure SPIFFS is started before try to use ftp methods  
#ifdef ESP32       
  if (SPIFFS.begin(true)) {
#elif defined ESP8266
  if (SPIFFS.begin()) {
#endif
    Serial.println("SPIFFS opened!");
    ftpSrv.begin(ftp_user, ftp_password);   //username, password for ftp
  }
}

void loop() {
  ftpSrv.handleFTP();
}

How Arduino C++ Code Works

As the library files are at the same level as the ino file, we declare the ESP8266FtpServer.h library between quotes to indicate to the compiler that we must use the local files.

#include "ESP8266FtpServer.h"

We then create the C++ object that will start the FTP server and manage external requests

FtpServer ftpSrv;

When starting the ESP8266, we start the FTP server using the begin() method in the setup(). The begin method of the FtpServer object takes two parameters which allow you to define the login credentials for the FTP server(user, password).

if (SPIFFS.begin()) {
   Serial.println("SPIFFS opened!");
   ftpSrv.begin(ftp_user, ftp_password); // username, password for ftp. Set ports in ESP8266FtpServer.h (default 21, 50009 for PASV)
}

Finally, the requests to connect to the server handleFTP function are monitored in the loop().

void loop(void){
  ftpSrv.handleFTP();
}

Other parameters can be configured directly in the library

  • Define FTP_DEBUG uncomment to enable sending debug messages on the serial monitor (convenient for debugging)
  • FTP_CTRL_PORT port of the FTP server. By default, this is the 21
  • FTP_TIME_OUT time before automatic disconnection (I have a doubt on its functioning)

It’s finished, you can now access your files in FTP.

Accessing SPIFFS using Filezilla

In the current version, only the passive mode is supported. SFTP (Secure FTP) is not supported.

Start FileZilla. The easiest way to connect to an ESP8266 in FTP and use the site manager. I encountered difficulties using the Quick Connect bar. Open the Site Manager from the File menu and configure the access as follows:

  • Host IP address of the ESP8266
  • Port 21 (or the one configured by the FTP_CTRL_PORT key)
  • Protocol TFP – File Transfer Protocol
  • Encryption Simple FTP connection (not secure)
  • Authentication type Normal
  • Username and Password

filezila ftp configuration esp32

Start the connection by clicking on Connection. The connection is now saved by FileZilla, it will not be necessary to do it again.

As the connection is not secure, you must accept the warning before you can connect to the ESP32.

esp32 ftp spiffs filezilla autoriser acces non securise

Once the connection is established, you have access to all the files in the Data directory of the Arduino project.

esp32 ftp filezilla spiffs

SPIFFS and LittleFS files can also be accessed by FTP on ESP8266.

Updates

09/04/2020 First publication of the tutorial

Click to rate this post!
[Total: 0 Average: 0]

Are you having a problem with this topic?

Maybe someone has already found the solution, visit the forum before asking your question
Ask your question

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
Share on whatsapp

Did you like this project ? Don't miss any more projects by subscribing to our weekly newsletter!

We will be happy to hear your thoughts

      Leave a Reply

      DIY Projects