Failed to upload image to server with FTP ESP8266

Hi, Im trying to upload an image to server host, with FTP

device that I'm using is WEMOS D1 R1 with esp8266

I did upload to the server but its only upload 0kb file

here is my code :

/*
   FTP passive client for IDE v1.0.1 and w5100/w5200
   Posted October 2012 by SurferTim
   Modified 6 June 2015 by SurferTim

  adapted to run on a WeMos D1  ESP8266 device
   
*/

// WiFiexample to send a single line file

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <SD.h>

File webFile;

#define MTU_Size    2*1460
#define ssid "Kaangaroo"
#define password "daffaspro123"
//#define SPRINTF       // comment out to use strcat() 



const char* server = "files.000webhost.com";

char fileName[13] = "IMAGE01.jpg";

WiFiClient client;
WiFiClient dclient;
int status = WL_IDLE_STATUS;     // the Wifi radio's status

char outBuf[128];
char outCount;

// fileName to your file (8.3 format!) and file 

char clientBuf[64]; // file to send
int clientCount;


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

   Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
      WiFi.hostname("Name");
      WiFi.begin(ssid, password);
     
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println("");
      Serial.println("WiFi connected");
     
      // Print the IP address
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());
      
  
  
  Serial.println(F("Ready. Press f"));

  
}

void loop()
{
  byte inChar;
  inChar = Serial.read();
  if(inChar == 'f')
  {
    if(doFTP()) Serial.println(F("FTP OK"));
    else Serial.println(F("FTP FAIL"));
  }
}


byte doFTP()
{
  if (client.connect(server,21)) {
    Serial.println(F("Command connected"));
  } 
  else {
     Serial.println(F("Command connection failed"));
    return 0;
  }
  // send login name and password
  if(!eRcv()) return 0;
  client.println(F("USER babythesismonitor"));
  if(!eRcv()) return 0;
  client.println(F("PASS daffa009"));
  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();
    return 0;
  }
  // setup to send file file fileName
  client.println(F("CWD /public_html"));
  client.print(F("STOR "));
  client.println(fileName);
  webFile = SD.open(fileName,FILE_READ);

  while(webFile.available()){
    if (webFile.available()>= MTU_Size){
      clientCount = MTU_Size;
      webFile.read(&clientBuf[0],clientCount);
    }
  else{
      clientCount = webFile.available();
      webFile.read(&clientBuf[0],clientCount);
    }
    
    if(clientCount > 0){
        dclient.stop();
        return 0;
      }
      // send file contents
      dclient.write((const uint8_t *)&clientBuf[0],clientCount);
      clientCount = 0;
    }
  
  
  
  
  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"));
  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"));
  Serial.println(F("SD closed"));
}


}

.
WiFi connected
IP address: 192.168.137.223
Ready. Press f
Command connected
220 ProFTPD Server (000webhost.com) [::ffff:145.14.144.87]
331 User babythesismonitor OK. Password required
230-Your bandwidth usage is restricted
230 OK. Current restricted directory is /
215 UNIX Type: L8
200 TYPE is now 8-bit binary
227 Entering Passive Mode (145,14,144,87,203,202).
Data port: 52170
Data connected
Data disconnected
250 OK. Current directory is /public_html
150 Connecting to port 50724
226 File successfully transferred
Command disconnected
FTP OK

  byte inChar;

It is NOT a good idea to use a type in a name when the type in the name is not the type of the name.

char clientBuf[64]; // file to send

That array holds the data to send, not the file to send.

char is a signed type. Your binary data is not signed. Using the proper type of array would not be a bad thing.

int clientCount;

Are you sure that you don't need a long to hold a value up to 64?

#define MTU_Size 2*1460
if (webFile.available()>= MTU_Size){
clientCount = MTU_Size;
webFile.read(&clientBuf[0],clientCount);
}
The second argument to the read() method is the size of the array. It hardly seems likely that you can fit 2920 bytes in a 64 element array.

Thank you for your reply, I updated my code based on your suggestion

#include <SPI.h>
#include <SD.h>
#include <SoftwareSerial.h>   
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266mDNS.h> 

#define chipSelect D4 //pin SDCard

#define wifiSSID "Kaangaroo"
#define wifiPassword "daffaspro123"
#define server  "files.000webhost.com"


WiFiClient client;
WiFiClient dclient;
File webFile;
int status = WL_IDLE_STATUS;     // the Wifi radio's status

char outBuf[128];
char outCount;
char fileName[13] = "IMAGE01.JPG";
#define MTU_Size    10*1460  // this size seems to work best

byte clientBuf[MTU_Size];
int clientCount = 0;

void setupWiFi(){
  //To Connect the WiFi
  WiFi.begin(wifiSSID, wifiPassword);
  Serial.print("WiFi connecting");
  while (WiFi.status() != WL_CONNECTED){
    Serial.print(".");
    delay(500); 
  }
  Serial.println();
  Serial.println("WiFi connected to:");
  Serial.println(WiFi.SSID());
  Serial.println("IP Address:");
  Serial.println(WiFi.localIP());
}

byte doFTP(){
  if (client.connect(server,21)) {
    Serial.println(F("Command connected"));
  }
  else {
    Serial.println(F("Command connection failed"));
    return 0;
  }
  // send login name and password
  if(!eRcv()) return 0;
  client.println(F("USER babythesismonitor"));
  //if(!eRcv()) return 0;
  client.println(F("PASS daffa009"));
  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();
    return 0;
  }
  // setup to send file file fileName
  client.println(F("CWD /public_html"));
  client.print(F("STOR "));
  client.println(fileName);
  webFile = SD.open(fileName,FILE_READ);
  
  while(webFile.available()){
    //Serial.println("1");
    if (webFile.available()>= MTU_Size){
      clientCount = MTU_Size;
      webFile.read(&clientBuf[0],clientCount);
      //Serial.println("2");
    }
    else{
      clientCount = webFile.available();
      webFile.read(&clientBuf[0],clientCount);
      //Serial.println("3");
    }
    
    if(clientCount > 0){
      if(!eRcv()){
        dclient.stop();
        return 0;
      }
      // send file contents
      dclient.write((const uint8_t *)&clientBuf[0],clientCount);
      // Serial.println("4");
      clientCount = 0;
    }
  }
  webFile.close();
  // send file contents
  //dclient.write(clientBuf,clientCount);
  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"));
  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"));
  Serial.println(F("SD closed"));
}

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

  //setupFirebase();
  
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  
  Serial.println("Ready.");
  doFTP();
}

but now the processed got stop with exception ill attach the sreenshot later

ill attach the sreenshot later

Don't.

The exception information is text. Post the text AS TEXT.

PaulS:
Don't.

The exception information is text. Post the text AS TEXT.

PaulS:

  byte inChar;

It is NOT a good idea to use a type in a name when the type in the name is not the type of the name.

char clientBuf[64]; // file to send

That array holds the data to send, not the file to send.

char is a signed type. Your binary data is not signed. Using the proper type of array would not be a bad thing.

int clientCount;

Are you sure that you don't need a long to hold a value up to 64?

#define MTU_Size 2*1460
if (webFile.available()>= MTU_Size){
clientCount = MTU_Size;
webFile.read(&clientBuf[0],clientCount);
}
The second argument to the read() method is the size of the array. It hardly seems likely that you can fit 2920 bytes in a 64 element array.

here the exception

Exception (28):
epc1=0x40207cd6 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

ctx: cont
sp: 3fff3930 end: 3fff3bd0 offset: 01a0

stack>>>
3fff3ad0: 3ffee700 3fff299c 00000064 40205298
3fff3ae0: 00000032 3ffe8304 3fff50cc 402052d8
3fff3af0: 4020137a 3fff25e4 3fff2680 3ffe8928
3fff3b00: 3fff25e8 3ffe8304 00000000 3ffe8928
3fff3b10: 00000000 3fff2b7c 3fff2680 40207e10
3fff3b20: 3ffe8928 3fff2b7c 3fff2680 40205980
3fff3b30: 00000000 3fff2b7c 3fff2680 4020235d
3fff3b40: 3ffe8890 0000000b 3ffe88d3 3fff2ba8
3fff3b50: 3ffe88b4 0000000e 3fff2b7c 40204f39
3fff3b60: 3ffe8c6c 3fff26c8 3fff2b7c 3fff2ba8
3fff3b70: 3ffe88b4 3fff2b7c 3fff2b7c 40204f64
3fff3b80: 00000000 3fff26c8 3fff4eec 402018ac
3fff3b90: 3ffe8890 00000000 3fff2b7c 3fff2ba8
3fff3ba0: 3fffdad0 00000000 3fff2b7c 402024e0
3fff3bb0: 3fffdad0 00000000 3fff2ba0 4020526c
3fff3bc0: feefeffe feefeffe 3fff2bb0 40100710
<<<stack<<<

ets Jan 8 2013,rst cause:2, boot mode:(1,6)

There is a tool for deciphering the stack trace information from the ESP. You need to find, and use, that tool.