Introduction
In this post, we are going to send the saved information on an SD in an industrial PLC to an 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.Â
In this example, we will save information about our inputs into de SD card of our industrial Arduino, and we will send the information to an FTP server once every minute.
Previous reading
We recommend that you read the following blogs in order to understand the program of this blog. We used the following blog posts to do this example:
Requirements
- Download the FTP Library from Github:Â Arduino-FTP
- Download the Filter Library from Github: Filter
- Our M-Duino PLC:Â M-Duino Ethernet PLC.
You must also configure the RTC system to be able of knowing the date of the saved information. We suggest that you read the next blog to configure the RTC system before continuing this example:
- RTC on M-Duino:Â RTC test on M-Duino PLUS version
Code Example
/*
 * This example program saves the analog value of the input I0.12 into the SD card every second
 * Once per minute, send the information through FTP connection to the server and start
 * again the process
 */
// Library includes #include <Filter.h> #include <SD.h> #include <FTP.h> #include <RTC.h> #if defined(MDUINO_PLUS) #include <Ethernet2.h> #else #include <Ethernet.h> #endif // Set Ethernet properties of our PLC uint8_t mac[] = { 0xDE, 0xAD, 0x4E, 0x55, 0xFE, 0xED }; IPAddress ip(192, 168, 1, 2); IPAddress netmask(255, 255, 255, 0); IPAddress server(192, 168, 1, 1); // Define the variables for FTP management EthernetClient ftpControl; EthernetClient ftpData; FTP ftp(ftpControl, ftpData); // Set the user and password of FTP server const char *user = "anonymous"; const char *pass = "industrialShields"; const char *fileName = "FILE_NAME"; // Variable to save the hour and second int savedHour; int savedSecond; // Variables to manage the SD card Sd2Card card; SdVolume volume; // Variable for the analog filter AnalogFilter<5, 2> aFilter; // Setup function void setup() { // Set the speed of the serial port Serial.begin(9600UL); // Configure the ethernet protocol in our PLC Ethernet.begin(mac, ip, netmask); // Try to connect to the ftp server if (!ftp.connect(server, user, pass)) { // Connection was wrong Serial.println("Unable to connect to the FTP server"); while (true) ; } // Check and save the RTC hour if (!RTC.read()) { // RTC system detected an error Serial.println("Error detected in the RTC system"); while(true) ; } else { // RTC system is OK. Get the hour savedHour = RTC.getMinute(); savedSecond = RTC.getSecond(); } // Init the SD Card if (!SD.begin(53)) { Serial.println("Card failed, or not present"); while(true) ; } } // Loop function void loop() { // Capture the value of the analog input int input = analogRead(I0_12); int filtered = aFilter.update(input); // Check if the RTC detected a new hour if (RTC.read()) { // Do this process each second if (savedSecond != RTC.getSecond()) { savedSecond = RTC.getSecond(); logToSd(filtered); Serial.println(analogRead(filtered)); } // Do this process each hour if (savedHour != RTC.getMinute()) { savedHour = RTC.getMinute(); sendToFTP(); SD.remove("datalog.txt"); Serial.println("Saved completed"); } } else { // RTC system detected an error while (true) ; } } // Function to save information to SD void logToSd(int value) { File dataFile = SD.open("datalog.txt", FILE_WRITE); if (dataFile) { Serial.println("Saving to SD"); dataFile.println(value); dataFile.close(); } else { Serial.println("Error opening datalog.txt"); while (true) ; } } // Function to send the SD information through FTP void sendToFTP(void) { // Open the SD file File dataFile = SD.open("datalog.txt"); // Check if the file was succesfully open if (dataFile) { Serial.println("Sending trough FTP connection"); ftp.store("datalog.txt", dataFile); dataFile.close(); } else { Serial.println("Error opening datalog.txt"); while (true) ; } }
Sending SD information from PLC Arduino through FTP