My first post on this forum. Retired electronics engineer (graduated in the early 70's so a long time since I did any real work ). Started playing with arduinos a couple of weeks ago, very quickly found the nodeMCU and wow - what a lot for so little!
I was looked for an FTP client - wanted to upload files to my synology NAS - and googling, all roads led to 'Surfer Tim':
Tried it, didn't work. So more googling and hacked it to work - simple problem (still took me a day of head-scratching before the penny dropped), the original code was trying to upload a file to the root folder on my NAS, hence a permissions problem. But I'd started, so couldn't finish...
Made a few changes, the most significant being:
The ability to specify the destination folder.
No dependence on global variables - I think this is bad programming practice, or is it just me?
A lot more comments - more for my own benefit rather than anybody else.
So, my first contribution to the community, attached file - a revised FTP client.
AndyMH:
My first post on this forum. Retired electronics engineer (graduated in the early 70's so a long time since I did any real work ). Started playing with arduinos a couple of weeks ago, very quickly found the nodeMCU and wow - what a lot for so little!
I was looked for an FTP client - wanted to upload files to my synology NAS - and googling, all roads led to 'Surfer Tim':
Tried it, didn't work. So more googling and hacked it to work - simple problem (still took me a day of head-scratching before the penny dropped), the original code was trying to upload a file to the root folder on my NAS, hence a permissions problem. But I'd started, so couldn't finish...
Made a few changes, the most significant being:
The ability to specify the destination folder.
No dependence on global variables - I think this is bad programming practice, or is it just me?
A lot more comments - more for my own benefit rather than anybody else.
So, my first contribution to the community, attached file - a revised FTP client.
Thanks a lot!
I have occupied this code and it works very well, except when you disconnect the WiFi in the midst of an FTP transaction.
My solution was to add a timeout in the ERCV function.
/*------------------------------------------------------
* FUNCTION - eRcv
* Reads the response from an FTP server and stores the
* output in a buffer.Extracts the server return code from
* the buffer.
*
* Parameters passed:
* aclient - a wifi client connected to FTP server and
* delivering the server response
* outBuf - a buffer to store the server response on
* size - size of the buffer in bytes
*
* Return codes:
* These are the first three chars in the buffer and are
* defined in
* https://en.wikipedia.org/wiki/List_of_FTP_server_return_codes
*
* Dependencies:
* Libraries - <ESP8266WiFi.h> wifi library
* Functions - none
--------------------------------------------------------*/
short eRcv(WiFiClient aclient, char outBuf[], int size){
unsigned long start = millis();
byte thisByte;
char index;
String respStr = "";
while(!aclient.available()&& millis()-start <60000) delay(1);
index = 0;
while(aclient.available() && millis()-start <60000 ) {
thisByte = aclient.read();
Serial.write(thisByte);
if(index < (size - 2)) { //less 2 to leave room for null at end
outBuf[index] = thisByte;
index++;}
} //note if return from server is > size it is truncated.
if (millis()-start >=60000){
return 999;
}
outBuf[index] = 0; //putting a null because later strtok requires a null-delimited string
//The first three bytes of outBuf contain the FTP server return code - convert to int.
for(index = 0; index < 3; index++) {respStr += (char)outBuf[index];}
return respStr.toInt();
} // end function eRcv
Worked first time saving to my website - not usually the case with other code I have downloaded and tried. One problem was it was not saved where I expected - root. I modified the folder path from "" and it was saved to the correct folder.
Need to modify the code for my SD card and away we go.
I like what you guys did with the code. I do have one request though. If you guys wouldn't mind, could you fork my github repo, add your changes and then put up a PR? I would like to further improve on this making it more object oriented and an actual library so that this code doesn't need to be manually copied or included as part of your main script. The original repo is located here: GitHub - fryguy128/esp8266FTP: This repository contains arduino sketches related to FTP on the esp8266 The initial iteration I had created was something that I had just done in an evening when I was in college and that's why it was fairly rough when you first found it.
AndyMH:
My first post on this forum. Retired electronics engineer (graduated in the early 70's so a long time since I did any real work ). Started playing with arduinos a couple of weeks ago, very quickly found the nodeMCU and wow - what a lot for so little!
I was looked for an FTP client - wanted to upload files to my synology NAS - and googling, all roads led to 'Surfer Tim':
Tried it, didn't work. So more googling and hacked it to work - simple problem (still took me a day of head-scratching before the penny dropped), the original code was trying to upload a file to the root folder on my NAS, hence a permissions problem. But I'd started, so couldn't finish...
Made a few changes, the most significant being:
The ability to specify the destination folder.
No dependence on global variables - I think this is bad programming practice, or is it just me?
A lot more comments - more for my own benefit rather than anybody else.
So, my first contribution to the community, attached file - a revised FTP client.
rodrigokfw:
Thanks a lot!
I have occupied this code and it works very well, except when you disconnect the WiFi in the midst of an FTP transaction.
My solution was to add a timeout in the ERCV function.
Reads the response from an FTP server and stores the
output in a buffer.Extracts the server return code from
the buffer.
Parameters passed:
* aclient - a wifi client connected to FTP server and
* delivering the server response
* outBuf - a buffer to store the server response on
* size - size of the buffer in bytes
*
Dependencies:
* Libraries - <ESP8266WiFi.h> wifi library
* Functions - none
--------------------------------------------------------*/
short eRcv(WiFiClient aclient, char outBuf[], int size){
unsigned long start = millis();
byte thisByte;
char index;
String respStr = "";
while(!aclient.available()&& millis()-start <60000) delay(1);
index = 0;
while(aclient.available() && millis()-start <60000 ) {
thisByte = aclient.read();
Serial.write(thisByte);
if(index < (size - 2)) { //less 2 to leave room for null at end
outBuf[index] = thisByte;
index++;}
} //note if return from server is > size it is truncated.
if (millis()-start >=60000){
return 999;
}
outBuf[index] = 0; //putting a null because later strtok requires a null-delimited string
//The first three bytes of outBuf contain the FTP server return code - convert to int.
for(index = 0; index < 3; index++) {respStr += (char)outBuf[index];}
return respStr.toInt();
} // end function eRcv
I'm also working in to providing a clean modern C++ implementation for an FTP client. I've documented the work here:
Still a lot of work to do but so far is a little bit more cleaner and is working pretty good. I recently refactored the interface to make use of named type arguments: