我正试图在ESP8266的SD卡上接收位于FTP服务器上的文件(.txt文件)。我正在使用此代码,但已通过WiFi对其进行了编辑。
https://playground.arduino.cc/Code/FTP/
WiFi可以正常工作,我可以将SD卡中的文件发送到FTP服务器,但需要将其取反,这样我就可以接收位于FTP服务器上的文件。
如果我要从代码中删除此行:
// comment out next line to write to SD from FTP server
#define FTPWRITE
它应该从FTP服务器写入我的SD卡。它实际上发送了一些内容,但它在capslock中是一个空文件。我试图重命名文件,更改了文件中的文本和文件路径,但没有成功。
这是我的串行监视器在删除以下行时显示的内容:#define FTPWRITE
SD opened
Command connected
220 FileZilla Server 0.9.60 beta
331 Password required for kalenderdisplay
230 Logged on
215 UNIX emulated by FileZilla
200 Type set to I
227 Entering Passive Mode (x,x,x,x,218,99)
Data port: 55907
Data connected
150 Opening data channel for file download from server of "/Meeting.txt"
226 Successfully transferred "/Meeting.txt"
Data disconnected
这包括行时:#define FTPWRITE
SD opened
Command connected
220 FileZilla Server 0.9.60 beta
331 Password required for kalenderdisplay
230 Logged on
215 UNIX emulated by FileZilla
200 Type set to I
227 Entering Passive Mode (x,x,x,x,217,107)
Data port: 55659
Data connected
150 Opening data channel for file upload to server of "/Meeting.txt"
Writing
Data disconnected
226 Successfully transferred "/Meeting.txt"
221 Goodbye
Command disconnected
SD closed
FTP OK
那是我正在使用的代码
#include <SD.h>
#include <SPI.h>
#include <ESP8266WiFi.h>
// comment out next line to write to SD from FTP server
#define FTPWRITE
#define STASSID "xxx"
#define STAPSK "xxx"
const char* ssid = STASSID;
const char* password = STAPSK;
// change to your server
IPAddress server( x, x, x, x );
WiFiClient client;
WiFiClient dclient;
char outBuf[128];
char outCount;
// change fileName to your file (8.3 format!)
char fileName[13] = "Meeting.txt";
File fh;
void setup()
{
Serial.begin(115200);
if(SD.begin(4) == 0)
{
Serial.println(F("SD init fail"));
}
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
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();
}
}
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 kalenderdisplay"));
if(!eRcv()) return 0;
client.println(F("PASS kalenderdisplay"));
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();
}
是我的代码还是我的FTP服务器? 以及如何解决该问题?