If FTP isn't acceptable, let me know. I won't waste time on it.
Seems to be working ok. I can download and read the file.
It is FTP with user and password auth.
Edit: Here is the code. Enter 'f' to download. 'r' to read the file from the SD.
Change the user, password, and fileName. I cleaned up the code a bit. Left the response troubleshooting stuff for you.
#include <SD.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67 };
IPAddress ip( 192, 168, 1, 2 );
IPAddress gateway( 192, 168, 1, 1 );
IPAddress subnet( 255, 255, 255, 0 );
IPAddress server(192,168,1,29);
EthernetClient client;
EthernetClient dclient;
char outBuf[128];
char outCount;
char user[] = "myUser";
char password[] = "myPassword";
char fileName[] = "test.txt";
void setup()
{
Serial.begin(115200);
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
pinMode(10,OUTPUT);
digitalWrite(10,HIGH);
if(SD.begin(4) == 0)
{
Serial.println("SD init fail");
}
Ethernet.begin(mac, ip, gateway, gateway, subnet);
delay(2000);
Serial.println("Ready. Press f or r");
}
void loop()
{
byte inChar;
inChar = Serial.read();
if(inChar == 'f')
{
if(doFTP()) Serial.println("FTP OK");
else Serial.println("FTP FAIL");
}
if(inChar == 'r')
{
readSD();
}
}
File fh;
byte doFTP()
{
SD.remove(fileName);
fh = SD.open(fileName,FILE_WRITE);
if(!fh)
{
Serial.println("SD open fail");
return 0;
}
Serial.println("SD open");
if (client.connect(server,21)) {
Serial.println("Command connected");
}
else {
fh.close();
Serial.println("Command connection failed");
return 0;
}
if(!eRcv()) return 0;
client.write("USER ");
client.write(user);
client.write("\r\n");
if(!eRcv()) return 0;
client.write("PASS ");
client.write(password);
client.write("\r\n");
if(!eRcv()) return 0;
client.write("SYST\r\n");
if(!eRcv()) return 0;
client.write("TYPE I\r\n");
if(!eRcv()) return 0;
client.write("PWD\r\n");
if(!eRcv()) return 0;
// client.write("CWD ./htdocs\r\n");
// client.write("PWD\r\n");
// if(!eRcv()) return 0;
client.write("PASV\r\n");
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("Bad PASV Answer");
}
}
unsigned int hiPort,loPort;
hiPort = array_pasv[4] << 8;
loPort = array_pasv[5] & 255;
Serial.print("Data port: ");
hiPort = hiPort | loPort;
Serial.println(hiPort);
if (dclient.connect(server,hiPort)) {
Serial.println("Data connected");
}
else {
Serial.println("Data connection failed");
client.stop();
fh.close();
return 0;
}
client.write("RETR ");
client.println(fileName);
if(!eRcv())
{
dclient.stop();
return 0;
}
while(dclient.connected())
{
while(dclient.available())
{
byte c = dclient.read();
fh.write(c);
Serial.write(c);
}
}
fh.close();
Serial.println("SD closed");
dclient.stop();
Serial.println("Data disconnected");
client.write("QUIT\r\n");
client.stop();
Serial.println("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.write("QUIT\r\n");
while(!client.available()) delay(1);
while(client.available())
{
thisByte = client.read();
Serial.write(thisByte);
}
client.stop();
Serial.println("Command disconnected");
fh.close();
Serial.println("SD closed");
}
void readSD()
{
Serial.println("SD read");
fh = SD.open(fileName,FILE_READ);
if(!fh)
{
Serial.println("SD open fail");
return;
}
while(fh.available())
{
Serial.write(fh.read());
}
fh.close();
Serial.println("Done");
}