Industrial Shields Arduino IDE Library - FTP Server

How to send and receive files with Arduino Industrial Controller
August 29, 2019 by
Industrial Shields Arduino IDE Library - FTP Server
Serzh Ohanyan

Index

1. Introduction
2. Requirements
3. Arduino FTP library
4. Store Example
5. Retrieve Example
6. Take a look at the Ethernet range

Introduction

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

An FTP Server is commonly used to transfer 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 files 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 storing and retrieving 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 are available.

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

Ethernet Range >>>


​Search in our Blog

Industrial Shields Arduino IDE Library - FTP Server
Serzh Ohanyan August 29, 2019

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>