FTP upload file WF ETH

From openPicus Wiki
Jump to: navigation, search

Contents

What you are going to learn

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

  • connection to FTP Server
  • open a stream of data in write mode to FTP Server
  • send file content to 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 and write files of the "C:\FileZillaFolder" folder


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 Uploads "tutorial2.txt" file to FTP Server
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[257];
 
void FlyportTask()
{
    WFConnect(WF_DEFAULT);
    while (WFStatus != CONNECTED);
    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");
                    report = FTPConnect(&mySock, "INSERT HERE YOUR IP ADDRESS", "YOUR CONFIGURED PORT (default 21)", "flyport", "flypass");
                    if (report == FTP_CONNECTED)
                        UARTWrite(1, "Connection successfull\r\n");
                    else
                        UARTWrite(1, "Connection error\r\n");
                    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':
                    // Open Data Stream and store a new file 
                    // called "tutorial2.txt" on FTP Server storage folder
                    UARTWrite(1, "Open Stream in write (STOR) mode...\r\n");
                    report = FTPStreamOpen(mySock, "tutorial2.txt", STOR);
                    if(report == FTP_CONNECTED)
                        UARTWrite(1, "Stream opened!\r\n");
                    else
                        UARTWrite(1, "Errors opening stream in write mode\r\n");
 
                    // Write data to remote file
                    UARTWrite(1, "Sending data to stream\r\n");
                    if(FTPStreamStat() == FTP_STREAM_WRITING)
                    {
                        long wlen;
                        long toWriteLen = 65;
                        wlen = FTPStreamWrite("File STOR testing\nsecond file for the new file\ncreated by Flyport", toWriteLen);
                        sprintf(buff, "Written %ld BYTES!\r\n", wlen);
                        UARTWrite(1, buff);    
                    }
                    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