Basic Ethernet2 Server no-go

Just tried basic Ethernet server code using Ethernet arduino library, but I had to change one to Ethernet 2, because I think I have a W5500 chip.

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet2.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>


byte mac[] = {0xAD, 0xBE, 0xDE};
IPAddress ip(192, 168, 1, 177);
EthernetServer server(80);

void setup()
{
 Serial.begin(9600);
 while (!Serial)
 {
  ;
 }
 Ethernet.begin(mac, ip);
 server.begin();
 Serial.print("server is at ");
 Serial.println(Ethernet.localIP());
}


void loop()
{
 EthernetClient client = server.available();
 if (client)
 {
  Serial.println("new client");
 }
}

And here’s what it had to say about it.

C:\Program Files\Arduino\libraries\Ethernet\src\EthernetClient.h:8:7: warning: type of ‘vtable for EthernetClient’ does not match original declaration

class EthernetClient : public Client {

^

C:\Program Files\Arduino\libraries\Ethernet\src\EthernetClient.h:8:7: warning: type of ‘vtable for EthernetClient’ does not match original declaration

class EthernetClient : public Client {

^

C:\Users\Notshowingmynamelol\Documents\Arduino\libraries\Ethernet2\src\EthernetClient.h:8:7: note: previously declared here

class EthernetClient : public Client {

^

C:\Program Files\Arduino\libraries\Ethernet\src\Ethernet.cpp:6:9: warning: type of ‘_state’ does not match original declaration

uint8_t EthernetClass::_state[MAX_SOCK_NUM] = {

^

C:\Users\Notshowingmynamelol\Documents\Arduino\libraries\Ethernet2\src\Ethernet2.cpp:15:9: note: previously declared here

uint8_t EthernetClass::_state[MAX_SOCK_NUM] = { 0, };

^

C:\Program Files\Arduino\libraries\Ethernet\src\Ethernet.cpp:8:10: warning: type of ‘_server_port’ does not match original declaration

uint16_t EthernetClass::_server_port[MAX_SOCK_NUM] = {

^

C:\Program Files\Arduino\libraries\Ethernet\src\Ethernet.h:19:19: warning: type of ‘_server_port’ does not match original declaration

static uint16_t _server_port[MAX_SOCK_NUM];

^

C:\Program Files\Arduino\libraries\Ethernet\src\Ethernet.h:19:19: warning: type of ‘_server_port’ does not match original declaration

static uint16_t _server_port[MAX_SOCK_NUM];

^

C:\Program Files\Arduino\libraries\Ethernet\src\Ethernet.h:19:19: warning: type of ‘_server_port’ does not match original declaration

static uint16_t _server_port[MAX_SOCK_NUM];

^

C:\Users\Notshowingmynamelol\Documents\Arduino\libraries\Ethernet2\src\Ethernet2.cpp:16:10: note: previously declared here

uint16_t EthernetClass::_server_port[MAX_SOCK_NUM] = { 0, };

^

C:\Program Files\Arduino\libraries\Ethernet\src\Ethernet.cpp:136:15: warning: type of ‘Ethernet’ does not match original declaration

EthernetClass Ethernet;

^

C:\Program Files\Arduino\libraries\Ethernet\src\Ethernet.h:39:22: warning: type of ‘Ethernet’ does not match original declaration

extern EthernetClass Ethernet;

^

C:\Program Files\Arduino\libraries\Ethernet\src\Ethernet.h:39:22: warning: type of ‘Ethernet’ does not match original declaration

extern EthernetClass Ethernet;

^

C:\Users\Notshowingmynamelol\Documents\Arduino\libraries\Ethernet2\src\Ethernet2.cpp:209:15: note: previously declared here

EthernetClass Ethernet;

^

C:\Users\Notshowingmynamelol\Documents\Arduino\libraries\Ethernet2\src\EthernetUdp2.h:50:7: warning: type ‘struct EthernetUDP’ violates one definition rule

class EthernetUDP : public UDP {

^

C:\Program Files\Arduino\libraries\Ethernet\src\EthernetUdp.h:44:7: note: a type with the same name but different layout is defined in another translation unit

class EthernetUDP : public UDP {

^

C:\Users\Notshowingmynamelol\Documents\Arduino\libraries\Ethernet2\src/EthernetUdp2.h:50:7: warning: type ‘struct EthernetUDP’ violates one definition rule

class EthernetUDP : public UDP {

^

C:\Program Files\Arduino\libraries\Ethernet\src\EthernetUdp.h:44:7: note: a type with the same name but different layout is defined in another translation unit

class EthernetUDP : public UDP {

^

collect2.exe: error: ld returned 1 exit status

Multiple libraries were found for “Dhcp.h”
Used: C:\Users\Notshowingmynamelol\Documents\Arduino\libraries\Ethernet2
Not used: C:\Program Files\Arduino\libraries\Ethernet
Not used: C:\Program Files\Arduino\libraries\Ethernet
Not used: C:\Program Files\Arduino\libraries\Ethernet
Not used: C:\Program Files\Arduino\libraries\Ethernet
Using library Ethernet2 at version 1.0.4 in folder: C:\Users\Notshowingmynamelol\Documents\Arduino\libraries\Ethernet2
Using library SPI at version 1.0 in folder: C:\Users\Notshowingmynamelol\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.19\libraries\SPI
Using library Ethernet at version 1.1.2 in folder: C:\Program Files\Arduino\libraries\Ethernet
exit status 1
Error compiling for board Arduino/Genuino Uno

Had to cut off most of the error messages, but here it is.

Your code has unnecessary #include directives that caused the Ethernet library to be brought in as well as the Ethernet2 library, which caused conflicts. The solution is to remove the unnecessary #include directives, leaving only the one for Ethernet2.h:

#include <Ethernet2.h>


byte mac[] = {0xAD, 0xBE, 0xDE};
IPAddress ip(192, 168, 1, 177);
EthernetServer server(80);

void setup()
{
 Serial.begin(9600);
 while (!Serial)
 {
  ;
 }
 Ethernet.begin(mac, ip);
 server.begin();
 Serial.print("server is at ");
 Serial.println(Ethernet.localIP());
}


void loop()
{
 EthernetClient client = server.available();
 if (client)
 {
  Serial.println("new client");
 }
}

Thanks! I just used the directives that appeared when I selected "Ethernet", or In "https://www.arduino.cc/en/Tutorial/WebServer", or in my Arduino programming book. Sure I don't need SPI.h or Etheretclient.h if I expand my code?

As long as you're using Arduino IDE 1.6.6 or newer you only need to include Ethernet2.h. It was necessary to include those other files with Arduino 1.6.5 and older so that's why you see those lines still in the examples and tutorials for backwards compatibility but the Arduino IDE has been improved since then.

Thanks, but I had a problem with my serial monitor displaying 0.0.0.0, so I checked and found out I ended up getting a W5100 chip :o :frowning: So I just need Etherent.h now? Does W5100 even have a client function?

I changed my code to this:

#include <Ethernet.h>



int Svr=1;
byte mac[] = {0xAD, 0xBE, 0xDE};
IPAddress ip(129, 186, 1, 172);
EthernetServer server(80);


void setup()
{
 Serial.begin(9600);
 while (!Serial)
 {
  ;
 }
 Ethernet.begin(mac, ip);
 server.begin();
}

void loop()
{
 Serial.print("server1 is at ");
 Serial.println(Ethernet.localIP());
 EthernetClient client = server.available();
 while (!client)
 {
  ;
 }
 Serial.println("new client");
 Svr++;
 Serial.print("server");
 Serial.print(Svr);
 Serial.print(" is at ");
 Serial.println(Ethernet.localIP());
}

The compiler had no errors, but the Serial Monitor had this:

server1 is at 129.186.1.172

despite me going to “http://129.186.1.172/”, which displayed this:

This site can’t be reached

129.186.1.172 took too long to respond.
ERR_CONNECTION_TIMED_OUT