cbrandt:
Du coup, la fonction FTPopen(), ce n'est que ça, on est d'accord?
Oui, tout à fait 
Supprime quand même le fh.close() (dans ftpopen()).
D'ailleurs fh aussi devrait être une variable locale à ftpuload() et ftpdownload().
Hello, voici ce que ça donne avec les 2 fonctions FTPupload() & FTPdownload() à la suite. La 2d ne passe tirs pas 
Connection FTP -----------------
Command connected
220 FTP Server ready.
331 Password required for xxxxxxxx
230 User xxxxxxxx logged in
215 UNIX Type: L8
Connection FTP -----------------
Command connection failed
Upload -------------------------
227 Entering Passive Mode (82,165,109,152,225,16).
Data port: 57616
Data connected
SD opened for upload
150 Opening ASCII mode data connection for aqua_box_old/input/BOX_0001/input/test.txt
File uploaded
Download -----------------------
226 Transfer complete
Bad PASV Answer
Bad PASV Answer
Bad PASV Answer
Bad PASV Answer
Bad PASV Answer
Bad PASV Answer
Data port: 0
Data connection failed
Close FTP -----------------------
Data disconnected
Le Serial renvoie le msg "Command connection failed" à cause de mon code suivant:
if(FTPopen() == 0) { // Open FTP server
FTPopen();
}
C'est pas jolie, mais c'est pour l'instant le seul moyen que j'ai trouvé pour relancer la fonction FTPopen() après une coupure du Serial (et donc une connexion pas fermée correctement). A voir par la suite pour essayer de faire un truc plus propre ...
En revanche, la fonction FTPdownload() ne passe tjrs pas (pi, les 2 fonction passent bien séparément), voici mon code :
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
// this must be unique
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xFE, 0xBA };
// change to your network settings
IPAddress ip( 192, 168, 0, 32 );
IPAddress gateway( 192, 168, 0, 1 );
IPAddress subnet( 255, 255, 255, 0 );
// change to your server
IPAddress server( xx, xx, xx, xx );
EthernetClient client;
EthernetClient dclient;
char outBuf[128];
char outCount;
String repFTPinput = "aqua_box_old/input/BOX_0001/input/"; // download directory
String repFTPoutput = "aqua_box_old/input/BOX_0001/output/"; // upload directory
void setup()
{
Serial.begin(115200);
pinMode(10,OUTPUT);
digitalWrite(10,HIGH);
if(SD.begin(4) == 0)
{
Serial.println(F("SD init fail"));
}
Ethernet.begin(mac, ip, gateway, gateway, subnet);
digitalWrite(10,HIGH);
delay(2000);
}
File fh;
void loop()
{
if(FTPopen() == 0) { // Open FTP server
FTPopen();
}
//FTPopen();
FTPupload("test.txt"); // Download file on SD
FTPdownload("BOX_0001.ini"); // Upload file on FTP
FTPclose(); // Close FTP Server
}
byte FTPopen()
{
Serial.println(F(" "));
Serial.println(F(" "));
Serial.println(F("Connection FTP -----------------"));
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 xxxxxxxx"));
if(!eRcv()) return 0;
client.println(F("PASS xxxxxxxx"));
if(!eRcv()) return 0;
client.println(F("SYST"));
if(!eRcv()) return 0;
delay(200);
}
byte FTPupload(char fileName[13])
{
EthernetClient dclient; //ouverture socket
Serial.println(F("Upload -------------------------"));
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;
}
fh = SD.open(fileName,FILE_READ);
if(!fh)
{
Serial.println(F("SD open fail"));
}
Serial.println(F("SD opened for upload"));
client.print(F("STOR "));
client.println(repFTPinput+fileName);
if(!eRcv())
{
dclient.stop();
return 0;
}
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);
Serial.println(F("File uploaded"));
dclient.stop(); // fermeture socket
fh.close(); // fermeture fichier
delay(200);
return 1;
}
byte FTPdownload(char fileNameInit[13])
{
Serial.println(F("Download -----------------------"));
EthernetClient dclient; //ouverture socket
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;
}
SD.remove(fileNameInit);
fh = SD.open(fileNameInit,FILE_WRITE);
if(!fh)
{
Serial.println(F("SD open fail"));
}
if(!fh.seek(0))
{
Serial.println(F("Rewind fail"));
fh.close();
return 0;
}
Serial.println(F("SD opened for dowload"));
client.print(F("RETR "));
client.println(repFTPoutput+fileNameInit);
if(!eRcv())
{
dclient.stop();
return 0;
}
while(dclient.connected())
{
while(dclient.available())
{
char c = dclient.read();
fh.write(c);
Serial.write(c);
}
}
dclient.stop(); // fermeture socket
fh.close(); //fermeture fichier
delay(200);
return 1;
}
byte FTPdelete(char fileNameInit[13])
{
Serial.println(F("Delete -----------------------"));
client.print(F("DELE "));
client.println(repFTPoutput+fileNameInit);
Serial.println(F("File deleted"));
delay(200);
}
byte FTPclose()
{
Serial.println(F("Close FTP -----------------------"));
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"));
Serial.println(F("--------------------------------"));
}
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"));
}