Translating FTP code for use with Wi-Fi Shield library

I am attempting to use the code found here to upload a picture to my FTP server. The code on the page I linked is for Ethernet shields and I am using the original Arduino Wi-Fi shield.. I am using an Arduino MEGA 2560 and I made some changes to the main FTP code to make it compatible with the Wi-Fi shield I am using. I am not an expert on programming.

Regarding the connections, I mounted the shield and inserted the micro SD card and have done nothing else. Here is the edited code I uploaded on the Arduino:

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

#include <SD.h>
#include <SPI.h>
#include <WiFi.h>
// comment out next line to write to SD from FTP server
#define FTPWRITE

// this must be unique
byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x49, 0x72 };  

// change to your network settings
 char ssid[]= "Mynetwork";
 char pass[]= "Mypassword";


// change to your server
IPAddress server( 1, 2, 3, 4 );

WiFiClient client;
WiFiClient dclient;

char outBuf[128];
char outCount;

// change fileName to your file (8.3 format!)
char fileName[13] = "Kindred.jpg";

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

  digitalWrite(10,HIGH);

  if(SD.begin(4) == 0)
  {
    Serial.println(F("SD init fail"));          
  }

  WiFi.begin(ssid, pass); 
  digitalWrite(10,HIGH);
  delay(2000);
  Serial.println(F("Ready. Press f or r"));
}

void loop()
{
  byte inChar;

  inChar = Serial.read();

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

  if(inChar == 'r')
  {
    readSD();    
  }

}

File fh;

byte doFTP()
{
#ifdef FTPWRITE
  fh = SD.open(fileName,FILE_READ);
#else
  SD.remove(fileName);
  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,21)) {
    Serial.println(F("Command connected"));
  } 
  else {
    fh.close();
    Serial.println(F("Command connection failed"));
    return 0;
  }

  if(!eRcv()) return 0;

  client.println(F("USER myuser"));

  if(!eRcv()) return 0;

  client.println(F("PASS mypassword"));

  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 readSD()
{
  fh = SD.open(fileName,FILE_READ);

  if(!fh)
  {
    Serial.println(F("SD open fail"));
    return;    
  }

  while(fh.available())
  {
    Serial.write(fh.read());
  }

  fh.close();
}

If you compare the two codes you will see that I only made some minor changes such as replacing the Ethernet library functions with the Wi-Fi library functions. Here is the output of the serial monitor:

Ready. Press f or r
SD opened
Command connection failed
FTP FAIL

It is my first time doing something like this and so I will have a few questions. The IP address I am supposed to enter is the IP address of my network, right? I assume that my FTP server shares the same address. Any help would be appreciated!

Leona_S:
I am using an Arduino MEGA 2560 and I made some changes to the main FTP code to make it compatible with the Wi-Fi shield I am using. I am not an expert on programming.

Can you save us some time and comment on what changes you made?

I deleted this part of the code:

// change to your network settings
IPAddress ip( 192, 168, 2, 2 );    
IPAddress gateway( 192, 168, 2, 1 );
IPAddress subnet( 255, 255, 255, 0 );

and changed the following lines:

EthernetClient client;      -->          WiFiClient client;
EthernetClient dclient;    -->          WiFiClient dclient;


Ethernet.begin(mac, ip, gateway, gateway, subnet);     -->  WiFi.begin(ssid, pass);

can you check that your FTP server is accessable from other machines in the network, e.g. by running an FTP client on a different PC? this will check if the firewall is letting FTP requests thru

using Wireshark can you see the FTP requests from the Arduino?

Attempting to connect to the FTP server using a different PC does not work. I even disabled Firewall on both PC's. I am using "localhost" as the FTP server host and, on the FTP client, I enter the IP address of my laptop.

do you have the FTP server IP address correct?
run the Command Prompt (CMD) and type ipconfig, it should display

ireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . : lan
   Link-local IPv6 Address . . . . . : fe80::c1d4:6fba:2c61:8827%3
   IPv4 Address. . . . . . . . . . . : 192.168.1.96
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.254

in this case would be 192.168.1.96

are the server and the client machines on the same router ? what does the router control panel tell you about connected devices?

does you arduino connect to the WiFi network OK? if not run the ScanNetworks example

if you are running virtual machines (using software such as virtualbox) you can get odd effects due to the their network connections. I disable all the virtual network devices if I am not running VMs.

I have modified a Ethernet FTP test to use a ESP-O1 WiFi device (I don't have a WiFi shield with SD card)
it connects to to the WiFi network and when f is pressed connects to the FTP server and sends a single line file

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

   
*/

// WiFiexample to send a single line file

#include <SPI.h>
#include "WiFiEsp.h"

//#define SPRINTF       // comment out to use strcat() 

// update for your WiFi
char ssid[] = "zzzzz";            // your network SSID (name)
char pass[] = "xxxxx";        // your network password

// change to your server
IPAddress server( 192, 168, 1, 96 );

WiFiEspClient client;
WiFiEspClient 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 fileName[13] = "test.txt";
char clientBuf[64]="hello from ESP-01 1234567890";  // file to send
int clientCount = strlen(clientBuf);


void setup()
{
  Serial.begin(115200);
 // initialize serial for ESP module
  Serial1.begin(9600);
  // initialize ESP module
  WiFi.init(&Serial1);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  // you're connected now, so print out the data
  Serial.println("You're connected to the network");
  
  printWifiStatus();  delay(2000);
  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 horace"));
  if(!eRcv()) return 0;
  client.println(F("PASS horace"));
  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.print(F("STOR "));
  client.println(fileName);
  if(!eRcv())
  {
    dclient.stop();
    return 0;
  }
  // 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 printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

a run gives

[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 1.5.4
Attempting to connect to WPA SSID: zzzzz
[WiFiEsp] Connected to zzzzz
You're connected to the network
SSID: zzzzz
IP Address: 192.168.1.97
Signal strength (RSSI):-612 dBm
Ready. Press f
[WiFiEsp] Connecting to 192.168.1.96
Command connected
220-FileZilla Server 0.9.60 beta
220-written by Tim Kosse (tim.kosse@filezilla-project.org)
220 Please visit https://filezilla-project.org/
331 Password required for horace
230 Logged on
215 UNIX emulated by FileZilla
200 Type set to I
227 Entering Passive Mode (192,168,1,96,246,169)
Data port: 63145
[WiFiEsp] Connecting to 192.168.1.96
Data connected
150 Opening data channel for file upload to server of "/test.txt"
[WiFiEsp] Disconnecting  2
Data disconnected
226 Successfully transferred "/test.txt"
221 Goodbye
Command disconnected
FTP OK

the file test.txt on the server PC contains "hello from ESP-01 1234567890"

Thank you for you reply.

are the server and the client machines on the same router ? what does the router control panel tell you about connected devices?

Both the client and the server are connected to the same router. I don't really know what the control panel is supposed to tell me, other than that the two PC's have different IP addresses. I am sure of the server's IP address (192.168.1.6 in my case).

does you arduino connect to the WiFi network OK? if not run the ScanNetworks example

Yes, it does. I tested the example codes and the Wi-Fi shield is working fine.

I get this error when I connect to the localhost server:

You appear to be behind a NAT router. Please configure the passive mode settings and forward a range of ports in your router.
Warning: FTP over TLS is not enabled, users cannot securely log in.
Retrieving account settings, please wait...

Could this be affecting my client PC? I can connect to the server if I am using the same PC as the client.

you should only need to configure the NAT settings if you wish to be able to connect to the FTP server from the external internet. You would setup port forwarding to forward FTP packets to the appropriate PC.

try the simple FTP in post #6

  1. change #include "WiFiEsp.h" to include your WiFi header etc
  2. change the WiFi SSID and password
  3. change the FTP login username and password

post the response you see on the Serial Monitor

what FTP server are you using?

I tried the code you posted and got an error due to this part:

  Serial.begin(115200);
 // initialize serial for ESP module
  Serial1.begin(9600);
  // initialize ESP module
  WiFi.init(&Serial1);

I changed it like so:

  Serial.begin(9600);
 // initialize serial for ESP module

Serial monitor output:

Attempting to connect to WPA SSID: zzzz
You're connected to the network
SSID: zzzz
IP Address: 192.168.1.32
Signal strength (RSSI):-69 dBm
Ready. Press f
Command connection failed
FTP FAIL

what FTP server are you using?

I am using my laptop as the server. When I launch FileZilla Server, I enter "localhost" as my host and keep the port as default (14147) and I click "connect". Hope that answers the question.

I get Command connection failed when

  1. the FTP server is disabled (not running) or
  2. the firewall (inbound rules) is blocking access or
  3. I have the incorrect IP address for IPAddress server( 192, 168, 1, 96 );

horace:
I get Command connection failed when

  1. the FTP server is disabled (not running) or
  2. the firewall (inbound rules) is blocking access or
  3. I have the incorrect IP address for IPAddress server( 192, 168, 1, 96 );

Hi horace
i use heltec wifi kit8 send file to FTP, sometimes I do not complete it, I debug the error at line "array_pasv = atoi (tSt);" or "array_pasv = strtol (tStr, & ptr, 10);" Can you help me? Many thanks

does it fail on files over a certain size?
could you be overrunning an array?
what is the signal quality like?

horace:
does it fail on files over a certain size?
could you be overrunning an array?
what is the signal quality like?

Hello horace
Thank you for your interest, I answer the following:

  1. I don't use SD file, I use SPIFFS file.
  2. I checked, the program is not yet in the file transfer phase, and in the process of shaking hands with the FTP server
  3. none of my arrays have been used yet
  4. Good data quality, RSSI = -26dbm, ESP8266 distance to router = 0.5m
  5. because I misunderstood, so I got the above question, I checked and the 2 atoi or strtoi functions are good.
  6. After checking and using the serial debug, I found the following error:
    "test befor whiletest after whiletest respCode2230
    test befor whiletest after whiletest respCode2215 UNIX emulated by FileZilla
    Fatal exception 28 (LoadProhibitedCause):
    epc1 = 0x4020e3d2, epc2 = 0x00000000, epc3 = 0x00000000, excvaddr = 0x00000000, depc = 0x00000000 "
    the line "test befor whiletest after while" added by me in function eRcv():
    "Serial.write (" test befor while ");
    while (! client.available ()) delay (1);
    Serial.write ("test after while");

respCode = client.peek ();
"
7. However, sometimes the file is sent successfully
8. MY MAIN PROBLEM IS TO TRANSFER FILE FROM SPIFFS TO MYSERVER BY HTTP OR FTP AS WELL, I WOULD HELP YOU TO HELP ME