FTP download file WF ETH

From openPicus Wiki
Jump to: navigation, search

Contents

What you are going to learn

This tutorial shows how to download a file from a FTP Server in 4 simple steps.
Features shown are:

  • connection to FTP Server
  • get file size from FTP Server
  • receive file content from FTP Server


Introduction to FTP Library

FTP library reference is here


How to use it

Setup a FTP Server on your PC

First of all install Filezilla Server on your PC. The operation is explained at related filezilla-project wiki page.

For further information on Network settings you can follow this article. Please note that this guide is about to allow access from outside of your LAN. We suggest to use Flyport connected to the same LAN of your FTP Server before to mess with Firewalls, NAT, Service Providers, etc...


After you correctly setup your PC with Filezilla Server, some more operations are required:

  • create a user (we will use username "flyport" and password "flypass")
  • create a folder "FileZillaFolder" inside C: disk
  • grant to this user permission to access to the "C:\FileZillaFolder" folder
  • create a text file named "tutorial.txt" and write inside it the string "Hello Flyport! This is file content...Bye bye!!"


Firmware Development on IDE

Create a new project. Change IP Address and Port, using your Filezilla settings. If needed change also username and password with your FTP Server settings.


NOTE: If you would like to connect to a remote FTP Server, remember to configure your access point settings using IDE Wizard.



Compile and download code to the device.
Open the Serial Monitor utility on the IDE to send commands to Flyport. Serial monitor also provides feedback information about operation in progress.

Type the character reported above to start related operation:

Character Operation
0 Starts connection with FTP Server
1 Checks if Flyport is connected to FTP Server
2 Downloads "tutorial.txt" file from FTP Server until the end of file
3 Closes the connection


Code example

Source code for taskFlyport.c:

#include "taskFlyport.h"
 
TCP_SOCKET mySock = INVALID_SOCKET; // Socket declared and initialized
int report;
BOOL ftpConnected = FALSE;
char cmd;
char buff[260];
 
void FlyportTask()
{
    #if defined (FLYPORT_WF)
    WFConnect(WF_DEFAULT);
    while (WFGetStat() != CONNECTED);
    #elif defined (FLYPORT_ETH)
    while(!MACLinked);
    #endif
    vTaskDelay(20);
    UARTWrite(1,"Flyport connected... hello world!\r\n");
    while(1)
    {
        if(UARTBufferSize(1) > 0)
        {
            UARTRead(1, &cmd, 1);
            UARTFlush(1);
 
            switch(cmd)
            {
                case '0':
                    UARTWrite(1, "Connect socket to server\r\n");
                    // This function connects to FTP Server command socket
                    report = FTPConnect(&mySock, "INSERT HERE YOUR IP ADDRESS", "YOUR CONFIGURED PORT (default 21)", "flyport", "flypass");
                    if (report == FTP_CONNECTED)
                        UARTWrite(1, "Connection successfull");
                    else
                        UARTWrite(1, "Connection error");
 
                    break;
 
                case '1':
                    UARTWrite(1, "Check connection\r\n");
                    ftpConnected = FTPisConn(mySock);
 
                    if(ftpConnected == TRUE)
                        UARTWrite(1, "Socket connected!\r\n");
                    else
                        UARTWrite(1, "Socket not connected!!\r\n");
                    break;
 
                case '2':
                    UARTWrite(1, "Receiving data from stream...\r\n");
                    char dbgMess[50];
                    // using command socket request actual file size of "tutorial.txt" file
                    // stored on FTP Server storage folder
                    long fileSize = FTPFileSize(mySock, "tutorial.txt");
                    long toReadLen = fileSize;
                    if(toReadLen > 256)
                        toReadLen = 256;
                    long counter = 0;
 
                    // Open Data Stream and request tutorial.txt file
                    report = FTPStreamOpen(mySock, "tutorial.txt", RETR);
 
                    // Read the first packet of data
                    long rlen = FTPStreamRead(buff, toReadLen, 5);
                    fileSize -= rlen;
                    counter += rlen;
 
                    // Add termitation character to buffer
                    buff[rlen] = '\0';
                    // Print on Serial Monitor what was read
                    UARTWrite(1, buff);
 
                    // Check if data stream is in reading mode yet
                    if(FTPStreamStat() == FTP_STREAM_READING)
                    {
                        while(fileSize > 0)
                        {
                            // until file is not read to the end, continue
                            toReadLen = fileSize;
                            if(toReadLen > 256)
                                toReadLen = 256;
 
                            rlen = FTPStreamRead(buff, toReadLen, 5);
                            if(rlen < 0)
                                break;
 
                            buff[rlen] = '\0';
                            UARTWrite(1, buff);    
 
                            counter += rlen;
                            fileSize -= rlen;
                        }
 
                        // Print information about amount of Bytes read
                        sprintf(dbgMess, "\r\nTotal Read: %ld BYTES\r\n", counter);
                        UARTWrite(1, dbgMess);
                    }
                    else
                    {
                        UARTWrite(1, "Stream not opened correctly\r\n");
                    }
                    break;
 
                case '3':
                    UARTWrite(1, "Stream & connection Close\r\n");
                    // Close Data Stream (if opened yet)
                    FTPStreamClose();
                    // Close connection to FTP Server
                    FTPClose(mySock);
                    break;
            }
        }
    }
}
Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox