Transfer jpg image from SD to FTP Server

I am trying to transfer a very small image (jpg) from an SD card to an FTP server using a Photon. I can see that the FTP server does accept the connection, however, its log file shows the following error:

I am using the ParticleFtpClient library and the following code:

  myFile.open(filename, O_READ);
  int data;
  if (FTPclient.connect(FTPserver,FTPport)){
      while ((data = myFile.read()) >= 0){
          FTPclient.write(data);
      }
      FTPclient.stop();
  }

I have looked at a lot of weblinks but was unable to find out the problem and would appreciate some help.

@Jimmie, you defined data as a int which will read two bytes but only send one. You should define data as either a char or uint8_t. Try that to see if it works.

Thank you @peekay123.

I tried unint8_t, but it is giving “‘unint8_t’ does not name a type”

Oh blast! I misspelled it! It should be uint8_t !!!

Thanks again @peekay123. I tried both but it did not work.

Sorry for wasting your time as I am realizing that the way I am doing it should not work. I am connecting to an FTP Server and then copying data to it without even telling it the name of the file to store it in.

The code above is not mine and was taken from http://github.com/OUSmartInfrastructure/PhotonBikeSensor/tree/master/BikeSensorv3

When I tried using the ParticleFtpClient library, and using exact code like the example, I am getting the following error on open “no matching function for call to ‘particleftpclient::ParticleFtpClient::open(String&, int&, int&)’”

Also, the Web IDE goes wild and starts saying that it is missing .h files even though I checked that the Particle library is included along with its #include statement.

Is there anywhere a simple example that shows how to transfer a small jpg file from an SD card to an FTP server?

Thanks again.

We're trying to get images uploaded to an FTP server also.

Just having the #include statement added is not enough to pull the library into the IDE I think.

You may have to add these extra .h and .cpp tabs to the build IDE to get it to compile correctly so be sure to give that a try and report back.

1 Like

I solved the problem by doing the following:

  1. Do not use the built-in ParticleFtpClient library.

  2. Import the ParticleFtpClient.cpp and ParticleFtpClient.h files
    (I do not know how to attach cpp and h files but I copied those from the Particle FTPFileSender library)

  3. Use this code in the body

        #include "ParticleFtpClient.h"
        #include <time.h>

        using namespace particleftpclient;

        String hostname = "server.dyndns.biz";
        String username = "test";
        String password = "test";
        int port = 21;

        int timeout = 5;

        ParticleFtpClient ftp = ParticleFtpClient();
  1. Use the following code to download jpg file:
      if (ftp.open(hostname, port, timeout)) {
      ftp.user(username);
      ftp.pass(password);
      ftp.type("I");
      ftp.stor(filename);
      while ((data = myFile.read()) >= 0) {
        ftp.data.write(data);
      }
      long int sendTime = millis() - now;
      ftp.data.flush();
      ftp.quit();
      //-------------------
           
      Particle.publish("Picture Sent", String(filename) + "," + String(sendTime));
      } 
      else
      {
        Particle.publish("FTP Error", "Cannot login");
      }

That said, I still think there is a bug in Web IDE for the following reasons:

  1. Sometimes deleting a library deleting the library below it too.
  2. When a new file is created and contents added, the same contents initlaly appear in both .cpp and .h.
1 Like

@Jimmie Thanks for the tip!

@ScruffR in the part of the send data code shown below:

while ((data = myFile.read()) &gt;= 0) {
        ftp.data.write(data);
      }
[/quote]

This pulls data from the SD card in chunks as you already know, but can’t we change this to pull the JPG image data chunks from the variable that holds the picture image that’s created in your uCAM3 library?

You could try it.
Currently I’ve set up a test server for @Vitesze to direct the TCP transfer to and wait for his reply.

I could try but I have no idea how it would or could work which is why I asked you.

Conceptually, it seems the only difference is where the bytes are being pulled from, the Data stored on an SD card vs Data stored in a variable in the Photons memory. But maybe it’s not that simple? No idea really.

So how is FTP different than the Server setup you’re talking about for @Vitesze ? Is it an FTP server or something different?

One “major” difference I’d anticipate is the timing with the rather slow uCam compared to an SD as source. This may or may not cause the FTP connection to time out and the code above does not take that into account (AFAICT).

The server I set up is running the NodeJS service and a rudimentary website to download the images.
I can PM you the details too.

1 Like

@Jimmie, it should now be possible to import the library the normal way since 0.0.3 just got published by @jychuah

1 Like

Thank you @ScruffR. Glad to hear of the update.

Thanks also to @jychuah.