I am using SurferTim's FTP code here, which I have modified to work with WiFi. I can verify the code and upload it to the ESP8266, but when I try to run it, the board starts repeatedly resetting. I have had this problem every time I try to use the WiFi library with the ESP8266. However, when I change to using the ESP8266WiFi library (which works in other sketches, and can connect to the wifi properly), I get the following error. I am also including my modified version of the code below for reference. (Anywhere with three dashes --- is where I have removed info I don't want shared.) I'm still pretty new to this, so I may be missing something that should be obvious. Can anyone assist?
In file included from C:\Users\crlas_000\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi\src/ESP8266WiFi.h:39:0,
from C:\Users\crlas_000\Documents\Arduino\FTP\FTP.ino:10:
C:\Users\crlas_000\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi\src/WiFiClient.h: In instantiation of 'size_t WiFiClient::write(T&, size_t) [with T = unsigned char [64]; size_t = unsigned int]':
C:\Users\crlas_000\Documents\Arduino\FTP\FTP.ino:186:33: required from here
C:\Users\crlas_000\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi\src/WiFiClient.h:123:36: error: request for member 'available' in 'source', which is of non-class type 'unsigned char [64]'
size_t left = source.available();
^
C:\Users\crlas_000\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi\src/WiFiClient.h:127:5: error: request for member 'read' in 'source', which is of non-class type 'unsigned char [64]'
source.read(buffer.get(), will_send);
^
Using library SD at version 1.0.5 in folder: C:\Users\crlas_000\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\SD
Using library SPI at version 1.0 in folder: C:\Users\crlas_000\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\SPI
Using library ESP8266WiFi at version 1.0 in folder: C:\Users\crlas_000\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi
exit status 1
Error compiling for board Adafruit HUZZAH ESP8266.
#include <SD.h>
#include <SPI.h>
#include <ESP8266WiFi.h>
// comment out next line to write to SD from FTP server
#define FTPWRITE
// this must be unique
byte mac[] = { 0x18, 0xFE, 0x34, 0xDB, 0x52, 0x71 };
// change to your network settings
char* ssid = "---";
char* pass = "---";
// change to your server
IPAddress server( --- );
WiFiClient client;
WiFiClient dclient;
char outBuf[128];
char outCount;
// change fileName to your file (8.3 format!)
char fileName[13] = "current.txt";
void setup()
{
Serial.begin(115200);
digitalWrite(10,HIGH);
if(SD.begin(16) == 0)
{
Serial.println(F("SD init fail"));
}
WiFi.begin(ssid, pass);
digitalWrite(10,HIGH);
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();
}
}
File fh;
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 ---"));
if(!eRcv()) return 0;
client.println(F("PASS ---"));
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();
}