Browse our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria

Industrial Shields Arduino IDE Library - FTP Server

How to send and receive files with Arduino Industrial Controller

Industrial Shields Arduino IDE Library - FTP


Introduction

We've created a library that allow you to send/receive files to/from a FTP Server.

A FTP Server is commonly used to tansfer files between computer systems connected by a communication network. The basic mechanism as well as the set of protocol commands and responses have been defined in the context of a simplified architecture. 

This post explains how arduino-FTP library is made and how it works. First of all you have to include the library and create an FTP object:


Requirements

Download the library from github:
Arduino-FTP Library >>.


Industrial Arduino Controller

M-Duino PLC>>


Arduino FTP library

You can use any Client to send and receive file from an FTP Server. First of all you have to include the library and create an FTP object:


#include <FTP.h>
FTP ftp(ftpControl, ftpData);


Both ftpControl and ftpData are Client objects, like EthernetClient or other:


EthernetClient ftpControl;
EthernetClient ftpData;


The FTP Service is offered by the application layer of the TCP/IP network layer model to the user, normally using network port 20 or 21.
To connect to the FTP server you only have to set the server IP address, your user and your password. Optionally you can set the server TCP port, but the default one is 21:

ftp.connect(serverIP, user, password);


or in case of another TCP port (i. e. 6758);


ftp.connect(serverIP, 6758, user password);


When the connection is ready, it is possible to download files content from the FTP server:


char fileContent[512];
ftp.retrieve(fileName, fileContent, 512);


or to upload files to the FTP server:


const char *fileContent = "This is the new file content";
ftp.store(fileName, fileContent, strlen(fileContent));


You can see complete examples of store and retrieve files data in the examples directory of the github repository. 


Store Example

// FTP library example
// by Industrial Shields

#include <FTP.h>

#if defined(MDUINO_PLUS)
#include <Ethernet2.h>
#else
#include <Ethernet.h>
#endif

uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10, 10, 10, 6);
IPAddress namesServer(8, 8, 8, 8);
IPAddress gateway(10, 10, 10, 1);
IPAddress netmask(255, 255, 255, 0);

IPAddress server(192, 168, 1, 220);
const char *user = "YOUR-USER";
const char *pass = "YOUR-PASSWORD";
const char *fileName = "YOUR-FILE";

EthernetClient ftpControl;
EthernetClient ftpData;

FTP ftp(ftpControl, ftpData);

////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(9600UL);

  Ethernet.begin(mac, ip, namesServer, gateway, netmask);
  Serial.print("IP address: ");
  Serial.println(Ethernet.localIP());

  if (!ftp.connect(server, user, pass)) {
    Serial.println("Error connecting to FTP server");
    while (true);
  }

  Serial.println("You have 10 seconds to write something...");
  delay(10000UL);

  // Send the written content to the FTP file
  ftp.store(fileName, Serial);

  Serial.println("The written content is sent to the FTP file");
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  // Nothing to do
}


Retrieve Example

// FTP library example
// by Industrial Shields

#include <FTP.h>

#if defined(MDUINO_PLUS)
#include <Ethernet2.h>
#else
#include <Ethernet.h>
#endif

uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10, 10, 10, 6);
IPAddress namesServer(8, 8, 8, 8);
IPAddress gateway(10, 10, 10, 1);
IPAddress netmask(255, 255, 255, 0);

IPAddress server(192, 168, 1, 220);
const char *user = "YOUR-USER";
const char *pass = "YOUR-PASSWORD";
const char *fileName = "YOUR-FILE";

EthernetClient ftpControl;
EthernetClient ftpData;

FTP ftp(ftpControl, ftpData);

////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(9600UL);

  Ethernet.begin(mac, ip, namesServer, gateway, netmask);
  Serial.print("IP address: ");
  Serial.println(Ethernet.localIP());

  if (!ftp.connect(server, user, pass)) {
    Serial.println("Error connecting to FTP server");
    while (true);
  }

  // Print FTP file content to Serial
  Serial.println("FTP file content: ");
  size_t len = ftp.retrieve(fileName, Serial);

  Serial.println();
  Serial.print("FTP file size: ");
  Serial.println(len);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  // Nothing to do
}

Take a look at the Ethernet range

Arduino Mega for Industrial Solutions

Ethernet Programmable Logic Controller range


Ethernet Programmable Logic Controller range


Up to 58 IOs available.

Multiple protocols and  other options at your disposal like:
LoRa - Long Range solution.
WiFi - Wireless option.
GPRS - If there's not option to have a physical support.
DALI - Lighting protocol for specific solutions.

Ethernet Range >>


Do you want more information?

Just fill the form!

Tell me more!