Skip to content

Commit

Permalink
fix read dir with MLSD, and faster
Browse files Browse the repository at this point in the history
  • Loading branch information
ldab committed Jul 12, 2019
1 parent 013d76c commit 5aabdbc
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 32 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ A FTP-Client for the ESP32
## TODO

- [ ] List Dir contents test with different serves;
- [ ] Implement `FEAT` in order to choose which mode to list dir...
- [ ] Implement download;

* For the uploading example we will use the GitHub Octocat, which binary file is [here](./src/octocat.h):
Expand Down
22 changes: 12 additions & 10 deletions examples/upload_image/upload_image.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Distributed as-is; no warranty is given.
#define WIFI_SSID ""
#define WIFI_PASS ""

char ftp_server[] = "ftp.drivehq.com";
char ftp_server[] = "";
char ftp_user[] = "";
char ftp_pass[] = "";

Expand All @@ -40,26 +40,28 @@ void setup()

ftp.OpenConnection();

// Get directory contentx
// Get directory content
ftp.InitFile("Type A");
char list[256];
ftp.ContentList("/My Pictures/", list);
String list[128];
ftp.ChangeWorkDir("/public_html/zyro/gallery_gen/");
ftp.ContentList("", list);
Serial.print("\nDirectory info: ");
Serial.println(list);
Serial.println(list[2]);
ftp.CloseFile();


// Make a new directory
ftp.InitFile("Type A");
ftp.MakeDir("/jagua");
ftp.MakeDir("/my_new_dir");
ftp.CloseFile();

// Create the new file and send the image
ftp.InitFile("Type A");
ftp.ChangeWorkDir("/wwwhome/images");
ftp.NewFile("octocat.png");
ftp.InitFile("Type I");
ftp.ChangeWorkDir("/my_new_dir");
ftp.NewFile("octocat.jpg");
ftp.WriteData( octocat_pic, sizeof(octocat_pic) );
ftp.CloseFile();

// Create the file new and write a string into it
ftp.InitFile("Type A");
ftp.NewFile("hello_world.txt");
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "esp32_ftpclient",
"version": "0.0.1",
"version": "0.0.2",
"description": "A FTP-Client for the ESP32",
"keywords": "sensors, low power, mobile, ftp, web, cloud, iot, esp32, espressif",
"authors":
Expand Down
10 changes: 10 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=esp32_ftpclient
version=0.0.2
author=Leonardo Bispo
license=MIT
maintainer=Leonardo Bispo <[email protected]>
sentence=An FTP-Client for the ESP32.
paragraph=An FTP-Client for the ESP32.
category=Communication
url=https://github.com/ldab/ESP32_FTPClient
architectures=esp32
52 changes: 33 additions & 19 deletions src/ESP32_FTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,25 @@ void ESP32_FTPClient::WriteClientBuffered(WiFiClient* cli, unsigned char * data,
}

void ESP32_FTPClient::GetFTPAnswer (char* result, int offsetStart) {
//byte thisByte;
char thisByte;
outCount = 0;

String response;

while (!client.available()) delay(1);

while (client.available()) {
//thisByte = client.read();
response = client.readString();
response.toCharArray(outBuf, sizeof(outBuf));
/*if ( outCount < sizeof(outBuf) ) {
thisByte = client.read();
if (outCount < sizeof(outBuf)) {
outBuf[outCount] = thisByte;
outCount++;
outBuf[outCount] = 0;
}*/
}
}
if(result != NULL)
{
for(int i = offsetStart; i<sizeof(outBuf); i++)
{
if(result != NULL){
Serial.println("Result start");
for(int i = offsetStart; i<sizeof(outBuf); i++){
result[i] = outBuf[i - offsetStart];
}

Serial.print("Result: ");
//Serial.print(result);
Serial.print(response);
Serial.write(result);
Serial.println("Result end");
}
}
Expand Down Expand Up @@ -190,9 +182,31 @@ void ESP32_FTPClient::MakeDir(const char * dir) {
GetFTPAnswer();
}

void ESP32_FTPClient::ContentList(const char * dir, char * list) {
void ESP32_FTPClient::ContentList(const char * dir, String * list) {
char _resp[ sizeof(outBuf) ];
uint16_t _b = 0;

Serial.println("Send MLSD");
client.print(F("MLSD "));
client.print(F("MLSD"));
client.println(F(dir));
GetFTPAnswer(list);
GetFTPAnswer(_resp);

// Convert char array to string to manipulate and find response size
// each server reports it differently, TODO = FEAT
//String resp_string = _resp;
//resp_string.substring(resp_string.lastIndexOf('matches')-9);
//Serial.println(resp_string);

while( !dclient.available() ) delay(1);

while(dclient.available())
{
if( _b < 128 )
{
list[_b] = dclient.readStringUntil('\n');
//Serial.println(String(_b) + ":" + list[_b]);
_b++;
}
}

}
4 changes: 2 additions & 2 deletions src/ESP32_FTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class ESP32_FTPClient
{
private:
void WriteClientBuffered(WiFiClient* cli, unsigned char * data, int dataLength);
char outBuf[256];
char outBuf[128];
unsigned char outCount;
WiFiClient client;
WiFiClient dclient;
Expand All @@ -29,5 +29,5 @@ class ESP32_FTPClient
void ChangeWorkDir(const char * dir);
void DeleteFile(const char * file);
void MakeDir(const char * dir);
void ContentList(const char * dir, char * list);
void ContentList(const char * dir, String * list);
};

0 comments on commit 5aabdbc

Please sign in to comment.