Ethernet.h library with ESP32? Compile error

Good day, everyone.

I am trying to connect and use the W5500 Ethernet module to an ESP32 to use a wired connection instead of a wifi one.

I have tried several solutions including the following:
ESP32_W5500_NTP_CLIENT
UIPEthernet

However as I was not able to get these to even compile, I reverted back to the Arduino provided example





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

// network configuration.  gateway and subnet are optional.

 // the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
//the IP address for the shield:
byte ip[] = { 10, 0, 0, 177 };    
// the router's gateway address:
byte gateway[] = { 10, 0, 0, 1 };
// the subnet:
byte subnet[] = { 255, 255, 0, 0 };

// telnet defaults to port 23
EthernetServer server = EthernetServer(23);

void setup()
{
  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);

  // start listening for clients
  server.begin();
}

void loop()
{
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client == true) {
    // read bytes from the incoming client and write them back
    // to any clients connected to the server:
    server.write(client.read());
  }
}

This was a straight copy paste of the example code. It works on an arduino Uno but does not on an ESP32.

This is the compile error. I am able to push simpler sketches including the WIFI one and Blink.

ESP_ETH2:14:42: error: invalid cast to abstract class type 'EthernetServer'
 EthernetServer server = EthernetServer(23);
                                          ^
In file included from C:\Users\owner\Documents\Arduino\ESP_ETH2\ESP_ETH2.ino:2:
C:\Program Files (x86)\Arduino\libraries\Ethernet\src/Ethernet.h:253:7: note:   because the following virtual functions are pure within 'EthernetServer':
 class EthernetServer : public Server {
       ^~~~~~~~~~~~~~
In file included from C:\Users\owner\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.1\cores\esp32/Arduino.h:165,
                 from sketch\ESP_ETH2.ino.cpp:1:
C:\Users\owner\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.1\cores\esp32/Server.h:28:18: note: 	'virtual void Server::begin(uint16_t)'
     virtual void begin(uint16_t port=0) =0;
                  ^~~~~
ESP_ETH2:14:16: error: cannot declare variable 'server' to be of abstract type 'EthernetServer'
 EthernetServer server = EthernetServer(23);
                ^~~~~~
exit status 1
invalid cast to abstract class type 'EthernetServer'

change class EthernetServer : public Server
to class EthernetServer : public Stream
in Ethernet.h

I think you are supposed to give the DNS Server address between the IP address and the gateway router address. If you don't, the gateway router address is used as the DNS Server address and the subnet mask is used as the gateway router address.

If I do this, will it change that definition for all other boards too? If so, is there a way to maybe make a copy of the library so that this change is only used on ESP32 boards?

Yeah I found it weird, the documentation page says for Ethernet it's one way but for Server it's another way.

What is the difference between the server and ethernet documentation? They're both on the same Ethernet doc page.

the change is ok for all platforms

1 Like

Alrighty. I have gone to Documents/Arduino/libraries/Ethernet and set the Ethernet.h line to.
class EthernetServer : public Server
to
class EthernetServer : public Stream

I have included it to double check.
Now there is also an Ethernet library in the C://program files (x86)/Arduino/... which I have removed just in case, even though I am pretty sure the documents folder overrides it.

This is the error, which I think looks the same aside from he changed line.

Arduino: 1.8.13 (Windows 10), Board: "DOIT ESP32 DEVKIT V1, 80MHz, 230400, None"





















In file included from C:\Users\owner\Documents\DaxWinston\Pier Mayhem Devilhead V2\Client\esp_generic_ethernet\esp_generic_ethernet.ino:2:

C:\Users\owner\Documents\Arduino\libraries\Ethernet\src/Ethernet.h:259:17: error: conflicting return type specified for 'virtual EthernetClient EthernetServer::available()'

  EthernetClient available();

                 ^~~~~~~~~

In file included from C:\Users\owner\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.1\cores\esp32/Arduino.h:160,

                 from sketch\esp_generic_ethernet.ino.cpp:1:

C:\Users\owner\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.1\cores\esp32/Stream.h:48:17: note: overridden function is 'virtual int Stream::available()'

     virtual int available() = 0;

                 ^~~~~~~~~

esp_generic_ethernet:17:16: error: cannot declare variable 'server' to be of abstract type 'EthernetServer'

 EthernetServer server(23);

                ^~~~~~

In file included from C:\Users\owner\Documents\DaxWinston\Pier Mayhem Devilhead V2\Client\esp_generic_ethernet\esp_generic_ethernet.ino:2:

C:\Users\owner\Documents\Arduino\libraries\Ethernet\src/Ethernet.h:254:7: note:   because the following virtual functions are pure within 'EthernetServer':

 class EthernetServer : public Stream {

       ^~~~~~~~~~~~~~

In file included from C:\Users\owner\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.1\cores\esp32/Arduino.h:160,

                 from sketch\esp_generic_ethernet.ino.cpp:1:

C:\Users\owner\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.1\cores\esp32/Stream.h:49:17: note: 	'virtual int Stream::read()'

     virtual int read() = 0;

                 ^~~~

C:\Users\owner\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.1\cores\esp32/Stream.h:50:17: note: 	'virtual int Stream::peek()'

     virtual int peek() = 0;

                 ^~~~

C:\Users\owner\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.1\cores\esp32/Stream.h:51:18: note: 	'virtual void Stream::flush()'

     virtual void flush() = 0;

                  ^~~~~

exit status 1

cannot declare variable 'server' to be of abstract type 'EthernetServer'



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Ethernet.h (12.0 KB)

sorry. it should be class EthernetServer : public Print

1 Like

Good day.

After some extensive experimenting with the solution, I wanted to come here and mention that YES, this last modification to the ethernet library did indeed make it compile and send the data.

I am fairly certain that the library is working because when I use the following sketch:

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 0, 35);
IPAddress myDns(192, 168, 0, 1);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);


// telnet defaults to port 23

EthernetServer server(49250);

EthernetClient clients[8];

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  Ethernet.init(5);   // MKR ETH Shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit FeatherWing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit FeatherWing Ethernet

  // initialize the Ethernet device
  Ethernet.begin(mac, ip, myDns, gateway, subnet);

  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  // start listening for clients
  server.begin();

  Serial.print("Chat server address:");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // check for any new client connecting, and say hello (before any incoming data)
  EthernetClient newClient = server.accept();
  if (newClient) {
    Serial.print("new client");
    for (byte i=0; i < 8; i++) {
      if (!clients[i]) {
        Serial.print("We have a new client #");
        Serial.println(i);
        newClient.print("Hello, client number: ");
        newClient.println(i);
        // Once we "accept", the client is no longer tracked by EthernetServer
        // so we must store it into our list of clients
        clients[i] = newClient;
        break;
      }
    }
  }

  // check for incoming data from all clients
  for (byte i=0; i < 8; i++) {
    if (clients[i] && clients[i].available() > 0) {
      // read bytes from a client
      byte buffer[80];
      int count = clients[i].read(buffer, 80);
      // write the bytes to all other connected clients
      for (byte j=0; j < 8; j++) {
        if (j != i && clients[j].connected()) {
          clients[j].write(buffer, count);
        }
      }
    }
  }

  // stop any clients which disconnect
  for (byte i=0; i < 8; i++) {
    if (clients[i] && !clients[i].connected()) {
      Serial.print("disconnect client #");
      Serial.println(i);
      clients[i].stop();
    }
  }
}

I do see the IP address appearing in the serial monitor. And if I were to disconnect the power of the module, I see that an error appears, which I think is a good sign that the module is at least being detected correctly. I am using netcat as well as a python application to send packets and I do see that the esp32 registers a new client whenever I send a packet so this is really cool.

What isn't working is the Arduibo default Ethernet library sample code. I'm attempting to use netcat to send a byte of data to this port and I do see the green light on the W5500 ethernet module port light up when I hit enter to send, but the ESP32 does not actually seem to be reacting to the data.

Now I can, for now work with this sample code and try to retro fit it but I am just wondering why the sample code from this documentation page does not work?

What could be the next step in debugging this?

1 Like

Ok, seems like for my use case. This was the example that I needed to use - basically with this method I am he one responsible for keeping track of the connected client. So that works now.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.