I need ESP8266 ftp server to my Internet place. It needs also a password

ForumCategory: esp8266I need ESP8266 ftp server to my Internet place. It needs also a password
Pekka Riitamaki asked 4 months ago

Dear sirs,
I have tried to send a small file ( a time and a light value ) to my Internet place, but I have not succeed.
I have found this file, it opens my WIFI ,but I can’t get connection to my Internet place.
Can somebody to find my failure?

// This is FTP client program, but I cant it to connect Internet
// It conncets to wifi

#include <ESP8266WiFi.h>
#include <FS.h>
#define FTPWRITE
//Change these variables to meet your needs
//#############################################
//Wifi
const char* WIFI_SSID= "Wifi ID";
const char* WIFI_PASS="WIFI Password";
//FTP stuff

const char* host = "ftp://ftp.xxxxxx.fi";
const char* userName = "host id";
const char* password = "host password";
//File Operation
char fileName[23] = "C:\Hotbox\data.txt";

//############################################
//DO NOT CHANGE THESE
char outBuf[128];
char outCount;
WiFiClient client;
WiFiClient dclient;

//###########################################
//Move into setup()
void setup() {
// Serial setup
Serial.begin(115200);
delay(100);
//Wifi Setup
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
// WiFi.begin(ssid);
WiFi.begin( WIFI_SSID, WIFI_PASS );
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
//SPIFF setup
SPIFFS.begin();
}
//#########################################
//Move into loop()
/*
*In the current state, this loop will append
*'Success!!' to the end of a filename then
*upload it to your FTP server
*SPIFFS.open(path, mode)
Opens a file. path should be an absolute path starting with a slash (e.g. /dir/filename.txt).
mode is a string specifying access mode.
It can be one of "r", "w", "a", "r+", "w+", "a+". "a" tarkoittaa että lisätään tiedostoon tämä uusi laiuse
Meaning of these modes is the same as for fopen C function.
*/
void loop() {
File f = SPIFFS.open(fileName, "a");
if (!f) {
Serial.println("file open failed");
}
else {
Serial.println("file open ok");
}
f.println("Success!!");
f.close();
if(doFTP()) Serial.println(F("FTP OK"));
else Serial.println(F("FTP FAIL"));
delay(10000);
}
//#######################################
/*
* This is the function where the magic happens.
* all credit goes to SurferTim. This will open
* and write the file to your FTP server
*/
byte doFTP()
{
File fh = SPIFFS.open(fileName, "r");
if (!fh) {
Serial.println("file open failed");
}
if (client.connect(host,21)) {
Serial.println(F("Command connected"));
}
else {
fh.close();
Serial.println(F("Command connection failed"));
return 0;
}
if(!eRcv()) return 0;
client.print("USER ");
client.println(userName);

if(!eRcv()) return 0;

client.print("PASS ");
client.println(password);

if(!eRcv()) return 0;

client.println("SYST");
if(!eRcv()) return 0;
client.println("Type I");
if(!eRcv()) return 0;
client.println("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(host, hiPort)){
Serial.println("Data connected");
}
else{
Serial.println("Data connection failed");
client.stop();
fh.close();
}

client.print("STOR ");
client.println(fileName);
if(!eRcv())
{
dclient.stop();
return 0;
}
Serial.println(F("Writing"));

byte clientBuf[64];
int clientCount = 0;

while(fh.available())
{
clientBuf[clientCount] = fh.read();
clientCount++;

if(clientCount > 63)
{
dclient.write((const uint8_t *)clientBuf, 64);
clientCount = 0;
}
}
if(clientCount > 0) dclient.write((const uint8_t *)clientBuf, clientCount);
dclient.stop();
Serial.println(F("Data disconnected"));
client.println();
if(!eRcv()) return 0;
client.println("QUIT");
if(!eRcv()) return 0;
client.stop();
Serial.println(F("Command disconnected"));
fh.close();
Serial.println(F("File 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"));
}
Click to rate this post!
[Total: 0 Average: 0]
1 Answers
christophe Staff answered 3 months ago

Hello Pekka, i think you can try ESP8266FtpServer.h library developed by David Paiva explained here

Click to rate this post!
[Total: 0 Average: 0]
DIY Projects