Ethernet not connecting with Linux server

I got a solar logger running and I want to send the data to my Network hdd (iomega home media).
I use the following code:

void NasClient()
{
  if (client.connect(server,80)) {
    //delay(500);
    Serial.println("Connected");
    client.print("GET /cgi-bin/Arduino.php?vaerdi=");
    client.print(vermogen);
    client.println(" HTTP/1.1");
    client.println("Host: Iomega-09681e");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();
    //Serial.println();
  }

  else
  {
    Serial.println("Connection unsuccesfull");
  }

  //stop client
  client.stop();
  while(client.status() != 0)
  {
    delay(5);
  }
}

When I look at wireshark my arduino ask who is at 192.168.1.13 (IP off the NAS) and get's a reply.
But after that the arduino is not sending any of the [SYN] [ACK] messages. And no data.
The client status is 23 when it says connected.
I tried without the host, user agent and close but that doesn't help.
My NAS is trying to connect and is sending out the [SYN][ACK] messages.

When I try to run the PHP script on my web browser it does work.

Any ideas why the arduino will not connect to the linux server?

Maybe if you read and display the response, that would give you a hint.

  if (client.connect(server,80)) {
    Serial.println("Connected");
    client.print("GET /cgi-bin/Arduino.php?vaerdi=");
    client.print(vermogen);
    client.println(" HTTP/1.1");
    client.println("Host: Iomega-09681e");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();

    // read server response until it closes the connection
    // that is the signal it is finished sending packets.
    while(client.connected()) {
      while(client.available()) Serial.write(client.read());
    }
    client.stop();
  }

Well gave it a try but no response.
I added the wireshark screenshot.
It doesn’t show the who is at (ip) but it does do that.

I guess asking you to post the entire sketch is out of the question?

well i can do but i got a little further. it looks like the port from the arduino switches every time.
And my NAS only accepts some ports.

I tries it a couple of time and it got it.

Is there a way to specify the outgoing ports?

As for the code, this is my test code stript out from the whole code:

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


// input:
const int kwhpuls = 2;    // the pin that the pushbutton is attached to


// variabele
boolean readkwhpuls = false;
//int lastButtonState = LOW;
//int buttonState;
int vermogen = 0;
int maxvermogen = 0;
byte countNr =  1;
long kwh;// = 68120;// begin stand
int kwhDag;
String writebuffer = "";

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long starttijdtussenpuls = 0;
unsigned long starttijdtussenpuls2 = 0;
unsigned long tussentijd = 0;
unsigned long tussentijd2 = 0;
unsigned long slowLoop = 0;

//ethernet variables
byte mac[] = {  
  0x91, 0xA2, 0xDA, 0x00, 0xEF, 0xE7 };
unsigned int localPort = 8888;      // local port to listen for UDP packets
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

byte index = 0;
byte counter = 0;
boolean match = true;
int Packets = 0;
byte server[] = { 
  192, 168, 1, 13  };
EthernetClient client;

void setup() {
  //pinMode(kwhpuls, INPUT);
  pinMode(10, OUTPUT);


  // start serial port:
  Serial.begin(9600);
  Serial.println("Start");

  // start Ethernet and UDP
  if (Ethernet.begin(mac) == 0) {
    // set fixed ip adress:
    IPAddress ip(192,168,1,50);
    IPAddress gateway(192,168,1, 254);
    IPAddress subnet(255, 255, 255, 0);
    Ethernet.begin(mac,ip);    

  }

  delay(5000);

  //Serial.println(freeRam());
  Serial.println("Up");
  delay(1000);

  attachInterrupt(0,Pulse,RISING);

}

void Pulse ()
{
  readkwhpuls = true;
}

void loop () {

  //readkwhpuls = digitalRead(kwhpuls);
  /*
  if (readkwhpuls != lastButtonState) {
   // reset the debouncing timer
   lastDebounceTime = millis();
   
   if ((millis() - lastDebounceTime) < 20) {
   // whatever the reading is at, it's been there for longer
   // than the debounce delay, so take it as the actual current state:
   buttonState = readkwhpuls;
   }
   */

  if (readkwhpuls == true)
  {
    Serial.println("interrupt");
    lastDebounceTime = millis();
    //kijk of de pulse is geweest als ja dan:
    //if (buttonState == HIGH) {
    //kijk of de teller al loopt, als deze nog niet loopt start hem
    kwh ++;
    kwhDag ++;
    if (countNr == 1) {
      countNr = 2 ;
      //zet de begin tijd op 0
      starttijdtussenpuls = millis();
      if (starttijdtussenpuls2 != 0){
        tussentijd2 = millis() - starttijdtussenpuls2;
        vermogen = (1800000UL/tussentijd2);
        //log op sd kaart
        //sdkaart ();
        NasClient();
        // laat het scherm weten dat hij moet updaten
      }

    }
    //als de teller loopt zet deze uit en reken de tijd tussen de pulsen en daarbij behoorende vermogen

    else if (countNr == 2)
    {
      countNr = 1;
      starttijdtussenpuls2 = millis();
      tussentijd = millis() - starttijdtussenpuls;
      vermogen = (1800000UL/tussentijd);
      //log op sd kaart
      //sdkaart ();
      NasClient();
      // laat het scherm weten dat hij moet updaten
    }
    // save the reading.  Next time through the loop,
    // it'll be the lastButtonState:
    //lastButtonState = readkwhpuls;
    readkwhpuls = false;
  }


}

void NasClient()
{
  (client.connect(server, 80));
    // read server response until it closes the connection
    // that is the signal it is finished sending packets.

    Serial.println(client.status());
    Serial.println("Connected");
    client.print("GET http://192.168.1.13/cgi-bin/Arduino.php?vaerdi=TEST");
    client.println(" HTTP/1.1");
    client.println("Host: 192.168.1.13");
    //client.println("User-Agent: arduino-ethernet");
    //client.println("Connection: close");
    client.println();
    Serial.println("data send");
    /*
    while(client.connected()) {
      while(client.available()) Serial.write(client.read());
    }
    */
  //stop client
  client.stop();
  while(client.status() != 0)
  {
    delay(5);
  }
}


/*
int freeRam () {
 extern int __heap_start, *__brkval; 
 int v; 
 return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
 }
 
 */

Try this code. Change the server ip to yours, but let it download your home page, not the cgi-bin path.

If that works, then try your php page.

well i changed the IP and the message to my PHP message and that seems to work.
Only the arduino is crashing every time, but that is something to fix tommorrow.

I will post my working sketch later.

Got it working.
Buffer was to small so it crashed :cold_sweat:
Here is my code:

oid NasClient()
{
  (client.connect(server, 80));
    // read server response until it closes the connection
    // that is the signal it is finished sending packets.
    int Tijd = 123;
    int Datum=123;
    char LogData[128]; <--- buffer was to small

    sprintf(LogData,"Tijd=%d&Datum=%d&Ver=%d&Kwh=%d",Tijd,Datum,vermogen,kwh);
    Serial.println(client.status());
    Serial.println("Connected");
    Serial.println(LogData);
    client.write("GET http://192.168.1.13/cgi-bin/Arduino.php?");
    client.write(LogData);
    client.write(" HTTP/1.0\r\n\r\n");
    Serial.println("data send");
    
  //stop client
  client.stop();
  while(client.status() != 0)
  {
    delay(5);
  }
}

And here my PHP

<html>
	<?php


		$Tijd = $_GET['Tijd'];
		$Datum = $_GET['Datum'];
		$Vermogen = $_GET['Ver'];
		$Kwh = $_GET['Kwh'];
		$Temp = $_GET['Temp'];

		$date= date("Y-m-d");


		$myFile = "/nethdd/ActiveFolders/ftp/Logger/".$date.".txt";


		$fh = fopen($myFile, 'a') or die("can't open file");


		fwrite($fh, $Tijd);
		fwrite($fh, "\t");
		fwrite($fh, $Datum);
		fwrite($fh, "\t");
		fwrite($fh, $Vermogen);
		fwrite($fh, "\t");
		fwrite($fh, $Kwh);
		fwrite($fh, "\t");
		fwrite($fh, $Temp);
		fwrite($fh, "\r\n");

		fclose($fh);
	?>
</html>

When i’m done with my project i will post it all

Thanks for the help