Saving Images to FTP: a tutorial for the SerialCam

From openPicus Wiki
Jump to: navigation, search

What you are going to learn

This tutorial will walk the user through setting up a SerialCam, and uploading the retrieved images to a remote FTP server. This tutorial is written for the Flyport Ethernet and Wifi, as the GPRS model uses a slightly different FTP library.

What you need

In order to follow along this tutorial you need

To setup a local FTP server we suggest the freely available Filezilla Server to be installed on your PC. The operation is explained at the related filezilla-project wiki page.

For further information on network settings you can follow this article. Please note that this guide is about allowing access from outside of your LAN. We suggest to use the Flyport connected to the same LAN of your FTP Server before fiddling 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 the "C:\FileZillaFolder" folder

This is where we the images generated by the camera will be placed.

The code

The code is straightforward modification of the Streamed version available on the SerialCam page.
First the FTP connection is estabilished, then write access is requested. If everything went correctly, the software than instructs the camera to grab a picture and starts streaming the image in chunks to send them to the FTP.

The software doesn't run in cycle for a few reasons:

  • the user would probably want to check the end result before capturing another image
  • by default Filezilla Server is configured to not automatically overwrite files with the same name

The latter means that if the software were to be run in cycle, it would be able to correctly save an image to the FTP only on the first attempt (if there wasn't already an image with the same filename) because the subsequent overwrites would be prevented by the server.

#include "taskFlyport.h"
#include "grovelib.h"
#include "serialCamera.h"
 
TCP_SOCKET mysock = INVALID_SOCKET; // Socket declared and initialized
int report;
unsigned int wlen;
#define MYLENGTH 128
char data[MYLENGTH];
char msg[500];
 
void FlyportTask()
{  
	DWORD time_before, time_after;
	vTaskDelay(100);
	UARTWrite(1,"Welcome to GROVE NEST example!\r\n");
 
	// GROVE board
	void *board = new(GroveNest);
 
	// Connection to Network
	#if defined (FLYPORT)
	WFConnect(WF_DEFAULT);
	while (WFStatus != CONNECTED);
	#endif
	while(!DHCPAssigned);
	vTaskDelay(25);
	UARTWrite(1,"Flyport connected... hello world!\r\n");
 
	int status;
 
	void *camera = new(SerialCam, 2); // Please choose an available UART module, starting from 2 up to 4
	attachToBoard(board,camera,DIG3);
	status = configure(camera);
	if(status == -1)
		UARTWrite(1,"Error...camera didn't work properly\n\r");
	else if(status == -2)
		UARTWrite(1,"Error...image saving on sd card failed\n\r");
	else if(status == -3)
		UARTWrite(1,"Function not defined\n\r");
	else if(!status)
		UARTWrite(1,"Ok...image saved on sd card\n\r");
 
	vTaskDelay(100);
 
	UARTWrite(1, "Connect socket to server\r\n");
	report = FTPConnect(&mysock, "ftp.myserver.com", "21", "user", "pass");
	if (report == FTP_CONNECTED)
	{
		UARTWrite(1, "Connection successfull\r\n");
		UARTWrite(1, "Open Stream in write (STOR) mode...\r\n");
		report = FTPStreamOpen(mysock, "myimage.jpg", STOR);
		if(report == FTP_CONNECTED)
		{
			UARTWrite(1, "Stream opened!\r\n");
			UARTWrite(1,"SNAPSHOT ....\n\r");
 
 
			set(camera,IMAGESIZE,SIZE640x480);
 
			time_before = TickGetDiv256();
			if(set(camera,TAKEPICTURE))
				UARTWrite(1,"Error...taking the snapshot\n\r");
			status = get(camera,SD,"my_image.jpg"); // nome del file dove salvare l'immagine
			if(status == -1)
				UARTWrite(1,"Error...camera didn't work properly\n\r"); // problemi nella trasmissione dell'immagine
			else if(status == -2)
				UARTWrite(1,"Error...image saving on sd card failed\n\r");
			else if(status == -3)
				UARTWrite(1,"Function not defined\n\r");
			else if(!status)
				UARTWrite(1,"Ok...image saved on sd card\n\r");
 
			time_after = TickGetDiv256();
 
			sprintf(msg,"Time to take shot: %lu\n",time_after - time_before);
			UARTWrite(1,msg);
 
			UARTWrite(1, "Sending data to stream\r\n");
 
			if(FTPStreamStat() == FTP_STREAM_WRITING)
			{
				int i,j;
				unsigned int size = get(camera,SIZE);
				if(!size)
					UARTWrite(1,"Error...image 1 size = 0\n\r");
				sprintf(msg,"Img size is: %u\n",size);
				UARTWrite(1,msg);
 
				unsigned int count = size / MYLENGTH;
				unsigned int index = 0;
				unsigned int tail = size%MYLENGTH;
 
				sprintf(msg,"Number of chunks: %u\nSize of last chunk: %u\n", count, tail);
				UARTWrite(1,msg);
 
				for(i=0;i<count;i++)
				{
					status = get(camera,STREAM,data,index,MYLENGTH);
					if(status == 0)
					{
						if(FTPStreamStat() == FTP_STREAM_WRITING)
						{
							wlen = FTPStreamWrite(data, MYLENGTH);
							sprintf(msg, "Written %u of %u bytes\r\n", index+MYLENGTH, size);
							UARTWrite(1, msg);
						}
						else
						{
							UARTWrite(1,"Error in the FTP stream, aborting...\n");
							break;
						}
					}
					else
					{
						UARTWrite(1,"Image 1 transmission errror\n\r");
						break;
					}
					index += MYLENGTH;
				}
				if(tail)
				{
					status = get(camera,STREAM,data,index,tail);
					if(status == 0)
					{   
						if(FTPStreamStat() == FTP_STREAM_WRITING)
						{
							wlen = FTPStreamWrite(data, tail);
							sprintf(msg, "Written %u bytes\r\n\nOperation completed successfully\n", wlen);
							UARTWrite(1, msg);
 
						}
						else
						{
							UARTWrite(1,"Error in the FTP stream, aborting...\n");
						}   
					}
					else
						UARTWrite(1,"Image 1 transmission errror tail\n\r");
				}				
			}
			FTPStreamClose();
 
		}
		else
			UARTWrite(1, "Errors opening stream in write mode\r\n");
 
		if(FTPisConn(mysock))
		{
			FTPClose(mysock);
			UARTWrite(1,"Closing FTP socket\n");
		}
	}
	else
		UARTWrite(1, "Connection error\r\n");
 
	while(1);
}
Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox