summaryrefslogtreecommitdiffstats
path: root/examples/FTPClientSample/FTPClientSample.ino
blob: c456d110f961fed7b452a7962fc24e92c4654b2a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
   This is an example sketch to show the use of the FTP Client.

   Please replace
     YOUR_SSID and YOUR_PASS
   with your WiFi's values and compile.

   If you want to see debugging output of the FTP Client, please
   select select an Serial Port in the Arduino IDE menu Tools->Debug Port

   Send L via Serial Monitor, to display the contents of the FS
   Send F via Serial Monitor, to fromat the FS
   Send G via Serial Monitor, to GET a file from a FTP Server

   This example is provided as Public Domain
   Daniel Plasa <dplasa@gmail.com>

*/
#include <ESP8266WiFi.h>
#include <LittleFS.h>
#include <FTPClient.h>

const char *ssid PROGMEM = "YOUR_SSID";
const char *password PROGMEM = "YOUR_PASS";

// tell the FTP Client to use LittleFS
FTPClient ftpClient(LittleFS);

// provide FTP servers credentials and servername
FTPClient::ServerInfo ftpServerInfo("user", "password", "hostname_or_ip");

void setup(void)
{
  Serial.begin(74880);
  WiFi.begin(ssid, password);

  bool fsok = LittleFS.begin();
  Serial.printf_P(PSTR("FS init: %S\n"), fsok ? PSTR("ok") : PSTR("fail!"));

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.printf_P(PSTR("."));
  }
  Serial.printf_P(PSTR("\nConnected to %S, IP address is %s\n"), ssid, WiFi.localIP().toString().c_str());

  ftpClient.begin(ftpServerInfo);
}

enum consoleaction
{
  show,
  get,
  put,
  wait,
  format,
  list
};
consoleaction action = show;

String fileName;
bool transferStarted = false;
uint32_t startTime;

void loop()
{
  // this is all you need
  // make sure to call handleFTP() frequently
  ftpClient.handleFTP();

  //
  // Code below just to debug in Serial Monitor
  //
  if (action == show)
  {
    Serial.printf_P(PSTR("Enter 'F' to format, 'L' to list the contents of the FS, 'G'/'P' + File to GET/PUT from/to FTP Server\n"));
    action = wait;
  }
  else if (action == wait)
  {
    if (transferStarted )
    {
      const FTPClient::Status &r = ftpClient.check();
      if (r.result == FTPClient::OK)
      {
        Serial.printf_P(PSTR("Transfer complete, took %lu ms\n"), millis() - startTime);
        transferStarted = false;
      }
      else if (r.result == FTPClient::ERROR)
      {
        Serial.printf_P(PSTR("Transfer failed after %lu ms, code: %u, descr=%s\n"), millis() - startTime, ftpClient.check().code, ftpClient.check().desc.c_str());
        transferStarted = false;
      }
    }

    if (Serial.available())
    {
      char c = Serial.read();
      if (c == 'F')
        action = format;
      else if (c == 'L')
        action = list;
      else if (c == 'G')
      {
        action = get;
        fileName = Serial.readStringUntil('\n');
        fileName.trim();
      }
      else if (c == 'P')
      {
        action = put;
        fileName = Serial.readStringUntil('\n');
        fileName.trim();
      }
      else if (!(c == '\n' || c == '\r'))
        action = show;
    }
  }

  else if (action == format)
  {
    startTime = millis();
    LittleFS.format();
    Serial.printf_P(PSTR("FS format done, took %lu ms!\n"), millis() - startTime);
    action = show;
  }

  else if ((action == get) || (action == put))
  {
    startTime = millis();
    ftpClient.transfer(fileName, fileName, action == get ?
                       FTPClient::FTP_GET_NONBLOCKING : FTPClient::FTP_PUT_NONBLOCKING);
    Serial.printf_P(PSTR("transfer started, took %lu ms!\n"), millis() - startTime);
    transferStarted = true;
    action = show;
  }

  else if (action == list)
  {
    Serial.printf_P(PSTR("Listing contents...\n"));
    uint16_t fileCount = listDir("", "/");
    Serial.printf_P(PSTR("%d files/dirs total\n"), fileCount);
    action = show;
  }
}

uint16_t listDir(String indent, String path)
{
  uint16_t dirCount = 0;
  Dir dir = LittleFS.openDir(path);
  while (dir.next())
  {
    ++dirCount;
    if (dir.isDirectory())
    {
      Serial.printf_P(PSTR("%s%s [Dir]\n"), indent.c_str(), dir.fileName().c_str());
      dirCount += listDir(indent + "  ", path + dir.fileName() + "/");
    }
    else
      Serial.printf_P(PSTR("%s%-16s (%ld Bytes)\n"), indent.c_str(), dir.fileName().c_str(), (uint32_t)dir.fileSize());
  }
  return dirCount;
}