Search code examples
c++arduinoethernetlan

How do I fix multiple Arduinos on LAN causing timeouts


This is my situation: I have a switch and two Arduinos with Ethernet shields connected to it, both running a simple (test) web server program. My computer is also connected to the switch, so I can send requests to the Arduinos. This is the Arduino program (only the IP address on the second Arduino is different):

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //ethernet shield mac address
byte ip[] = { 192, 168, 0, 11 }; // arduino IP in lan
byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port

void setup() {
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();
}

void loop() {   
  EthernetClient client = server.available();
  if (client) {
    int nrOfCharacters = 0;
    String msg = "";
    while (client.connected() && client.available()) {
      char c = client.read();
      Serial.print(c);
      nrOfCharacters++;

      if(c == '\n' || nrOfCharacters >= 6 && c == ' ') 
      {
        httpResponse(client, msg);
      }
      else if(nrOfCharacters >= 6) {
        msg += c;
      }       
    }
  }
}

void httpResponse(EthernetClient client, String msg)
{
  client.println("HTTP/1.1 200 OK");
  client.println();
  client.print(msg);  
  client.stop();
}

One Arduino has IP address 192.168.0.10 and the other has IP address 192.168.0.11.

Everything works fine, and requests are handled perfectly, but when I sent a request to one Arduino, and subsequently to the other, then the second Arduino times out, and after that it works again. I can almost perfectly reproduce this problem over and over again.

I have tried inspecting the socket statuses on the Arduinos using this method I found online:

byte socketStat[MAX_SOCK_NUM];

void ShowSockStatus()
{
  for(int i = 0; i < MAX_SOCK_NUM; i++) {
      Serial.print(F("Socket#"));
      Serial.print(i);
      uint8_t s = W5100.readSnSR(i);
      socketStat[i] = s;
      Serial.print(F(":0x"));
      Serial.print(s, 16);
      Serial.print(F(" "));
      Serial.print(W5100.readSnPORT(i));
      Serial.print(F(" D:"));
      uint8_t dip[4];
      W5100.readSnDIPR(i, dip);
      for(int j = 0; j < 4; j++) {
        Serial.print(dip[j], 10);
        if(j < 3) Serial.print(".");
      }
      Serial.print(F("("));
      Serial.print(W5100.readSnDPORT(i));
      Serial.println(F(")"));
  }
  Serial.println("");
}

But I found no issues. I have also inspected if there are 'hickups' in calling the loop() method, but no problems there either.

I suspect it has something to do with data sent to one Arduino also being received on the other, causing some kind of issue with it, but I am not really familiar with the workings of network switches.

Does anyone have a way to fix this?


Solution

  • only the IP address on the second Arduino is different, you should have a different MAC Address as well.