Arduino to FTP : Protocol mismatch.

Hello All,

I am working on a project with an Automation Direct Open Source PLC P1AM-100, which used the arduino IDE to program. The goal of the project is to operate my sytem and collect data, then transfer that data to an FTP server(on a raspberry Pi) and post process it with python libraries. I have successfully communicated with the FTP server using WinSCP but when I try my PLC it throws an error:

SSH-2.0-OpenSSH_7.9p1 Raspbian-10+deb10u2
Protocol mismatch.

The FTP code is a copy of (Arduino Playground - FTP) with the doFTP function triggered my the end of my system sequence instead of letters.

SSH-2.0-OpenSSH_7.9p1 Raspbian-10+deb10u2
Protocol mismatch.

That sounds like you're not using an FTP server but an SSH server. An Arduino isn't able to do the SSH protocol. WinSCP is an FTP and sFTP client. Although the two variants have almost the same name technically they have almost nothing in common. One is an old protocol from the early Internet days while the other is a file transfer over SSH that more or less emulates the user interface of older FTP clients.

The FTP code is a copy of (Arduino Playground - FTP) with the doFTP function triggered my the end of my system sequence instead of letters.

You're sure? A copy of it with no changes? In this case I have serious doubts that you got above error message.

I too thought the response was strange. See my code below.

#include <Ethernet.h> //Load Ethernet Library
#include <EthernetUdp.h> //Load UDP Library
#include <SPI.h> //Load the SPI Library
#include <P1AM.h>
#include <P1_HSC.h>
#include <SD.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define FTPWRITE


IPAddress server(10, 225,*, *);
const char *user = "pi";
const char *pass = "caire";
// change fileName to your file (8.3 format!)
char fileName[13] = "test.txt";
char outBuf[128];
char outCount;

EthernetClient client;
EthernetClient dclient;

File myFile;
int Valve;
int Line_Purge_Time = 5000;
int O2PurgRate = 7;
int O2PURGE_TIME = 10;
int res = 50;
int After_Purge_Settle_Time = 20000;
float BackPRate = 0.9;
int NumCycles = 4;
String Name = "";

byte mac[] = {*,*,*,*,*,*}; //Assign a mac address
IPAddress ip(10,225,*,*); //Assign my IP adress
unsigned int localPort = 5000; //Assign a Port to talk over
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
String datReq; //String for our data
int packetSize; //Size of Packet
EthernetUDP Udp; //Define UDP Object



/*
 * FTP Code Below
 * 
 * 
 */

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

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

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

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

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

  if(!eRcv()) return 0;

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

  if(!eRcv()) return 0;

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

  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();
    myFile.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(myFile.available())
  {
    clientBuf[clientCount] = myFile.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();
      myFile.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"));

  myFile.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"));
  myFile.close();
  Serial.println(F("SD closed"));
}

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

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

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

  myFile.close();
}

if (client.connect(server,22)) {
Serial.println(F(“Command connected”));
}

Port 22 is the SSH listening port. FTP is on port 21