LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Get Text-Files (xxxx.txt) Data from Arduino via FTP

Hello Forum,

 

I've an Arduino running as an datalogger and I want to Display the data in LabVIEW. On my Arduino runs a FTP-Server that can send data (Text-Files) to my computerdisc. I'm using FileZilla-Server as FTP-Server.

Now I want to get the datafiles without FileZilla just with LabVIEW. 

 

Did anyone has an example for providing a FTP-Server in LabVIEW. My idea was, that I sent the filename via TCP to my Arduino and I get the datafile back. 

 

Before I put the Code togther I'll want to test it alone with LabVIEW. 

 

Thanks in advance.

 

Arduino FTP Code

/*
   FTP passive client for IDE v1.0.1 and w5100/w5200
   Posted October 2012 by SurferTim
   Modified 6 June 2015 by SurferTim
*/

#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
// comment out next line to write to SD from FTP server
#define FTPWRITE

// this must be unique
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67 };  

// change to your network settings
IPAddress ip( 192, 168, 2, 2 );    
IPAddress gateway( 192, 168, 2, 1 );
IPAddress subnet( 255, 255, 255, 0 );

// change to your server
IPAddress server( 1, 2, 3, 4 );

EthernetClient client;
EthernetClient dclient;

char outBuf[128];
char outCount;

// change fileName to your file (8.3 format!)
char fileName[13] = "test.txt";

void setup()
{
  Serial.begin(9600);

  digitalWrite(10,HIGH);

  if(SD.begin(4) == 0)
  {
    Serial.println(F("SD init fail"));          
  }

  Ethernet.begin(mac, ip, gateway, gateway, subnet); 
  digitalWrite(10,HIGH);
  delay(2000);
  Serial.println(F("Ready. Press f or r"));
}

void loop()
{
  byte inChar;

  inChar = Serial.read();

  if(inChar == 'f')
  {
    if(doFTP()) Serial.println(F("FTP OK"));
    else Serial.println(F("FTP FAIL"));
  }

  if(inChar == 'r')
  {
    readSD();    
  }

}

File fh;

byte doFTP()
{
#ifdef FTPWRITE
  fh = SD.open(fileName,FILE_READ);
#else
  SD.remove(fileName);
  fh = SD.open(fileName,FILE_WRITE);
#endif

  if(!fh)
  {
    Serial.println(F("SD open fail"));
    return 0;    
  }

#ifndef FTPWRITE  
  if(!fh.seek(0))
  {
    Serial.println(F("Rewind fail"));
    fh.close();
    return 0;    
  }
#endif

  Serial.println(F("SD opened"));

  if (client.connect(server,21)) {
    Serial.println(F("Command connected"));
  } 
  else {
    fh.close();
    Serial.println(F("Command connection failed"));
    return 0;
  }

  if(!eRcv()) return 0;

  client.println(F("USER myuser"));

  if(!eRcv()) return 0;

  client.println(F("PASS mypassword"));

  if(!eRcv()) return 0;

  client.println(F("SYST"));

  if(!eRcv()) return 0;

  client.println(F("Type I"));

  if(!eRcv()) return 0;

  client.println(F("PASV"));

  if(!eRcv()) return 0;

  char *tStr = strtok(outBuf,"(,");
  int array_pasv[6];
  for ( int i = 0; i < 6; i++) {
    tStr = strtok(NULL,"(,");
    array_pasv[i] = atoi(tStr);
    if(tStr == NULL)
    {
      Serial.println(F("Bad PASV Answer"));    

    }
  }

  unsigned int hiPort,loPort;

  hiPort = array_pasv[4] << 8;
  loPort = array_pasv[5] & 255;

  Serial.print(F("Data port: "));
  hiPort = hiPort | loPort;
  Serial.println(hiPort);

  if (dclient.connect(server,hiPort)) {
    Serial.println(F("Data connected"));
  } 
  else {
    Serial.println(F("Data connection failed"));
    client.stop();
    fh.close();
    return 0;
  }

#ifdef FTPWRITE 
  client.print(F("STOR "));
  client.println(fileName);
#else
  client.print(F("RETR "));
  client.println(fileName);
#endif

  if(!eRcv())
  {
    dclient.stop();
    return 0;
  }

#ifdef FTPWRITE
  Serial.println(F("Writing"));

  byte clientBuf[64];
  int clientCount = 0;

  while(fh.available())
  {
    clientBuf[clientCount] = fh.read();
    clientCount++;

    if(clientCount > 63)
    {
      dclient.write(clientBuf,64);
      clientCount = 0;
    }
  }

  if(clientCount > 0) dclient.write(clientBuf,clientCount);

#else
  while(dclient.connected())
  {
    while(dclient.available())
    {
      char c = dclient.read();
      fh.write(c);      
      Serial.write(c); 
    }
  }
#endif

  dclient.stop();
  Serial.println(F("Data disconnected"));

  if(!eRcv()) return 0;

  client.println(F("QUIT"));

  if(!eRcv()) return 0;

  client.stop();
  Serial.println(F("Command disconnected"));

  fh.close();
  Serial.println(F("SD closed"));
  return 1;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;

  while(!client.available()) delay(1);

  respCode = client.peek();

  outCount = 0;

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);

    if(outCount < 127)
    {
      outBuf[outCount] = thisByte;
      outCount++;      
      outBuf[outCount] = 0;
    }
  }

  if(respCode >= '4')
  {
    efail();
    return 0;  
  }

  return 1;
}


void efail()
{
  byte thisByte = 0;

  client.println(F("QUIT"));

  while(!client.available()) delay(1);

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }

  client.stop();
  Serial.println(F("Command disconnected"));
  fh.close();
  Serial.println(F("SD closed"));
}

void readSD()
{
  fh = SD.open(fileName,FILE_READ);

  if(!fh)
  {
    Serial.println(F("SD open fail"));
    return;    
  }

  while(fh.available())
  {
    Serial.write(fh.read());
  }

  fh.close();
}

TCP LabVIEW -> Arduino 

 

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67}; //adresse mac de la carte
IPAddress ip( 169, 254, 155, 2 );
IPAddress gateway( 169, 254, 155, 1 );
IPAddress subnet( 255, 255, 255, 0 );
String msg=" ";
char thisChar;
const int led_rouge = 17;
EthernetServer server(3363);

void setup() {
  // initialize the ethernet device
  pinMode(led_rouge, OUTPUT);
  Ethernet.begin(mac, ip);
  // start listening for clients
  server.begin();
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
    digitalWrite(led_rouge, LOW);
  }

  Serial.print("Chat server address:");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // wait for a new client:
  EthernetClient client = server.available();

  // when the client sends the first byte, say hello:
  if (client.connected()) {
    if (client) {
      if (client.available() != 0) {
        thisChar = client.read();
        msg = String(msg + thisChar);
      }
      Serial.print(msg);

      if (msg == "49") {
        digitalWrite(led_rouge, HIGH);
        delay(1000);
      }
      else
      {
        digitalWrite(led_rouge, LOW);
      }
    }
  }
}

 

0 Kudos
Message 1 of 9
(6,347 Views)

There are FTP VIs in the "Data Communication -> Protocols -> FTP" palette.

 

0xDEAD

0 Kudos
Message 2 of 9
(6,338 Views)

Yes I know, but I was Looking for an running example. 

0 Kudos
Message 3 of 9
(6,284 Views)

I think it would be more sensible to set the arduino up as an FTP server and create a tool in LabVIEW that acts as an FTP client. LabVIEW would send the PUT/GET requests to instigate the data transfer and the arduino just reacts to those requests. In this case, the user interface in LabVIEW is the decision maker, not the arduino code.

 

0xDEAD

 

 

0 Kudos
Message 4 of 9
(6,275 Views)

That would be nice. But I can't configer the Arduino as a FTP-Server. 

I found a way with FileZilla. I'll try it...

 

https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000019LizSAE&l=de-DE

0 Kudos
Message 5 of 9
(6,269 Views)

@Herrx wrote:

Hello Forum,

 

I've an Arduino running as an datalogger and I want to Display the data in LabVIEW. On my Arduino runs a FTP-Server that can send data (Text-Files) to my computerdisc.

 



@Herrx wrote:

That would be nice. But I can't configer the Arduino as a FTP-Server. 

I found a way with FileZilla. I'll try it...


That is a bit confusing...

0 Kudos
Message 6 of 9
(6,262 Views)

Ahhh ok. Very attentive. the Arduino runs as an FTP-Clinet and LabVIEW should be the Server or FileZilla.

0 Kudos
Message 7 of 9
(6,259 Views)

@Herrx wrote:

Ahhh ok. Very attentive. the Arduino runs as an FTP-Clinet and LabVIEW should be the Server or FileZilla.


We'll adapt Smiley Very Happy.

 

I don't think there are OoTB LabVIEW FTP servers available. Usually, external programs are used I imagine, as they do a pretty good job. If you need some kind of event when the file is uploaded, I'd look into .NET FileWatcher event. If you really want to make your own FTP server, I'd also look into .NET. I've once make a REST server with .NET, and it wasn't that hard.

0 Kudos
Message 8 of 9
(6,255 Views)

Hi Herrx,

 

there is no FTP-Server that can be hosted from LabVIEW directly. Its questionable if this makes sense at all, since Windows itself offers you the feature of hosting a FTP-Server:

https://www.windowscentral.com/how-set-ftp-server-windows-10

 

Routing the FTP through LabVIEW would only make sense if you want to adjust some access rights, but propably you can do that from LabVIEW using WinAPI-Calls.

 

I hope this helps, let me know if there are further questions.

 

Cheers,
Jan Göbel
Staff Technical Support Engineer

0 Kudos
Message 9 of 9
(6,249 Views)