Ethernet.begin (mac, ip) issues, LinkOFF error, Connection not established

I've been trying to program one arduino as the server and another one as a client.

For the server, when I use Ethernet.begin (mac, ip), Ethernet.linkStatus ( ) returns LinkOFF. And consequently there's no connection.
On the client side, where Ethernet.begin (mac) is used, Ethernet.linkStatus ( ) does not return any errors.

I was told to update my library from Ethernet.h to Ethernet2.h. But then I got errors that functions - Ethernet.hardwareStatus ( ), Ethernet.linkStatus ( ) and the condition if(server) is not defined. I used Ethernet2.h regardless, and removed all these constructs from my code.

PS

  • My Server IP is unique on the network
  • The port I'm using is free
  • The MAC address is unique and correct
  • I've double, triple, quadruple checked all the connections
  • When I was using Ethernet.h, Ethernet.hardwareStatus ( ) detected the W5500 chip on both server and client side.
  • Ethernet.linkStatus ( ) fails wherever Ethernet.begin (mac, ip) is used (In all sample sketches from Arduino as well)

This is the server code:
[I've tried to use server.accept ( ) instead of server.available ( ), but with Ethernet2.h that also gave me an error]

#include <Ethernet2.h>
#include <SPI.h>

byte mac[] = { 0xA8 , 0x61 , 0x0A , 0xAE , 0x76 , 0x23 };                            
IPAddress ip(10,16,60,201);                                                              //defined static IP 

EthernetServer server (1033);                                                          //defined own port number

void setup() {
    
     Serial.begin(9600);     
     Serial.println("\nIn Setup");

     Ethernet.begin(mac,ip);
    Serial.println("\n\nEthernet.begin( ) may have passed");
      
    server.begin();                                                                  //INITIALISE SERVER
    Serial.println("\n\nPerhaps the server is listening");
}

void loop() {
     
     EthernetClient client = server.available();                                    //maybe use server accept to get a client cause the client might not have data ready?
     
     if (client.connected( )) {
         Serial.println("\nClient is connected"); 
     }
     else {
        Serial.println("\nClient not connected"); 
     }
     delay (30000);
     delay (30000);
     delay (30000);
     delay (30000);
     client.stop( );
     
}

And here's the client code:

#include <Ethernet2.h>
#include <SPI.h>


byte mac[] = { 0xA8 , 0x61 , 0x9A , 0xAE , 0x89 , 0x25 };                           //MAC address from sticker, except last hex value
IPAddress ServerIP(10,16,60,201);                                                   //defined static IP of server

EthernetClient client;


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

     pinMode(30, OUTPUT);
     pinMode(33, OUTPUT);
    
    
    if(Ethernet.begin(mac)==1) {                                                    //ETHERNET INITIALISATION TEST
          Serial.println("Ethernet successfully initialised");
    }
    else {
          Serial.println("Ethernet initialisation failed");
    }
 
   
}

void loop() {
      client.connect(ServerIP, 1033);                                               //CONNECT TO SERVER AND CHECK CONNECTION
    delay(1000); 
    if (client.connected()){             
        digitalWrite(30, HIGH);                                                     //BLUE LED -- STATUSLED 30
        delay(10000);
        digitalWrite(30, LOW);         
    }
    else { 
        digitalWrite(33, HIGH);                                                     //RED LED -- STATUSLED 33
        delay(10000);
        digitalWrite(33, LOW);         
    }

            
}

What am I missing? What's going wrong?
I have considered that there might be a synchronization issue, but it seems that the only way to confirm this is to make it work.

Ethernet2 s obsolete. Ethernet is updated.

test linkStatus after a delay

  if (Ethernet.linkStatus() == LinkOFF) {
    delay(500);
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
  }

for begin(mac) there is DHCP communication so it creates the delay

1 Like

The server side code neither works with Ethernet2.h nor with Ethernet.h.
There are no LinkOFF issues in the client code where Ethernet.begin (mac) is used. The LinkOFF issues only occurs where Ethernet.begin(mac, ip) is used.
Even though I can confirm that begin(mac, ip) does initialise the MAC and IP correctly.

What does you hardware setup look like? Arduinos, shields, LAN switch etc

This is a rough representation of the setup.
The shield is Ethernet v2

Does the client reply to ping?

If you upload the server sketch to the client, will the same error appear?

The server sketch uploaded to the client arduino fails as well.
I only have two ethernet cables at the moment, so I rearranged the network a bit :

I got the IP from the client through localIP( ) and pinged it. The client responded to the ping.

yes

2 Likes

Maybe you got the wrong syntax in the server sketch:

Should be

IPAddress ip[] = { 10,16,60,201 }; 

according to the Ethernet documentation

it is the right syntax

Ah so begin(mac) automatically creates the delay cause of DHCP and with begin(mac, ip) it needs to be introduced manually?

What I have used is a constructor for IPAddress - Ethernet - IPAddress() - Arduino Reference

just an alternative for byte ip [ ] = { }

Thank you! linkStatus ( ) is passing now. But I'm still not able to connect to the client.

This is my server loop and I replaced server.available ( ) with server.accept ( ), still no luck.

void loop() {
     
     EthernetClient client = server.accept();                                    //maybe use server accept to get a client cause the client might not have data ready?
     
     if (client.connected( )) {
         Serial.println("\nClient is connected"); 
     }
     else {
        Serial.println("\nClient not connected"); 
     }
     delay (30000);
     delay (30000);
    
     client.stop( );
     
}

Do you have any suggestions for this?

void loop() {
     
     EthernetClient client = server.accept();                                    //maybe use server accept to get a client cause the client might not have data ready?
     
     if (client.connected( )) {
         Serial.println("\nClient is connected"); 
     }
     else {
        Serial.println("\nClient not connected"); 
     }
     delay (30000);
     delay (30000);
    
     client.stop( );
     
}

Is the server listening also during those 2 delay(30000) ? If not, the client got a very tiny time window to connect. If server is listening, what are the chances for a connection timeout to occur during the whole 1 minute spent in delay? You should use millis() instead of delay.

yes

how do you test it?

Fancy that.. something delay doesn't have authority over.

the TCP/IP stack is in this case in the Ethernet chip W5500 firmware

I'm testing the connection with if (client.connected ( )) on both the server and client side. On the server side I have serial outputs and on the client side status LEDs

Edit - I am now testing the return value of client.connect (serverIP, port) on the client side and it's returning 0 everytime. I'm not sure what to make of that

the webclient and webserver examples work?