|
| 1 | +/* |
| 2 | + FTP client |
| 3 | +
|
| 4 | + This sketch connects to a FTP server through a MKR GSM 1400 board. |
| 5 | +
|
| 6 | + Circuit: |
| 7 | + * MKR GSM 1400 board |
| 8 | + * Antenna |
| 9 | + * SIM card with a data plan |
| 10 | +
|
| 11 | + created 21 Dec 2018 |
| 12 | + by Tryhus |
| 13 | +*/ |
| 14 | + |
| 15 | +// libraries |
| 16 | +#include <MKRGSM.h> |
| 17 | +#include <GSMFTP.h> |
| 18 | +#include <GSMFileSystem.h> |
| 19 | +#include "arduino_secrets.h" |
| 20 | + |
| 21 | +// Please enter your sensitive data in the Secret tab or arduino_secrets.h |
| 22 | +// PIN Number |
| 23 | +const char PINNUMBER[] = SECRET_PINNUMBER; |
| 24 | +// APN data |
| 25 | +const char GPRS_APN[] = SECRET_GPRS_APN; |
| 26 | +const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN; |
| 27 | +const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD; |
| 28 | + |
| 29 | +// initialize the library instance |
| 30 | +GSMFileSytem fileSystem; |
| 31 | +GSMFTP ftp; |
| 32 | +GPRS gprs; |
| 33 | +GSM gsmAccess; |
| 34 | + |
| 35 | +void setup() { |
| 36 | + // initialize serial communications and wait for port to open: |
| 37 | + Serial.begin(9600); |
| 38 | + while (!Serial) { |
| 39 | + ; // wait for serial port to connect. Needed for native USB port only |
| 40 | + } |
| 41 | + |
| 42 | + Serial.println("Starting Arduino FTP client."); |
| 43 | + // connection state |
| 44 | + bool connected = false; |
| 45 | + |
| 46 | + // After starting the modem with GSM.begin() |
| 47 | + // attach the shield to the GPRS network with the APN, login and password |
| 48 | + while (!connected) { |
| 49 | + if ((gsmAccess.begin(PINNUMBER) == GSM_READY) && |
| 50 | + (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) { |
| 51 | + connected = true; |
| 52 | + } else { |
| 53 | + Serial.println("Not connected"); |
| 54 | + delay(1000); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +void loop() { |
| 60 | + |
| 61 | + Serial.println("Connect to FTP server."); |
| 62 | + if (ftp.connect(SECRET_FTP_HOST, SECRET_FTP_USER, SECRET_FTP_PASSWORD) == false) { |
| 63 | + Serial.println("Failed to Connect to FTP server."); |
| 64 | + } |
| 65 | + |
| 66 | + Serial.println("Change of directory"); |
| 67 | + if (ftp.cd(SECRET_FTP_REMOTE_DIR) == false) { |
| 68 | + Serial.println("Failed to change of directory."); |
| 69 | + } |
| 70 | + |
| 71 | + Serial.print("Free space "); |
| 72 | + Serial.println(fileSystem.freeSpace()); |
| 73 | + |
| 74 | + Serial.println("Create remote file : test"); |
| 75 | + if (ftp.mkdir("test") == false) { |
| 76 | + Serial.println("Failed to create the file."); |
| 77 | + } |
| 78 | + |
| 79 | + Serial.println("Rename remote file : test to test2"); |
| 80 | + if (ftp.rename("test", "test2") == false) { |
| 81 | + Serial.println("Failed to rename the file."); |
| 82 | + } |
| 83 | + |
| 84 | + Serial.println("Write a binary file in local memory"); |
| 85 | + double valueWR = -12.5789876; |
| 86 | + double valueRD = 0; |
| 87 | + if (fileSystem.write("myFile", &valueWR, sizeof(valueWR)) == false) { |
| 88 | + Serial.println("Failed to write file"); |
| 89 | + } |
| 90 | + |
| 91 | + Serial.println("Send the file to the server"); |
| 92 | + if (ftp.upload("myFile", "myFileToServer") == false) { |
| 93 | + Serial.println("Failed to upload the file."); |
| 94 | + } |
| 95 | + |
| 96 | + Serial.println("Retreive the file from the server to local memory"); |
| 97 | + if (ftp.download("myFileToServer", "myFileToLocalMemory") == false) { |
| 98 | + Serial.println("Failed to download the file."); |
| 99 | + } |
| 100 | + |
| 101 | + Serial.println("Check that the original file is identical to the one that was received"); |
| 102 | + if (fileSystem.read("myFileToLocalMemory", &valueRD, sizeof(valueRD)) == false) { |
| 103 | + Serial.println("Failed to read file"); |
| 104 | + } |
| 105 | + else if (valueWR != valueRD) { |
| 106 | + Serial.println("Failed to read file, value is corrupted"); |
| 107 | + } |
| 108 | + |
| 109 | + Serial.print("Free space "); |
| 110 | + Serial.println(fileSystem.freeSpace()); |
| 111 | + |
| 112 | + Serial.println("Display local files"); |
| 113 | + if (fileSystem.ls(true) == false) { |
| 114 | + Serial.println("Failed to display local files"); |
| 115 | + } |
| 116 | + |
| 117 | + Serial.println("Remove local files"); |
| 118 | + for (int i = 0; i < fileSystem.fileCount(); ++i) { |
| 119 | + fileSystem.remove(fileSystem.file(i).name); |
| 120 | + } |
| 121 | + |
| 122 | + Serial.println("Display local files"); |
| 123 | + if (fileSystem.ls(true) == false) { |
| 124 | + Serial.println("Failed to display local files"); |
| 125 | + } |
| 126 | + |
| 127 | + Serial.println("Display remote files"); |
| 128 | + if (ftp.ls(true) == false) { |
| 129 | + Serial.println("Failed to display files."); |
| 130 | + } |
| 131 | + |
| 132 | + Serial.println("Delete the created files"); |
| 133 | + if (ftp.remove("test2") == false) { |
| 134 | + Serial.println("Failed to remove files : test2."); |
| 135 | + } |
| 136 | + if (ftp.remove("myFileToServer") == false) { |
| 137 | + Serial.println("Failed to remove files : myFileToServer."); |
| 138 | + } |
| 139 | + |
| 140 | + Serial.println("Display remote files"); |
| 141 | + if (ftp.ls(true) == false) { |
| 142 | + Serial.println("Failed to display files."); |
| 143 | + } |
| 144 | + |
| 145 | + Serial.println("Disconnect to FTP server"); |
| 146 | + if (ftp.disconnect() == false) { |
| 147 | + Serial.println("Failed to disconnect."); |
| 148 | + } |
| 149 | + |
| 150 | + for (;;) |
| 151 | + ; |
| 152 | +} |
| 153 | + |
0 commit comments