client.connect fail

Helo,
I wanted connect my arduino to a raspberry pi but I have some problems.

At first I connected the arduino directly to the raspberry and everthing was fine.

Then I pluged the raspberry and the arduino in a switch. The config of the switch is alright and I have only one network. The result is the Arduino can´t connect with the raspberry.

Pleas help :frowning:

thx

#include <SPI.h>
#include <Ethernet.h>             // library for ethernet functions

int T1=1;
int T2=2;
//ETHERNET-SETTINGS
byte mac[]     = { 0x5D, 0xA2, 0xFA, 0x2D, 0x76, 0x7C };    // MAC-Adresse des Arduino
byte ip[]      = { 172, 20, 10, 20 };                     // IP-Adresse des Arduino
byte gateway[] = { 10, 0, 0, 138 };                    // Gateway
byte subnet[]  = { 255, 255, 255, 0 };                    // SubNet
byte server[]  = { 172, 20, 10, 2 };                     // IP-Adresse des Servers
 
EthernetClient client;
char host[]    = "172.20.10.2";                      // Domain
char url[]     = "/SaveTempToMySQL.php"; // Pfad zur PHP-Datei
char key[]     = "123456";                     // Kennwort aus PHP-Datei
char c;                                                     // Variable für Rückgabe des Servers
 
long Interval  = 10;                                        // Upload-Interval in Minuten

void setup()
{
  delay(1000);
 
 
  Serial.begin(9600);
 
  Ethernet.begin(mac, ip, gateway, subnet);
  

}
 
void loop()
{
  
 delay(30000);
 Serial.println("Test");
  



  if (client.connect(server, 80)) // Verbindung zum Server aufbauen
  {
    Serial.println("Verbunden, Sende Daten...");
    
    client.print("GET ");
   // Serial.println("GET ");
    client.print(url);
    //Serial.println(url);
    client.print("?T1=");
    //Serial.print("?T1=");
    client.print(T1);
    //Serial.println(T1);
    client.print("&T2=");
    //Serial.print("&T2=");
    client.print(T2);
    //Serial.println(T2);
    client.print("&key=");
    client.print(key);
    //Serial.print("&key=" + String(key));
    client.println(" HTTP/1.1");
    //Serial.println(" HTTP/1.1");
   client.print("Host: ");
    //Serial.print("Host: " + String(host));
    client.print("10.0.0.17");
   
    //Serial.println();
    client.println("User-Agent: Arduino");
    //Serial.println("User-Agent: Arduino");
    client.println("Accept: text/html");
    //Serial.println("Connection: close");
    client.println();
    client.stop();
    client.flush();
    //Serial.println();
  }
else{

Serial.println("fail");
}

}

connected the arduino directly to the raspberry and everthing was fine.

so which one was the DNS server? did they both have static IP addresses in the same subnet?

when I read this

byte ip[]      = { 172, 20, 10, 20 };    // IP-Adresse des Arduino
byte gateway[] = { 10, 0, 0, 138 };             // Gateway
byte subnet[]  = { 255, 255, 255, 0 };        // SubNet
byte server[]  = { 172, 20, 10, 2 };             // IP-Adresse des Servers

I find the gateway a bit weird unless you have some good routing capabilities in your home. I would probably expect your gateway to be 172.20.10.1 which would make more sense

can you check on your computer - if it is connected to the same network - what the gateway is??

Hy,

In the sketch on the arduino is the Gateway 172.20.10.1. I forgot to change it in the text above.

I suppose it isn´t necessry to have a Gateway when all clients are in the same Network.

And I also have Static addresses on both devices, Arduino and raspbarry (server).

I also tried to connect the both devices without a connecten to the router.

Only the raspberry and the arduino where plugged in the Switch.

thx

Some switches, especially TP-Link, have a problem with the Arduino's ethernet shield.

Don't know if this is part of your problem, but it may be eventually. Your call to Ethernet.begin is not correct.

// replace this
  Ethernet.begin(mac, ip, gateway, subnet);
// with this
  Ethernet.begin(mac, ip, gateway, gateway, subnet);

diode1990:
I suppose it isn´t necessry to have a Gateway when all clients are in the same Network.

is that a switch or a hub or a router?

Thx

Should I use this : Ethernet.begin(mac, ip, gateway, gateway, subnet);

with 2x gateway?

You also think it could be because of the switch. I also thought the switch dropped the packages.

Do you know a switch witch can handle Arduino packages.

I used a mikrotik router with switch funktion

:slight_smile:

You are fine with MikroTik. That is what I use. I've never had a problem with my ethernet shield. Don't know what model router you are using, but they can be a bit touchy to set up. I don't normally recommend them for a beginner. I use various RB433 models. RB433, RB433AH and RB433UAH.

Yes, two gateway parameters in the begin call. The first is the dns server ip, and the second is the gateway ip. If you need a "real" dns server, replace the first parameter with your dns server ip.

Thx

Actually it should it work, when I plug the arduino in a Switch????

I will try some other switches.

diode1990:
Actually it should it work, when I plug the arduino in a Switch????

Yes. It will work with a MikroTik router set up as a switch, or Netgear, or D-Link. I've tested them. I was helping users on the MikroTik forum for years with setup and routing questions.

I checked the mikrotik config, and I suppose the config isn´t allright the ports are only bridged.

Could that be a reason?

Could be. If you want to set it up as a switch, that is done in "/interface ethernet" and "/interface ethernet switch". Assign the network settings to the master interface, and all others (slave ports) should have the master-port set to the master interface. As I recall, there is a "switch1" as the default.

Thanks a lot

Great suppport