Send data to FTP server and append values to a log file

Hi

I have some issues appending data to a file on a ftp server, server is not on my ESP bord, it is a online ftp server.

The data I want to append to file data.txt is 'load' and 'temp'

The code creates the file on server, but it dont append data to it, it stays 0 byte.
image

Appreciate any help!

M

Code:

/**
   This file modifies SurferTim's FTP code (http://playground.arduino.cc/Code/FTP)
   
*/

#include <WiFi.h>
#include <FS.h>
#include "SPIFFS.h"
#define FTPWRITE

float load = 25.3;  // kg from a scale
float temp = 22.1;

const char* ssid = "a";
const char* wifipass = "aaaaaaaa";

  //FTP stuff
  const char* host     = "ftp.server.com";
  const char* userName = "server@server.com";
  const char* password = "FTPpass";


//File Operation
char fileName[13] = "data.txt";

//############################################
//DO NOT CHANGE THESE
char outBuf[128];
char outCount;
WiFiClient client;
WiFiClient dclient;

//###########################################
//Move into setup()
void setup() {
  // Serial setup
  Serial.begin(57600);
  delay(100);

  //Wifi Setup
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  //WiFi.begin(ssid);
  WiFi.begin(ssid, wifipass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  //SPIFF setup
  SPIFFS.begin();
}

//#########################################
//Move into loop()
/*
  In the current state, this loop will append
  'Success!!' to the end of a filename then
  upload it to your FTP server
*/

void loop() {
  // Open the file on the SPIFFS file system in "append" mode
  File f = SPIFFS.open(fileName, "a");
  if (!f) {
    Serial.println("File open failed");
  } else {
    f.println(load);         // Append the content of the 'load' variable to the file
    f.print(",");            // make comma separated content to file
    f.println(temp);         // append temperature to file
    f.close();
    Serial.println("Data appended to file");
  }
  if (doFTP()) {
    Serial.println("FTP OK");
  } else {
    Serial.println("FTP FAIL");
  }
  delay(10000);
}

//#######################################
/*
   This is the function where the magic happens.
   all credit goes to SurferTim. This will open
   and write the file to your FTP server
*/
byte doFTP()
{
  File fh = SPIFFS.open(fileName, "r");
  if (!fh) {
    Serial.println("file open failed");
  }
  if (client.connect(host, 21)) {
    Serial.println(F("Command connected"));
  }
  else {
    fh.close();
    Serial.println(F("Command connection failed"));
    return 0;
  }

  if (!eRcv()) return 0;

  client.print("USER ");
  client.println(userName);

  if (!eRcv()) return 0;

  client.print("PASS ");
  client.println(password);

  if (!eRcv()) return 0;

  client.println("SYST");

  if (!eRcv()) return 0;

  client.println("Type I");

  if (!eRcv()) return 0;

  client.println("PASV");

  if (!eRcv()) return 0;

  char *tStr = strtok(outBuf, "(,");
  int array_pasv[6];
  for ( int i = 0; i < 6; i++) {
    tStr = strtok(NULL, "(,");
    array_pasv[i] = atoi(tStr);
    if (tStr == NULL)
    {
      Serial.println(F("Bad PASV Answer"));

    }
  }

  unsigned int hiPort, loPort;
  hiPort = array_pasv[4] << 8;
  loPort = array_pasv[5] & 255;
  Serial.print(F("Data port: "));
  hiPort = hiPort | loPort;
  Serial.println(hiPort);
  if (dclient.connect(host, hiPort)) {
    Serial.println("Data connected");
  }
  else {
    Serial.println("Data connection failed");
    client.stop();
    fh.close();
  }

  client.print("STOR ");
  client.println(fileName);
  if (!eRcv())
  {
    dclient.stop();
    return 0;
  }
  Serial.println(F("Writing"));

  byte clientBuf[64];
  int clientCount = 0;

  while (fh.available())
  {
    clientBuf[clientCount] = fh.read();
    clientCount++;

    if (clientCount > 63)
    {
      dclient.write((const uint8_t *)clientBuf, 64);
      clientCount = 0;
    }
  }
  if (clientCount > 0) dclient.write((const uint8_t *)clientBuf, clientCount);

  dclient.stop();
  Serial.println(F("Data disconnected"));
  client.println();
  if (!eRcv()) return 0;

  client.println("QUIT");

  if (!eRcv()) return 0;

  client.stop();
  Serial.println(F("Command disconnected"));

  fh.close();
  Serial.println(F("File closed"));
  return 1;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;

  while (!client.available()) delay(1);

  respCode = client.peek();

  outCount = 0;

  while (client.available())
  {
    thisByte = client.read();
    Serial.write(thisByte);

    if (outCount < 127)
    {
      outBuf[outCount] = thisByte;
      outCount++;
      outBuf[outCount] = 0;
    }
  }

  if (respCode >= '4')
  {
    efail();
    return 0;
  }

  return 1;
}


void efail()
{
  byte thisByte = 0;

  client.println(F("QUIT"));

  while (!client.available()) delay(1);

  while (client.available())
  {
    thisByte = client.read();
    Serial.write(thisByte);
  }

  client.stop();
  Serial.println(F("Command disconnected"));
}

You don't append, you store a file on the server. So you should ensure the file doesn't exist if you execute this sketch.

If that doesn't help, post the complete serial output you get (no pictures!)

looks like you are doing your own ftp client..
have you tried any libs available??

FTPClient_Generic

looks nice..

~q

I have tried a lot of stuff, always something not working...
The code I have presented is 'almost' working, but file is empty...

I know that ask for complete code here is waste of time. But if somebody could "give" me a 'FTP store as csv' I would jump sky high!

M

Did you read my post #2? Did you understand it?