Problem for FTP server, time limit for FTP server operated .

Hi, Can you help me with my code? I attach a copy below.
I need a code time limit for FTP server operated. If time out , thread FTP will reset and arduino will back to read sensors and save data in SD Card.

#include<Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
#include <SPI.h>
#include <SD.h>
#include <ESP8266WiFi.h>
#include "RTClib.h"

RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};


// FTP SERVER
#define FTP_PORT         54218
#define STASSID          "Marketing"
#define STAPSK           "87654321"
#define FTP_USER         "Dika AM"
#define FTP_PASS         "100894"
#define FTPWRITE
const char* ssid         = STASSID;
const char* password     = STAPSK;

const char* server = "192.168.50.100" ;

WiFiClient client;
WiFiClient dclient;

char outBuf[128];
char outCount;



// SD Card
File dataku;
char filename[] = "0000000000.txt";
const int CS =D8;

// Sensor Compass and Angle Sensor
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);


void setup()
{
  Serial.begin(115200);
  // Initializing SD Card....................
  while (!Serial) 
  {
     ; 
  }

   Serial.print("Initializing SD card…");
   if (!SD.begin(CS)) {
     Serial.println("initialization failed!");
     while (1);
   }
  Serial.println("initialization done.");
  
  // Connected WiFi
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() !=WL_CONNECTED)
  {
    delay(500);
  }
  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  
  // Initializing Sensors
  
  Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
  
  if(!mag.begin())
  {
    Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
    while(1);
  }
 
  sensor_t sensor;
  mag.getSensor(&sensor);
  
 #ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
  #endif

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }
  if (rtc.lostPower()) {
   Serial.println("RTC lost power, let's set the time!");
   rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}
  
void loop()
{
  for (int i = 1; i<1000; i++)
  {
    sensors();
  }
  
    if(doFTP()) 
    {
      Serial.println(F("FTP OK"));
      SD.remove(filename);
    }

    
    else 
    {
      Serial.println(F("FTP FAIL"));
    }
  
 } 

File fh;
byte doFTP()
{
#ifdef FTPWRITE
  fh = SD.open(filename,FILE_READ);
#else
 
  fh = SD.open(filename,FILE_WRITE);
#endif
  if(!fh)
  {
    Serial.println(F("SD open fail"));
    return 0;   
  }

#ifndef FTPWRITE 
  if(!fh.seek(0))
  {
    Serial.println(F("Rewind fail"));
    fh.close();
    return 0;   
  }
#endif
  Serial.println(F("SD opened"));
  if (client.connect(server,FTP_PORT))
  {
    Serial.println(F("FTP Connected"));
  }
  else {
    fh.close();
    Serial.println(F("FTP Connection Failed"));
    return 0;
  }

  if(!eRcv()) return 0;

  char buf[50];
  sprintf(buf,"USER %s", FTP_USER);
  client.println(buf);
  if(!eRcv()) return 0;
  sprintf(buf, "PASS %s", FTP_PASS);
  client.println(buf);
  if(!eRcv()) return 0;
  client.println(F("SYST"));
  if(!eRcv()) return 0;
  client.println(F("Type I"));
  if(!eRcv()) return 0;
  client.println(F("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(server,hiPort))
  {
    Serial.println(F("Data connected"));
  }
  else
  {
    Serial.println(F("Data connection failed"));
    client.stop();
    fh.close();
    return 0;
  }

#ifdef FTPWRITE
  client.print(F("STOR "));
  client.println(filename);
#else
  client.print(F("RETR "));
  client.println(filename);
#endif
  if(!eRcv())
  {
    dclient.stop();
    return 0;
  }
#ifdef FTPWRITE
  Serial.println(F("Writing"));

  byte clientBuf[64];
  int clientCount = 0;

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

    if(clientCount > 63)
    {
      dclient.write(clientBuf,64);
      clientCount = 0;
    }
  }
  if(clientCount > 0) dclient.write(clientBuf,clientCount);
#else
  while(dclient.connected())
  {
    while(dclient.available())
    {
      char c = dclient.read();
      fh.write(c);     
      Serial.write(c);
    }
  }
#endif

  dclient.stop();
  Serial.println(F("Data disconnected"));
  if(!eRcv()) return 0;
  client.println(F("QUIT"));
  if(!eRcv()) return 0;
  client.stop();
  Serial.println(F("Command disconnected"));
  fh.close();
  Serial.println(F("SD 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"));
  fh.close();
  Serial.println(F("SD closed"));
}

void sensors()
{
  sensors_event_t event; 
  mag.getEvent(&event);
  float heading = atan2(event.magnetic.y, event.magnetic.x);
  float declinationAngle = 0.57;
  heading += declinationAngle;
    if(heading < 0)
    heading += 2*PI;
    
    if(heading > 2*PI)
    heading -= 2*PI;
    
    int headingDegrees = heading * 180/M_PI; 
    
    char Times [120];
    char Dates [100];
    DateTime now = rtc.now();
    int Year = (now.year());
    int Month = (now.month());
    int Day = (now.day());
    //String Days = (daysOfTheWeek[now.dayOfTheWeek()]);
    int Hour = (now.hour());
    int Minute = (now.minute());
    int Second = (now.second());
    sprintf(Dates, "%li/%li/%li", Year,Month, Day);
    Serial.print(Dates);
    Serial.print(", ");
    sprintf(Times, " %li:%li:%li", Hour,Minute,Second);
    Serial.print(Times);
    Serial.print(", ");
  Serial.print("Heading: ");
  Serial.print(headingDegrees);
  delay(250);
  GetFileName ();
  dataku = SD.open(filename, FILE_WRITE);
  if (dataku)
  {
    //Data = (days[timeClient.getDay()] + "," + timeClient.getHours() + ":" + timeClient.getMinutes() + ":" + timeClient.getSeconds + "," + "Heaading" + headingDegrees
    dataku.print(Dates);
    dataku.print(", ");
    dataku.print(Times);
    dataku.print(", ");
    dataku.print(headingDegrees);
    dataku.println("");
    dataku.close();
    Serial.println(" | Data Tersimpan");
 }
 else
 {
    Serial.println(" | Gagal Tersimpan!");
 }
}

void GetFileName ()
{
  DateTime now = rtc.now();
  filename[0] = (now.year()/1000)%10 + '0'; //To get 1st digit from year()
  filename[1] = (now.year()/100)%10 + '0'; //To get 2nd digit from year()
  filename[2] = (now.year()/10)%10 + '0'; //To get 3rd digit from year()
  filename[3] = now.year()%10 + '0'; //To get 4th digit from year()
  filename[4] = now.month()/10 + '0'; //To get 1st digit from month()
  filename[5] = now.month()%10 + '0'; //To get 2nd digit from month()
  filename[6] = now.day()/10 + '0'; //To get 1st digit from day()
  filename[7] = now.day()%10 + '0'; //To get 2nd digit from day()
  filename[8] = now.hour()/10 + '0';
  filename[9] = now.hour()%10+ '0';
  Serial.print (", ");
  Serial.println(filename);
}

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