Download a file from FTP remote server

From openPicus Wiki
Jump to: navigation, search

Contents

What you are going to learn

This tutorial shows how to download a file from a remote FTP Server using Flyport GPRS.

Introduction to data mode

To perfom any type of data communication, APN parameters should be configured. It is suggested to set APN parameters everytime Flyport GPRS resets (or wakes up from hibernation/sleep)
Connection functions (like TCP/HTTP Client connection, FTP Client file exchange, email sending) are available only when APN is set correctly,

NOTE: APN (Access Point Name) parameters are specific for each mobile operator. Please, ask to your SIM Card provider.

How to use it

Everytime the button is pressed Flyport GPRS starts "file.txt" download. Once file is correctly downloaded, the first 100 BYTEs of data are read and printed to IDE Serial Monitor.

This tutorial shows how to create a firmware with "state machine" execution. The state machine keeps track of your code execution. In this way, if problems occurs, Flyport can automatically start from a certain state. If the the file exchange process go wrong for any reason (could be an incoming call or network disconnection etc...) the firmware restarts from APN setup phase.

NOTE: The following example should be configured with your own parameters (APN and email).

Code example

Source code for taskFlyport.c:

#include "taskFlyport.h"
 
char sampleStr[100];
int actionState = 0; // 0: State Machine in idle state...
FTP_SOCKET ftpSocket;
 
void FlyportTask()
{
    vTaskDelay(20);
    UARTWrite(1,"Flyport Task Started...\r\n");
 
    //Wait for GSM Connection successfull
    do
    {
        UpdateConnStatus();
        while(LastExecStat() == OP_EXECUTION);
    }while((LastConnStatus() != REG_SUCCESS)&&(LastConnStatus() != ROAMING));
 
    vTaskDelay(20);
    UARTWrite(1,"Flyport registered on network!\r\n");
 
    // Initialize p9 as digital input with internal pullup resistor
    IOInit(p9, inup);
 
    while(1)
    {
        switch(actionState)
        {
            case 0:
                // Poll Button input state...
                if(IOGet(p9) == off)
                {
                    _dbgwrite("Starting FTP file download...\r\n");
                    actionState++; // Go to next step     
                }
                break;
 
            // Step #1: Set APN parameters
            case 1:
                UARTWrite(1, "Setup APN params\r\n");
                APNConfig("apn.domain.fly","usernameAPN","passwordAPN",DYNAMIC_IP,DYNAMIC_IP,DYNAMIC_IP);
                vTaskDelay(20);
                while(LastExecStat() == OP_EXECUTION)
                    vTaskDelay(20);
                // Choose next step:
                if(LastExecStat() != OP_SUCCESS)
                    actionState = 1; // Repeat from case 1
                else
                    actionState++; // execute next step
                break;
 
            // Step #2: Set FTP Server Configuration
            case 2:
                UARTWrite(1, "Setup FTP configuration\r\n");
                FTPConfig(&ftpSocket, "your_ftp_site", "your_user_id", "your_password", 21);
                while(LastExecStat() == OP_EXECUTION)
                    vTaskDelay(20);
                // Choose next step:
                if(LastExecStat() != OP_SUCCESS)
                    actionState = 2; // Repeat from case 2
                else
                    actionState++; // execute next step
                break;
 
            // Step #3: Receive file from FTP
            case 3:
                UARTWrite(1, "Receiving data from FTP: ");
                FTPReceive(&ftpSocket, "file.txt", "/", "file.txt");
                while(LastExecStat() == OP_EXECUTION)
                    vTaskDelay(20);
                // Choose next step:
                if(LastExecStat() != OP_SUCCESS)
                {
                    UARTWrite(1, "not received!\r\nFlyport will try again soon\r\n");
                    actionState = 2; // Repeat from case 2
                }
                else
                {
                    UARTWrite(1, "data successfully received!\r\n");
                    actionState++; // Read file content from FlashFileSystem
                }
                break;
 
            // Step #4: Read File Content
            case 4:
                UARTWrite(1, "Reading data from file: ");
                FSRead("file.txt", (BYTE*)sampleStr, 100);
                while(LastExecStat() == OP_EXECUTION)
                    vTaskDelay(20);
                // Choose next step:
                if(LastExecStat() != OP_SUCCESS)
                {
                    UARTWrite(1, "no data read!\r\nFlyport will be try again soon\r\n");
                    actionState = 2;
                }
                else
                {
                    _dbgwrite(sampleStr);
                    _dbgwrite("\r\n");
                    actionState = 0;
                    _dbgwrite("\r\nOPERATION COMPLETED!\r\n");
                }
                break;
        }
    }
}


Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox