Append data to a remote file via FTP

From openPicus Wiki
Jump to: navigation, search

Contents

What you are going to learn

This tutorial shows how to send a file to 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 reads Analog Input 1 value. When 10 different samples are stored, data is written inside a file on GPRS transceiver internal flash memory. Then this file is sent to an FTP Server. File stored on remote FTP Server data will never be lost, since the write method used is "APPE" (add data to existing file, or create a new one in case it does not exist).
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).

Flyport PRO

Code example

Source code for taskFlyport.c:

#include "taskFlyport.h"
 
BOOL sampleData = TRUE;
int samplesCounter = 0;
BOOL uploadData = FALSE;
char sampleStr[100];
int actionState = 0; // 0: State Machine in idle state...
FTP_SOCKET ftpSocket;
 
void FlyportTask()
{
    vTaskDelay(20);
    int i;
    for(i = 0; i < 9; i++)
        ADCDetach(i);
    ADCAttach(2);    // Enable only Analog 2 channel (pin p5)
 
    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)
    {
	vTaskDelay(20);
	IOPut(p31, toggle);
 
	// Poll Button input state...
	if(IOGet(p9) == off)
	{
            UARTWrite(1, "Button Pressed!\r\n");
	    sampleData = TRUE;
	}
 
	switch(actionState)
	{
	    // Step #0: Prepare temperature CSV data
	    // sample up to 10 analog values (1 each button pressure),
	    // then upload to remote FTP
	    case 0:
	        if(sampleData == TRUE)
		{
		    // Reset Flag
		    sampleData = FALSE;
		    UARTWrite(1, "Sampling data...");
 
		    // Read Analog 1 value
		    int analogValue = ADCVal(1);
 
		    // Store it inside "sampleStr" char array
		    char msg[8];
		    sprintf(msg, "%d,", analogValue);
		    if(samplesCounter == 0)
			sprintf(sampleStr, "%s", msg);
		    else
			strcat(sampleStr, msg);
 
		    UARTWrite(1, "done!\r\n");	
 
		    // Increase samples counter
		    samplesCounter++;
 
		    // Choose State Machine next operation:
		    if(samplesCounter > 9)
		    {
			strcat(sampleStr, "\r\n");
			samplesCounter = 0; // Reset samples counter
			actionState++; // Go to next step 
		    }
		}
		break;
 
	    // Step #1: Write to local file
	    case 1:
	        UARTWrite(1, "Writing on Flash File \"templog.csv\"\r\n");
 
	        FSWrite("templog.csv", (BYTE*)sampleStr, strlen(sampleStr));
	        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 APN parameters
	    case 2:
	        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 = 2; // Repeat from case 2
		else
		    actionState++; // execute next step
		break;
 
	    // Step #3: Set FTP Server Configuration
	    case 3:
		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 #4: Append Data to FTP file
	    case 4:
	        UARTWrite(1, "Sending data to FTP: ");
	        FTPSend(&ftpSocket, "templog.csv", "/", "templog.csv", TRUE);
	        while(LastExecStat() == OP_EXECUTION)
	            vTaskDelay(20);
		// Choose next step:
		if(LastExecStat() != OP_SUCCESS)
		{
		    UARTWrite(1, "not sent!\r\nFlyport will try again soon\r\n");
		    actionState = 2; // Repeat from case 2
		}
		else
		{
		    UARTWrite(1, "data successfully sent!\r\n");
		    actionState = 0; // Restart application loop
		}
		break;
	}
    }
}


Flyport

Bill of Materials

  • 1x Flyport GPRS Module
  • 1x miniUSB programmer
  • 1x Power Supply
  • 1x Proto Nest Board
  • 1x Button


Schematic

ButtonInput p7 inup.png


Code example

Source code for taskFlyport.c:

#include "taskFlyport.h"
 
BOOL sampleData = TRUE;
int samplesCounter = 0;
BOOL uploadData = FALSE;
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 p7 as digital input with internal pullup resistor
    IOInit(p7, inup);
 
    while(1)
    {
	vTaskDelay(20);
	IOPut(p21, toggle);
 
	// Poll Button input state...
	if(IOGet(p7) == off)
	{
            UARTWrite(1, "Button Pressed!\r\n");
	    sampleData = TRUE;
	}
 
	switch(actionState)
	{
	    // Step #0: Prepare temperature CSV data
	    // sample up to 10 analog values (1 each button pressure),
	    // then upload to remote FTP
	    case 0:
	        if(sampleData == TRUE)
		{
		    // Reset Flag
		    sampleData = FALSE;
		    UARTWrite(1, "Sampling data...");
 
		    // Read Analog 1 value
		    int analogValue = ADCVal(1);
 
		    // Store it inside "sampleStr" char array
		    char msg[8];
		    sprintf(msg, "%d,", analogValue);
		    if(samplesCounter == 0)
			sprintf(sampleStr, "%s", msg);
		    else
			strcat(sampleStr, msg);
 
		    UARTWrite(1, "done!\r\n");	
 
		    // Increase samples counter
		    samplesCounter++;
 
		    // Choose State Machine next operation:
		    if(samplesCounter > 9)
		    {
			strcat(sampleStr, "\r\n");
			samplesCounter = 0; // Reset samples counter
			actionState++; // Go to next step 
		    }
		}
		break;
 
	    // Step #1: Write to local file
	    case 1:
	        UARTWrite(1, "Writing on Flash File \"templog.csv\"\r\n");
 
	        FSWrite("templog.csv", (BYTE*)sampleStr, strlen(sampleStr));
	        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 APN parameters
	    case 2:
	        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 = 2; // Repeat from case 2
		else
		    actionState++; // execute next step
		break;
 
	    // Step #3: Set FTP Server Configuration
	    case 3:
		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 #4: Append Data to FTP file
	    case 4:
	        UARTWrite(1, "Sending data to FTP: ");
	        FTPSend(&ftpSocket, "templog.csv", "/", "templog.csv", TRUE);
	        while(LastExecStat() == OP_EXECUTION)
	            vTaskDelay(20);
		// Choose next step:
		if(LastExecStat() != OP_SUCCESS)
		{
		    UARTWrite(1, "not sent!\r\nFlyport will try again soon\r\n");
		    actionState = 2; // Repeat from case 2
		}
		else
		{
		    UARTWrite(1, "data successfully sent!\r\n");
		    actionState = 0; // Restart application loop
		}
		break;
	}
    }
}
Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox