SOLVED: FTP on NodeMCU - connection issues

I've built a datalogger to monitor the connections on my local area network.

All details here if anyone is interested.

My problem is that I use FTP to download data from the Node to my PC; however after running for a while (maybe months) the FTP refuses to connect.

I've shown the relevant bits of FTP handling here, but I've also attached the full code for context. The program is a state machine driven by millis(), and the only code I can see that could block is the "PerformPing" routine - which is only active for 7 seconds in every 60.

#include <ESP8266FtpServer.h>       //ftp server library


FtpServer ftpSrv;   //set #define FTP_DEBUG in ESP8266FtpServer.h to see ftp verbose on serial



void setup()
{
 
  //monitor Wifi connection and handle D0

  gotIpEventHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP & event)
  {
    //Serial.print("Station connected, IP: ");
    //Serial.println(WiFi.localIP());
    WiFiState = 1;
    digitalWrite(dPin[0], 0); //blue LED is active LOW
  });

  disconnectedEventHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected & event)
  {
    Serial.println("Station disconnected");
    WiFiState = 0;
    digitalWrite(dPin[0], 1); //blue LED OFF
  });

  connectWiFi();


  //FTP Setup, ensure SPIFFS is started before ftp;
  if (SPIFFS.begin()) {
    Serial.println("SPIFFS opened!");
    ftpSrv.begin("esp8266", "esp8266");   //username, password for ftp.  set ports in ESP8266FtpServer.h  (default 21, 50009 for PASV)
  }

void loop() {
  ftpSrv.handleFTP();        //make sure in loop you call handleFTP()!!

PerformPing

/*   code for pinging and led display  */
bool PerformPing()
{
  if (roundComplete)  //ready for next set of pings
  {
    isFinished = false;
    int url = target;
    bool pingOK = pinger.Ping(IPAddress(host1[url].IP), pingNumber, pingTime);
    roundComplete = false;
  }
  else if (isFinished)        // one set of pings to a single target complete and results in
  {
    isFinished = false;
    //correct data: average is only correct if no pings lost; otherwise its the sum of the valid ping times
    if (pingsLost == 0) {
      host1[target].tPing = pingAvg;
    }
    else {
      host1[target].tPing = int((pingAvg + pingsLost * pingTime) / pingNumber);
    }
    host1[target].nLost = pingsLost;
    checkFailedPings();
    roundComplete = true;
  }
  return roundComplete;
}

netmonitor.ino (20.5 KB)

Well, all by myself I have found the answer while working on a new project.

void loop() {
  ftpSrv.handleFTP();        //make sure in loop you call handleFTP()!!

If the loop includes code that can take significant time the FTP fails. Presumably it just times out.
My sequence of pings must have been taking too long.

The sketch uses this library

and the example FTPServerSample.ino (code below) is VERY responsive - note the loop has no other code to slow it down.

#ifdef ESP8266
#include <ESP8266WiFi.h>
#elif defined ESP32
#include <WiFi.h>
#include "SPIFFS.h"
#endif

#include <ESP8266FtpServer.h>

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


FtpServer ftpSrv;   //set #define FTP_DEBUG in ESP8266FtpServer.h to see ftp verbose on serial


void setup(void){
  Serial.begin(115200);
  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());


  /////FTP Setup, ensure SPIFFS is started before ftp;  /////////
  
  /////FTP Setup, ensure SPIFFS is started before ftp;  /////////
#ifdef ESP32       //esp32 we send true to format spiffs if cannot mount
  if (SPIFFS.begin(true)) {
#elif defined ESP8266
  if (SPIFFS.begin()) {
#endif
      Serial.println("SPIFFS opened!");
      ftpSrv.begin("esp8266","esp8266");    //username, password for ftp.  set ports in ESP8266FtpServer.h  (default 21, 50009 for PASV)
  }    
}
void loop(void){
  ftpSrv.handleFTP();        //make sure in loop you call handleFTP()!!  
 // server.handleClient();   //example if running a webserver you still need to call .handleClient();
 
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.