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:
Industrial Arduino Controller
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;
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
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.
Industrial Shields Arduino IDE Library - FTP Server