Skip to content

Commit

Permalink
fix infinite while loop when Offline
Browse files Browse the repository at this point in the history
  • Loading branch information
ldab committed Aug 9, 2019
1 parent 2f51996 commit 126e0a1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
19 changes: 11 additions & 8 deletions examples/upload_image/upload_image.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ https://github.com/ldab/ESP32_FTPClient
Distributed as-is; no warranty is given.
******************************************************************************/

#include "Arduino.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <ESP32_FTPClient.h>
Expand Down Expand Up @@ -45,19 +45,22 @@ void setup()
String list[128];
ftp.ChangeWorkDir("/public_html/zyro/gallery_gen/");
ftp.ContentList("", list);
Serial.print("\nDirectory info: ");
Serial.println(list[2]);
ftp.CloseFile();

Serial.println("\nDirectory info: ");
for(int i = 0; i < sizeof(list); i++)
{
if(list[i].length() > 0)
Serial.println(list[i]);
else
break;
}

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

// Create the new file and send the image
ftp.ChangeWorkDir("my_new_dir");
ftp.InitFile("Type I");
ftp.ChangeWorkDir("/my_new_dir");
ftp.NewFile("octocat.jpg");
ftp.WriteData( octocat_pic, sizeof(octocat_pic) );
ftp.CloseFile();
Expand Down
19 changes: 12 additions & 7 deletions src/ESP32_FTPClient.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include <WiFiClient.h>
#include "ESP32_FTPClient.h"

ESP32_FTPClient::ESP32_FTPClient(char* _serverAdress, char* _userName, char* _passWord){
ESP32_FTPClient::ESP32_FTPClient(char* _serverAdress, char* _userName, char* _passWord, uint16_t _timeout){
userName = _userName;
passWord = _passWord;
serverAdress = _serverAdress;
timeout = _timeout;
}

WiFiClient* ESP32_FTPClient::GetDataClient() {
Expand Down Expand Up @@ -39,7 +40,8 @@ void ESP32_FTPClient::GetFTPAnswer (char* result, int offsetStart) {
char thisByte;
outCount = 0;

while (!client.available()) delay(1);
unsigned long _m = millis();
while (!client.available() && millis() < _m + timeout) delay(1);

while (client.available()) {
thisByte = client.read();
Expand Down Expand Up @@ -84,7 +86,7 @@ void ESP32_FTPClient::CloseConnection() {
void ESP32_FTPClient::OpenConnection() {
Serial.print(F("Connecting to: "));
Serial.println(serverAdress);
if (client.connect(serverAdress, 21)) { // 21 = FTP server
if (client.connect(serverAdress, 21, timeout)) { // 21 = FTP server
Serial.println(F("Command connected"));
}
GetFTPAnswer();
Expand Down Expand Up @@ -198,8 +200,9 @@ void ESP32_FTPClient::ContentList(const char * dir, String * list) {
//String resp_string = _resp;
//resp_string.substring(resp_string.lastIndexOf('matches')-9);
//Serial.println(resp_string);

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

unsigned long _m = millis();
while( !dclient.available() && millis() < _m + timeout) delay(1);

while(dclient.available())
{
Expand All @@ -221,7 +224,8 @@ void ESP32_FTPClient::DownloadString(const char * filename, String &str) {
char _resp[ sizeof(outBuf) ];
GetFTPAnswer(_resp);

while( !GetDataClient()->available() ) delay(1);
unsigned long _m = millis();
while( !GetDataClient()->available() && millis() < _m + timeout) delay(1);

while( GetDataClient()->available() )
{
Expand All @@ -240,7 +244,8 @@ void ESP32_FTPClient::DownloadFile(const char * filename, unsigned char * buf, s

char _buf[2];

while( !dclient.available() ) delay(1);
unsigned long _m = millis();
while( !dclient.available() && millis() < _m + timeout) delay(1);

while(dclient.available())
{
Expand Down
3 changes: 2 additions & 1 deletion src/ESP32_FTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ class ESP32_FTPClient
char* serverAdress;
unsigned char clientBuf[1500];
size_t bufferSize = 1500;
uint16_t timeout = 10000;
WiFiClient* GetDataClient();

public:
ESP32_FTPClient(char* _serverAdress, char* _userName, char* _passWord);
ESP32_FTPClient(char* _serverAdress, char* _userName, char* _passWord, uint16_t _timeout = 10000);
void OpenConnection();
void CloseConnection();
void NewFile (const char* fileName);
Expand Down

0 comments on commit 126e0a1

Please sign in to comment.