Search code examples
c++ftpesp32

ESP32 FTP problems (AppendFile function)


By running the code below you are able to connect to the remote FTP server (NAS QNAP TS-251, QuFTP Service 1.3.1) and create the file by writing to it. After closing the file if you want to add text I go to use the AppendFile() function and then the Write() function and both give this error on the serial of ESP32:

FTP error: 425 Unable to build data connection: Connection refused

Code used

#include "Arduino.h"
#include <WiFi.h>
#include <WiFiClient.h> 
#include <ESP32_FTPClient.h>

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

char ftp_server[] = "";
char ftp_user[]   = "";
char ftp_pass[]   = "";

ESP32_FTPClient ftp (ftp_server,ftp_user,ftp_pass);

void setup()
{
  Serial.begin( 115200 );

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connessione Wi-Fi in corso...");
  }
  Serial.println("");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  Serial.print("\nMax Free Heap: ");
  Serial.println(ESP.getMaxAllocHeap());
  Serial.println("");

  ftp.OpenConnection();

  //Change directory
  ftp.ChangeWorkDir("/Web/W.Station/Data/");

  ftp.InitFile("Type A");
  ftp.NewFile("helloworld.txt");
  ftp.Write("Hi, I'm a new file");
  ftp.CloseFile();


  ftp.AppendFile("helloworld.txt");
  ftp.Write("\nWriting with AppendFile");
  ftp.CloseFile();
  ftp.CloseConnection();
}

Server log Server log of the ESP32 activity via FTP

What I expect What I expect is that by using the AppendFile function I can like "reopen" the file and then use the Write function to add the text to the file. I don't know if I'm getting the functions wrong, but searching the web one can find really little documentation on the subject.


Solution

  • Thanks for the reply Martin Prikryl, with the library "ESP32_FTPClient.h" it was still giving me some problems with communication even putting "InitFile" before the append. So I tried to change library and switched to a more generic one and it works great with this one.

    Working code

    #include "Arduino.h"
    #include <WiFi.h>
    #include <WiFiClient.h> 
    
    #include <FTPClient_Generic.h>
    
    const char* ssid = "";
    const char* password = "";
    
    char ftp_server[] = "";
    
    char ftp_user[]   = "";
    char ftp_pass[]   = "";
    
    char dirName[]    = "/Web/W.Station/Data/";
    char fileName[] = "helloworld.txt";
    
    FTPClient_Generic ftp (ftp_server, ftp_user, ftp_pass, 60000);
    
    void setup() {
      Serial.begin( 115200 );
    
    
    
    
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connessione Wi-Fi in corso...");
      }
      Serial.println("");
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());
    
    
    
    
    
    
      Serial.print("Max Free Heap: ");
      Serial.println(ESP.getMaxAllocHeap());
    
    
    
    
    
    
      ftp.OpenConnection();
    
      //Change directory
      ftp.ChangeWorkDir(dirName);
    
      Serial.println("Creating new file helloworld.txt");
    
      // Create a new file to use as the download example below:
      ftp.InitFile(COMMAND_XFER_TYPE_ASCII);
      ftp.NewFile(fileName);
    
      String textContent = String("Hi, I'm a new ASCII file created");
    
      ftp.Write(textContent.c_str());
      ftp.CloseFile();
    
    
    
    
    
    
      ftp.InitFile(COMMAND_XFER_TYPE_ASCII);
      ftp.AppendFile(fileName);
    
      textContent = String("\nAdded text");
    
      ftp.Write(textContent.c_str());
      ftp.CloseFile();
    
      ftp.CloseConnection();
    
    }
    
    void loop() {}