arduino-libraries / MKRGSM

GNU Lesser General Public License v2.1
55 stars 51 forks source link

Problem with FTP keep getting Failed to start FTP transfer #89

Closed etuds123 closed 5 years ago

etuds123 commented 5 years ago

Hi there,

First I would like to thank you for this amazing library.

As the title says, I'm getting problem transferring data from my SD card to my server.

My code :

/*
  FTP client

 This sketch connects to a FTP server through a MKR GSM 1400 board.

 Circuit:
 * MKR GSM 1400 board
 * Antenna
 * SIM card with a data plan

 created 21 Dec 2018
 by Tryhus
*/

// libraries
#include <MKRGSM.h>
#include <GSMFTP.h>
#include <GSMFileSystem.h>

#include <SPI.h>
#include <SD.h>

#include "arduino_secrets.h" 

// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[] = SECRET_PINNUMBER;
// APN data
const char GPRS_APN[] = SECRET_GPRS_APN;
const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;

//this file must be present in the remote directory SECRET_FTP_REMOTE_DIR
const String c_downloadFileName = "downloadFile";

// initialize the library instance
GSMFTP ftp;
GPRS gprs;
GSM gsmAccess;

void setup() {
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Starting Arduino FTP client.");
  // connection state
  bool connected = false;

  // After starting the modem with GSM.begin()
  // attach the shield to the GPRS network with the APN, login and password
  while (!connected) {
    if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
      (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
      connected = true;
    }
    else {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
}

void loop() {

  GSMFTPElem remoteFile;

  test("Connect to FTP server",ftp.connect(SECRET_FTP_HOST, SECRET_FTP_USER, SECRET_FTP_PASSWORD, SECRET_FTP_PORT));
  test("Change current remote directory",ftp.cd(SECRET_FTP_REMOTE_DIR));
  test("Display remote files",ftp.ls(remoteFile, true));

  sendSDFile("test.txt", "datalogInFTPServer.txt", 1024);

  for (;;)
    ;
}

bool test(const String& msg, bool function)
{
  if (function == true) {
    Serial.print("OK - ");
  }
  else {
    Serial.print("ERROR - ");
  }
  Serial.println(msg);

  return function;
}

bool sendSDFile(const String& SDfileName, const String& FTPfileName, uint16_t packetSize)
{
  bool ok = true;
  int res = 0;
  uint32_t remainingBytes = 0;
  File file;

  char* packet = new char[packetSize];
  if (packet == nullptr) {
    Serial.println("Allocation error");
    return false;
  }

  file = SD.open(SDfileName);
  if (file == false) {
    Serial.println("Failed to open SD file");
    goto Err;
  }

  remainingBytes = file.size();

  //Start FTP transfer in stream mode

  if (ftp.streamInStart(FTPfileName) == false) {
    Serial.println("Failed to start FTP transfer");
    goto Err;
  }

  while (remainingBytes > 0) {
    uint32_t bytes = (remainingBytes >= packetSize) ? packetSize : remainingBytes;

    //read a packet of data
    if (file.readBytes(packet, bytes) != bytes) {
      Serial.println("Failed to read SD file");
      goto Err;
    }

    //send the packet to the FTP server
    if (ftp.streamOut(packet, bytes) == false) {
      Serial.println("Failed to send data to FTP server");
      goto Err;
    }
    remainingBytes -= bytes;
  }

  //wait for the transfer to be completed
  while (res == 0) {
    res = ftp.streamOutReady();
  }

  if (res != 1) {
    Serial.println("Failed to send data to FTP server");
    goto Err;
  }

  goto Out;

Err:
  ok = false;
Out:
  file.close();
  if (packet != nullptr) {
    delete[] packet;
  }
  return ok;
}

the Output :

Starting Arduino FTP client.
Initializing SD card...OK - Connect to FTP server
OK - Change current remote directory
drwx--x--- 6 717968 48 4096 May  9 13:46 .
drwx--x--- 6 717968 48 4096 May  9 13:46 ..
-rw-r--r-- 1 717968 717968 128000 May  8 16:11 FileToStream
-rw-r--r-- 1 717968 717968 128 May  8 16:05 myFile.txt
-rw-r--r-- 1 717968 717968 0 May  8 11:53 new_file
drwxr-xr-x 2 717968 717968 6 May  8 11:42 op
drwx--x--- 11 717968 48 4096 May  8 15:56 public_html
drwx--x--- 2 717968 717968 4096 Jul 16  2018 tmp
OK - Display remote files
Failed to start FTP transfer

As the output shows, the Arduino connect successfully to the server via FTP but it always say "Failed to start FTP transfer".

sandeepmistry commented 5 years ago

This library currently does not support FTP, however there is a pull request open for this - I suggest you comment there if that's what you are using: https://github.com/arduino-libraries/MKRGSM/pull/73