Multiples connections TCP/IP Arduino UNO+module w5500

Hello everyone, I'm not used to using Arduino and I'm having issues with a project.

I need to connect the Arduino sender to 3 Arduino receivers.

The Arduino sender receives GPIO pins (input) and depending on the received pin, it sends information (0 or 1) to the respective Arduino receiver via TCP/IP connection.
The Arduino sender and the receivers are on different networks (there are no firewall blocks).
The main components of the Arduino sender are an Arduino Uno and a W5500 Ethernet module.
The Arduino receiver has an Arduino Uno, a W5500 Ethernet module, an H-bridge to connect an LED strip if the received information is 1, and an RGB LED that indicates the connection status.

The system works well with up to two connected receivers; beyond the third, the behavior becomes unstable and nothing works as it should. The receivers lose connection, and the sender keeps disconnecting from the switch port.

Below, I'll include the codes for the sender and the receiver.

SENDER

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

// Define gpi pins

const int BUTTON_PIN_A = 7;
const int BUTTON_PIN_B = 6;
const int BUTTON_PIN_C = 5;

// define server port 
const int serverPort = 4080;


byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x05};

// IPs dos clients
IPAddress clientA(10, 140, 32, 5);
IPAddress clientB(10, 140, 3, 16);
IPAddress clientC(10, 140, 29, 139);


// my ip

IPAddress ip(10, 140, 21, 15);
IPAddress gtw(10, 140, 21, 1);
IPAddress subnet(255, 255, 255, 0);

EthernetClient TCPclientA;
EthernetClient TCPclientB;
EthernetClient TCPclientC;

int status_A = 0;
int porta_A = 0;
char info_A = '0';

int status_B = 0;
int porta_B= 0;
char info_B = '0';

int status_C = 0;
int porta_C= 0;
char info_C= '0';

void setup() {
 
pinMode(BUTTON_PIN_A, INPUT);
pinMode(BUTTON_PIN_B, INPUT);
pinMode(BUTTON_PIN_C, INPUT);

Ethernet.begin(mac, ip, gtw, subnet);
Ethernet.setGatewayIP(gtw);

  // connect to TCP Arduinos Client

   if (TCPclientA.connect(clientA, serverPort)) 
    {
	// Serial.println("Connected to TCP server A");
    }
   else{
    TCPclientA.stop();
       }

    if (TCPclientB.connect(clientB, serverPort)) 
    {
	//Serial.println("Connected to TCP server B");
    }
   else{
    TCPclientB.stop();
       }

 if (TCPclientC.connect(clientC, serverPort)) 
    {
	//Serial.println("Connected to TCP server C");
    }
   else{
    TCPclientC.stop();
       }
}

void loop() {


  porta_A = digitalRead(BUTTON_PIN_A);
  porta_B = digitalRead(BUTTON_PIN_B);
  porta_C = digitalRead(BUTTON_PIN_C);


  if (!TCPclientA.connected()) {
     //TCPclientA.stop();
	 
    TCPclientA.setConnectionTimeout(100);
    TCPclientA.connect(clientA, serverPort);

    TCPclientA.write(info_A);
    TCPclientA.flush();
  }
  
 else{
     TCPclientA.stop(); 
 } 
 
  if (!TCPclientB.connected()) {
	  
  TCPclientB.setConnectionTimeout(100);
  TCPclientB.connect(clientB, serverPort);

  TCPclientB.write(info_B);
  TCPclientB.flush();
  } 
  
  else{
    TCPclientB.stop();
  }
  
  if (!TCPclientC.connected()) {
   //  TCPclientC.stop();
  TCPclientC.setConnectionTimeout(100);
  TCPclientC.connect(clientC, serverPort);

  TCPclientC.write(info_C);
  TCPclientC.flush();
  }   
  
  else{
    TCPclientC.stop();
  }


 if (status_B != porta_B)
  {
    status_B = porta_B;

    if (status_B == 0)
    {
      info_B = '0';
    }
    else
    {
      info_B = '1';
    }
   
   TCPclientB.write(info_B);
   TCPclientB.flush();
  }

  
 if (status_C != porta_C)
  {
    status_C = porta_C;

    if (status_C == 0)
    {
      info_C = '0';
    }
    else
    {
      info_C = '1';
    }
   
   TCPclientC.write(info_C);
   TCPclientC.flush();
  }  

 if (status_A != porta_A)
  {
    status_A = porta_A;

    if (status_A == 0)
    {
      info_A = '0';
    }
    else
    {
      info_A = '1';
    }
    
    TCPclientA.write(info_A);
    TCPclientA.flush();
  
  } 

}

RECEIVERS (
The code for the other receivers is the same as this one, with only changes to the MAC address and IP information.)

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

// LED RGB for status
int redPin = 2; //TERMINAL VERMELHO
int greenPin = 3; //TERMINAL VERDE
int bluePin = 4; //TERMINAL AZUL

String cor;

const int LED_PIN = 7;
const int serverPort = 4080;

byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};
IPAddress ip(10, 140, 32, 5);
IPAddress gtw(10, 140, 32, 1);
IPAddress subnet(255, 255, 255, 0);
EthernetServer TCPserver(serverPort);

void setup() {

  //Serial.begin(9600);
  pinMode(LED_PIN, OUTPUT);

  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);


  // Inicializa rede:
  Ethernet.begin(mac, ip, gtw, subnet);
  
  // Testa se o módulo Ethernet existe
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    ErrorModule();
      while (true) {
      delay(1);
    }
  }

  if (Ethernet.linkStatus() == LinkOFF) {
    ErrorRed();
    //Serial.println("Cabo desconectado ou sem presença de link.");
  }
  
  TCPserver.begin();
  
}


void loop() {

  EthernetClient client = TCPserver.available();
  

  if (client) {

    GoodGreen();
    char command = client.read();

    if (command == '1')
      digitalWrite(LED_PIN, HIGH); // Turn LED off
    else if (command == '0')
      digitalWrite(LED_PIN, LOW );  // Turn LED on

    Ethernet.maintain();
  }
  else
  {
    ErrorRed();
  }


    if (Ethernet.hardwareStatus() == EthernetNoHardware) 
    {
    ErrorModule();
    }
    else
    {
     if (Ethernet.linkStatus() == LinkOFF) 
      {
        ErrorRed();
      } 
      else
      {
        GoodGreen();
      }
    }
  
}

void ErrorModule() { // Erro no modulo ETH - cor "laranja e verde"
  analogWrite(greenPin, 165);
  digitalWrite(redPin, HIGH);
  digitalWrite(bluePin, LOW);
}
void ErrorRed() { // Erro de comunicacao - cor vermelho
  digitalWrite(greenPin, LOW);
  digitalWrite(redPin, HIGH);
  digitalWrite(bluePin, LOW);
}
void GoodGreen() { // Status - ativo e comunicando - verde
  digitalWrite(greenPin, HIGH);
  digitalWrite(redPin, LOW);
  digitalWrite(bluePin, LOW);
}

Hi @jhess_silva ,

Welcome to the forum..

hmm.. subnet looks wrong to me..
it would lock you in to 10.140.21.xxx
subnet should be 255.255.0.0
then you can access 10.140.xxx.xxx
same goes for the others as well..

good luck.. ~q

You know not sure if you can..
but this would be allot easier if you could do a udp broadcast..
the sender would just broadcast and any listeners all get the data at the same time..

simpler design that expands easily..

~q

// change this
Ethernet.begin(mac, ip, gtw, subnet);
// to this
Ethernet.begin(mac, ip, gtw, gtw, subnet);

Thank you very much, that worked. Looking at the library, I noticed that even if I didn't specify it, it fills in the DNS information! Putting the gateway back in place worked

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