2 telnet Clients on one Arduino Ethernet

Hi Everyone,

I’m trying to connect to two devices (KVM Switches) by Telnet. But apparently it seems it can’t connect to two client together (one is fine, if I comment the code of the second one), does anyone have an idea ?

Here is my code :

// Frameworks includes
#include <SPI.h>
#include <Ethernet.h>
#include <avr/io.h>
#include <avr/wdt.h>

// Ethernet configuration
// Mac Address and IP of the controller
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x05};
IPAddress ip(192,168,34,3);

// KVM1 IP Address
IPAddress server(192,168,32,11);

// KVM2 IP Address
IPAddress server2(192,168,32,12);

// Ethernet Client
EthernetClient client;
EthernetClient client2;

// Pinout configuration
 const int buttonPin = A1;
 const int buttonPin_B = A0; 
 const int ledPin_A = 6;
 const int ledPin_B = 5;
 const int ledPin_C = 4;
 const int ledPin_D = 3;
 const int ledPin_E = 2;


// Values for Buttons High and Low resistors

const int BUTTON1LOW = 4;
const int BUTTON2LOW = 30;
const int BUTTON3LOW = 55;
const int BUTTON4LOW = 78;
const int BUTTON5LOW = 100;
const int BUTTON6LOW = 121;
const int BUTTON7LOW = 142;
const int BUTTON8LOW = 162;
const int BUTTON9LOW = 4;
const int BUTTON10LOW = 30;
const int BUTTON11LOW = 55;
const int BUTTON12LOW = 78;
const int BUTTON13LOW = 100;
const int BUTTON14LOW = 121;
const int BUTTON15LOW = 141;
const int BUTTON16LOW = 160;

const int BUTTON1HIGH = 24;
const int BUTTON2HIGH = 50;
const int BUTTON3HIGH = 75;
const int BUTTON4HIGH = 98;
const int BUTTON5HIGH = 120;
const int BUTTON6HIGH = 139;
const int BUTTON7HIGH = 160;
const int BUTTON8HIGH = 180;
const int BUTTON9HIGH = 24;
const int BUTTON10HIGH = 50;
const int BUTTON11HIGH = 75;
const int BUTTON12HIGH = 98;
const int BUTTON13HIGH = 120;
const int BUTTON14HIGH = 141;
const int BUTTON15HIGH = 161;
const int BUTTON16HIGH = 180;
 
 // Variables
 int buttonState;
 int lastButtonState = LOW;

 // Long Variables
 long lastDebounceTime = 0;  // the last time the output pin was toggled
 long debounceDelay = 50;    // the debounce time; increase if the output flickers
 unsigned long currentTime;
 unsigned long loopTime;

void setup() {
  
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);

  // give the Ethernet shield a second to initialize:
  delay(1000);
  
  // Start Serial
  Serial.begin(9600);
  
  // We define the PINOUT modes
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin_B, INPUT_PULLUP);
  pinMode(ledPin_A, INPUT);
  pinMode(ledPin_B, INPUT);
  pinMode(ledPin_C, INPUT);
  pinMode(ledPin_D, INPUT);

  // if you get a connection, report back via serial:
  if (client.connect(server, 23)) {
    delay(1000);
  } else {
    Serial.println("Server 1 down");
  }
  if (client2.connect(server2, 23)) {
    delay(1000);
  } else {
    Serial.println("Server 2 down");
  }
  
  // We login into the Nion server
  if (client.connected()) {
      delay(3000);
     if (client2.connected()) {
     delay(3000);
      
  } 
  }
  
  // We get the current time in Millis (to sample the encoder)
  currentTime = millis();
  loopTime = currentTime;
}

void loop()
{
   
   // read the state of the switch into a local variable:
   int reading = analogRead(buttonPin);
   int reading_B = analogRead(buttonPin_B); 
   int tmpButtonState = LOW;             // the current reading from the input pin
   
   if(reading_B>BUTTON16LOW && reading_B<BUTTON16HIGH){
     //Read switch 16
     tmpButtonState = 16;
   }else if(reading_B>BUTTON15LOW && reading_B<BUTTON15HIGH){
     //Read switch 15
     tmpButtonState = 15;
   }else if(reading_B>BUTTON14LOW && reading_B<BUTTON14HIGH){
     //Read switch 14
     tmpButtonState = 14;
   }else if(reading_B>BUTTON13LOW && reading_B<BUTTON13HIGH){
     //Read switch 13
     tmpButtonState = 13;
   }else if(reading_B>BUTTON12LOW && reading_B<BUTTON12HIGH){
     //Read switch 12
     tmpButtonState = 12;
   }else if(reading_B>BUTTON11LOW && reading_B<BUTTON11HIGH){
     //Read switch 11
     tmpButtonState = 11;
   }else if(reading_B>BUTTON10LOW && reading_B<BUTTON10HIGH){
     //Read switch 10
     tmpButtonState = 10;
   }else if(reading_B>BUTTON9LOW && reading_B<BUTTON9HIGH){
     //Read switch 9
     tmpButtonState = 9;
   }else if(reading>BUTTON8LOW && reading<BUTTON8HIGH){
     //Read switch 8
     tmpButtonState = 8;
   }else if(reading>BUTTON7LOW && reading<BUTTON7HIGH){
     //Read switch 7
     tmpButtonState = 7;
   }else if(reading>BUTTON6LOW && reading<BUTTON6HIGH){
     //Read switch 6
     tmpButtonState = 6;
   }else if(reading>BUTTON5LOW && reading<BUTTON5HIGH){
     //Read switch 5
     tmpButtonState = 5;
   }else if(reading>BUTTON4LOW && reading<BUTTON4HIGH){
     //Read switch 4
     tmpButtonState = 4;
   }else if(reading>BUTTON3LOW && reading<BUTTON3HIGH){
     //Read switch 3
     tmpButtonState = 3;
   }else if(reading>BUTTON2LOW && reading<BUTTON2HIGH){
     //Read switch 2
     tmpButtonState = 2;
   }else if(reading>BUTTON1LOW && reading<BUTTON1HIGH){
     //Read switch 1
     tmpButtonState = 1;
   }else{
     //No button is pressed;
     tmpButtonState = LOW;
   } 

   // If the switch changed, due to noise or pressing:
   if (tmpButtonState != lastButtonState) {
     buttonState = tmpButtonState;
   } 

   // save the reading.  Next time through the loop,
   // it'll be the lastButtonState:
   lastButtonState = buttonState;
  
if (client.connected()) {

       // Presets reload buttons
       switch(buttonState){
         case 1:
           // Load Screen 1A
           client.println("r 1 2");
           Serial.println("1A");
           delay(1000);
           // LED 1 ON
//           pinMode(ledPin_A, OUTPUT);
//           digitalWrite(ledPin_A, HIGH);
//           pinMode(ledPin_B, OUTPUT);
//           digitalWrite(ledPin_B, LOW);
//           delay(500);
           break;
         
..// Same with 7 other buttons on client 1 //..
           
         case 9:
           // Load Screen 1B
           client2.println("r 1 2");
           Serial.println("1B");
           delay(1000);
           // LED 9 ON
//           pinMode(ledPin_C, OUTPUT);
//           digitalWrite(ledPin_C, HIGH);
//           pinMode(ledPin_D, OUTPUT);
//           digitalWrite(ledPin_D, LOW);
//           delay(500);
           break;
           
..// Same with 7 other buttons on client 2 //..
       }

}else{
  pinMode(ledPin_A, OUTPUT);
  digitalWrite(ledPin_A, HIGH);
  pinMode(ledPin_B, OUTPUT);
  digitalWrite(ledPin_B, LOW);
  delay(1000);
  pinMode(ledPin_A, INPUT);
  pinMode(ledPin_B, INPUT);
  
  if(reading_B>BUTTON16LOW && reading_B<BUTTON16HIGH){
     //Reset is network issue
      wdt_enable(WDTO_30MS);
      while(1) {};
  }
  
  Serial.println("Can't connect");
}

}

Thank you !

What's the output of your sketch?

You mean the serial ?

Yes, what's the output of the serial interface while you're running the sketch?