c++arduinoesp32ethernetarduino-esp32

Limit of EthernetClient connections on ESP32


I'm working on a project on which I need to have multiple EthernetClient connections to different servers. All of these connections must be kept alive simultaneously and by this I mean I cannot stop or close one of these clients before starting the other.

Currently I can keep 2 simultaneous EthernetClient successful connections, but after this, any subsequent client fails to connect to any server. The following code is running on an ESP32-S3 with a W5500 Ethernet module.

#include <Arduino.h>
#include <EthernetLarge.h>
#include <SPI.h>

#define     SPI_MOSI    35
#define     SPI_MISO    37
#define     SPI_SCK     36
#define     ETHERNET_CS 38

SPIClass SPI_Ethernet;
EthernetClient ethCli1;
EthernetClient ethCli2;
EthernetClient ethCli3;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

void setup() 
{
  Serial0.begin(115200);
  SPI_Ethernet.begin(SPI_SCK, SPI_MISO, SPI_MOSI, ETHERNET_CS);

  pinMode(39, OUTPUT);
  pinMode(47, OUTPUT);
  digitalWrite(47, HIGH);
  digitalWrite(39, LOW);
  delay(500);
  digitalWrite(39, HIGH);
  
  Ethernet.init(ETHERNET_CS);
  Ethernet.begin(mac);
  Serial0.println(Ethernet.localIP());

  if( !(ethCli1.connect("httpbin.org", 80)) )
      Serial0.println("connection > 1 < failed");
  else
    Serial0.println("connection > 1 < succeeded");

  if( !(ethCli2.connect("httpbin.org", 80)) )
    Serial0.println("connection > 2 < failed");
  else
    Serial0.println("connection > 2 < succeeded");

  if( !(ethCli3.connect("httpbin.org", 80)) )
    Serial0.println("connection > 3 < failed");
  else
    Serial0.println("connection > 3 < succeeded");
}

void loop() 
{
}

Which outputs:

connection > 1 < succeeded
connection > 2 < succeeded
connection > 3 < failed

In this code I'm using the OPEnSLab-OSU/EthernetLarge Ethernet library. But I also tried to run it with the Ethernet.h with the same output. In the W5500 documentation I was able to find the information that it can keep up to 8 connections open, but I wasn't able to find if the ESP32 has any restrictions like a max of simultaneous connections. Is it possible to keep more than 2 connections alive simultaneously?

In short:


Solution

  • The OPEnSLab-OSU/EthernetLarge library by default defines MAX_SOCK_NUM to 2. Increasing this value allowed me to keep more simultaneous connections.

    Regarding the Ethernet.h library, I also checked it and it was already setting MAX_SOCK_NUM to 8. Testing the code again with it also allowed me to keep more simultaneous connections. Why I wasn't able to keep them before when using Ethernet.h it is still a mystery to me.