Server only works once from different subnet

I can access and send HTTP requests all day long from a machine on the same subnet as the arduino... but if I try from a machine on a different subnet, it only works once and then won't respond after that. I can ping all day long from the same subnet, but only 1-2 pings get a response from a machine on another subnet. It's not a routing or switching issue.

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

void turnLightsOff();
void turnLightsOn();

byte mac[] = { 0xA6, 0xA2, 0xB3, 0xC4, 0xD5, 0x82 }; //physical mac address
byte ip[] = { 10, 1, 7, 199 }; // ip in lan
byte gateway[] = { 10, 1, 7, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask

EthernetServer server(80); //server port

String readString; 

//////////////////////

void setup(){
  
  pinMode(53, OUTPUT); //pin selected to control --
  pinMode(37, OUTPUT); //pin selected to control --
  pinMode(49, OUTPUT); //pin selected to control --
  pinMode(47, OUTPUT); //pin selected to control --
  pinMode(45, OUTPUT); //pin selected to control --
  pinMode(43, OUTPUT); //pin selected to control --
  pinMode(41, OUTPUT); //pin selected to control --
  pinMode(39, OUTPUT); //pin selected to control --
  digitalWrite(53, HIGH);
  digitalWrite(37, HIGH);
  digitalWrite(49, HIGH);
  digitalWrite(47, HIGH);
  digitalWrite(45, HIGH);
  digitalWrite(43, HIGH);
  digitalWrite(41, HIGH);
  digitalWrite(39, HIGH);
//  start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("product purchase alarm... system ready."); // so I can keep track of what is loaded
}

      void turnLightsOff()
          {
            digitalWrite(53, HIGH);
            digitalWrite(37, HIGH);
            digitalWrite(51, HIGH);
            digitalWrite(49, HIGH);
            digitalWrite(47, HIGH);
            digitalWrite(45, HIGH);
            digitalWrite(43, HIGH);
            digitalWrite(41, HIGH);
            digitalWrite(39, HIGH);
            Serial.println("Lightbar Off");
          }   

void loop(){
  
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
//          Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/json");
          client.println();
          
          if (readString.indexOf("?options") > 0)
          {
           client.println("{\"stage_options\": {\"option1\": \"stage=1\",\"option2\": \"stage=2\",\"option3\": \"stage=3\",\"option4\": \"stage=4\",\"option5\": \"stage=5\",\"option6\": \"stage=6\",\"option7\": \"stage=7\",\"option8\": \"stage=8\"},\"all_on_or_off\": {\"option9\": \"allon\",\"option10\": \"alloff\"},\"individual_relays_on\": {\"option11\": \"only1\",\"option12\": \"only2\",\"option13\": \"only3\",\"option14\": \"only4\",\"option15\": \"only5\",\"option16\": \"only6\",\"option17\": \"only7\",\"option18\": \"only8\"},\"individual_relays_off\":{\"option19\": \"only1_off\",\"option20\": \"only2_off\",\"option21\": \"only3_off\",\"option22\": \"only4_off\",\"option23\": \"only5_off\",\"option24\": \"only6_off\",\"option25\": \"only7_off\",\"option26\": \"only8_off\"}}"); 
           
          }

          

          //stopping client
		  client.stop();	
          Serial.println("client stopped");

          ///////////////////// control arduino pin
          
          //delay between relays
          int lightDelay = 100;
          int lightsOffDelay = 10000;
                   
          if(readString.indexOf("?stage=1") > 0)//checks for on
          {
            digitalWrite(53, LOW);
            Serial.println("Lightbar On Stage 1");
            delay(lightsOffDelay);
            turnLightsOff();
          }
          
          if(readString.indexOf("?stage=2") > 0)//checks for on
          {
            digitalWrite(53, LOW);
            delay(lightDelay);
            digitalWrite(37, LOW);
            Serial.println("Lightbar On Stage 2");
            delay(lightsOffDelay);
            turnLightsOff();
          }
          
          
          if(readString.indexOf("?only3_off") > 0)//checks for on
          {
            digitalWrite(49, HIGH);
            Serial.println("Only 3 Off");
          }
          
          if(readString.indexOf("?only4_off") > 0)//checks for on
          {
            digitalWrite(47, HIGH);
            Serial.println("Only 4 Off");
          }
          
          if(readString.indexOf("?only5_off") > 0)//checks for on
          {
            digitalWrite(45, HIGH);
            Serial.println("Only 5 Off");
          }
          
          if(readString.indexOf("?only6_off") > 0)//checks for on
          {
            digitalWrite(43, HIGH);
            Serial.println("Only 6 Off");
          }
          
          if(readString.indexOf("?only7_off") > 0)//checks for on
          {
            digitalWrite(41, HIGH);
            Serial.println("Only 7 Off");
          }
          
          if(readString.indexOf("?only8_off") > 0)//checks for on
          {
            digitalWrite(39, HIGH);
            Serial.println("Only 8 Off");
          }

          //clearing string for next read
          readString="";
          readString="";

        }
      }
    }
  }
}

I'll start with the most obvious error. You omitted a parameter in the Ethernet.begin() call.

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

Thanks for the reply. Funny enough, I thought that I copied the code wrong and that was a typeo so I took it out... I'll put it back in, but it's still doing the same thing.

What is the last thing that shows on the serial monitor?

Do you know this for certain?

It's not a routing or switching issue.

The last thing the serial prints is: "product purchase alarm... system ready."

And yes, I know for certain it's not a routing or switching issue.

This was a $15 ethernet shield, but I've ordered the real arduino branded one and will give that a try... just to rule it out.

Just installed the new Ethernet shield... same issue.

Try this test sketch, then try pinging this IP from the other network. How does that do? The second IP displayed is the gateway. Insure that is what you are using with a static assignment.

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

byte mac[] = {  0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

void setup() {
  Serial.begin(9600);

  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.print(F("Starting ethernet..."));
  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else {
      Serial.println(Ethernet.localIP());
      Serial.println(Ethernet.gatewayIP());
  }
}

void loop() {
}

Here is the output:

Starting ethernet...
10.1.1.86
10.1.1.1

On another note - I'm capturing packets with wireshark and looking at the first response (since it works the first time only), there is an error... I'm wondering if there is something wrong with the MAC address? Just a stab without doing any research as of yet since I just saw it... I'll post the error in a second, have to go to the other computer.

That is not what I expected to see. I expected to see a 10.1.7.x IP.

Try this. Same code with one addition. The third IP will be the subnet mask. What is it?

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

byte mac[] = {  0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

void setup() {
  Serial.begin(9600);

  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.print(F("Starting ethernet..."));
  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else {
      Serial.println(Ethernet.localIP());
      Serial.println(Ethernet.gatewayIP());
      Serial.println(Ethernet.subnetMask());
   }
}

void loop() {
}

That's my mistake - I was testing something when I posted the code initially... The IP address statically set is 10.1.1.107, Gateway 10.1.1.1 subnet 255.255.255.0

Then this original IP and gateway are not correct. That will cause a routing problem.

byte ip[] = { 10, 1, 7, 199 }; // ip in lan
byte gateway[] = { 10, 1, 7, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask

On that network, this would be correct, considering the IP is not already being used. If it is already being used by another device, that would also cause a routing problem.

byte ip[] = { 10, 1, 1, 199 }; // ip in lan
byte gateway[] = { 10, 1, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask

I'm sorry - I've made it confusing by posting the wrong code initially... So let me straighten it out here.

Here is the current code for the arduino:

//bbarber 4-17-15

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

void turnLightsOff();
void turnLightsOn();

byte mac[] = { 0xB0, 0x06, 0x45, 0x97, 0xFE, 0x82 }; //physical mac address
byte ip[] = { 10,1,1,107 }; // ip in lan
byte gateway[] = { 10, 1, 1,1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask

EthernetServer server(80); //server port

String readString; 

//////////////////////

void setup(){
  
  pinMode(53, OUTPUT); //pin selected to control --
  pinMode(37, OUTPUT); //pin selected to control --
  pinMode(49, OUTPUT); //pin selected to control --
  pinMode(47, OUTPUT); //pin selected to control --
  pinMode(45, OUTPUT); //pin selected to control --
  pinMode(43, OUTPUT); //pin selected to control --
  pinMode(41, OUTPUT); //pin selected to control --
  pinMode(39, OUTPUT); //pin selected to control --
  digitalWrite(53, HIGH);
  digitalWrite(37, HIGH);
  digitalWrite(49, HIGH);
  digitalWrite(47, HIGH);
  digitalWrite(45, HIGH);
  digitalWrite(43, HIGH);
  digitalWrite(41, HIGH);
  digitalWrite(39, HIGH);
//  start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("product purchase alarm... system ready."); // so I can keep track of what is loaded
}

      void turnLightsOff()
          {
            digitalWrite(53, HIGH);
            digitalWrite(37, HIGH);
            digitalWrite(51, HIGH);
            digitalWrite(49, HIGH);
            digitalWrite(47, HIGH);
            digitalWrite(45, HIGH);
            digitalWrite(43, HIGH);
            digitalWrite(41, HIGH);
            digitalWrite(39, HIGH);
            Serial.println("Lightbar Off");
          }   

void loop(){
  
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
//          Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/json");
          client.println();
          
          if (readString.indexOf("?options") > 0)
          {
           client.println("{\"stage_options\": {\"option1\": \"stage=1\",\"option2\": \"stage=2\",\"option3\": \"stage=3\",\"option4\": \"stage=4\",\"option5\": \"stage=5\",\"option6\": \"stage=6\",\"option7\": \"stage=7\",\"option8\": \"stage=8\"},\"all_on_or_off\": {\"option9\": \"allon\",\"option10\": \"alloff\"},\"individual_relays_on\": {\"option11\": \"only1\",\"option12\": \"only2\",\"option13\": \"only3\",\"option14\": \"only4\",\"option15\": \"only5\",\"option16\": \"only6\",\"option17\": \"only7\",\"option18\": \"only8\"},\"individual_relays_off\":{\"option19\": \"only1_off\",\"option20\": \"only2_off\",\"option21\": \"only3_off\",\"option22\": \"only4_off\",\"option23\": \"only5_off\",\"option24\": \"only6_off\",\"option25\": \"only7_off\",\"option26\": \"only8_off\"}}"); 
           client.stop();
          Serial.println("client stopped");
          }

          if (readString.indexOf("?stage=1") > 0)
          {
           client.println("{\"status\":\"success\",\"stage\":1}"); 
           client.stop();
          Serial.println("client stopped");
          }
          if (readString.indexOf("?stage=2") > 0)
          {
           client.println("{\"status\":\"success\",\"stage\":2}"); 
           client.stop();
          Serial.println("client stopped");
          }
          if (readString.indexOf("?stage=3") > 0)
          {
           client.println("{\"status\":\"success\",\"stage\":3}"); 
           client.stop();
          Serial.println("client stopped");
          }
          if (readString.indexOf("?stage=4") > 0)
          {
           client.println("{\"status\":\"success\",\"stage\":4}"); 
           client.stop();
          Serial.println("client stopped");
          }
          if (readString.indexOf("?stage=5") > 0)
          {
           client.println("{\"status\":\"success\",\"stage\":5}"); 
           client.stop();
          Serial.println("client stopped");
          }
          if (readString.indexOf("?stage=6") > 0)
          {
           client.println("{\"status\":\"success\",\"stage\":6}"); 
           client.stop();
          
          //stopping client


          ///////////////////// control arduino pin
          
          //delay between relays
          int lightDelay = 100;
          int lightsOffDelay = 10000;
                   
          if(readString.indexOf("?stage=1") > 0)//checks for on
          {
            digitalWrite(53, LOW);
            Serial.println("Lightbar On Stage 1");
            delay(lightsOffDelay);
            turnLightsOff();
          }
          
          if(readString.indexOf("?stage=2") > 0)//checks for on
          {
            digitalWrite(53, LOW);
            delay(lightDelay);
            digitalWrite(37, LOW);
            Serial.println("Lightbar On Stage 2");
            delay(lightsOffDelay);
            turnLightsOff();
          }
          
         if(readString.indexOf("?stage=3") > 0)//checks for on
          {
            digitalWrite(53, LOW);
            delay(lightDelay);
            digitalWrite(37, LOW);
            delay(lightDelay);
            digitalWrite(49, LOW);
            Serial.println("Lightbar On Stage 3");
            delay(lightsOffDelay);
            turnLightsOff();
          }
          
        if(readString.indexOf("?stage=4") > 0)//checks for on
          {
            digitalWrite(53, LOW);
            delay(lightDelay);
            digitalWrite(37, LOW);
            delay(lightDelay);
            digitalWrite(49, LOW);
            delay(lightDelay);
            digitalWrite(47, LOW);
            Serial.println("Lightbar On Stage 4");
            delay(lightsOffDelay);
            turnLightsOff();
          }
          
       if(readString.indexOf("?stage=5") > 0)//checks for on
          {
            digitalWrite(53, LOW);
            delay(lightDelay);
            digitalWrite(37, LOW);
            delay(lightDelay);
            digitalWrite(49, LOW);
            delay(lightDelay);
            digitalWrite(47, LOW);
            delay(lightDelay);
            digitalWrite(45, LOW);
            Serial.println("Lightbar On Stage 5");
            delay(lightsOffDelay);
            turnLightsOff();
          }
          
      if(readString.indexOf("?stage=6") > 0)//checks for on
          {
            digitalWrite(53, LOW);
            delay(lightDelay);
            digitalWrite(37, LOW);
            delay(lightDelay);
            digitalWrite(49, LOW);
            delay(lightDelay);
            digitalWrite(47, LOW);
            delay(lightDelay);
            digitalWrite(45, LOW);
            delay(lightDelay);
            digitalWrite(43, LOW);
            Serial.println("Lightbar On Stage 6");
            delay(lightsOffDelay);
            turnLightsOff();
          }
          
     

          //clearing string for next read
          readString="";
          readString="";

        }
      }
    }
  }
}

The machine I can access just fine from has an is on the same subnet (10.1.1.0/24) and any other subnet only works once, then nothing. Again, I can confirm it is not a switching or routing issue as it's an enterprise network that I admin, with packets being routed between these very two subnets all day long with no issues (10.1.1.0/24 - 10.1.5.0/24).

So just to clairify, I can ping and access 10.1.1.107 (unused static address of arduino) from 10.1.1.95 (workstation), but can only get two initial ping replies when pinging from 10.1.5.10 (laptop) and then the pings are not replied to.

Sorry for the confusion... my bad.

I just tried my Mega 2560 with ethernet shield on 192.168.2.2/24 from 192.168.1.253/24, and I can ping it all day long. Don't know what to tell you.

edit: I can also access the server on the Mega all day long.

Any chance you're using layer 3 switching?

bobby3:
Any chance you're using layer 3 switching?

No. Two separate interfaces with different subnets on each. No switch. No bridge. MikroTik OS using a RB433AH.

ok - I'm going to try layer 2.

layer 2 seems to be working just fine.... Do you know if there are any known issues with switching arduino traffic via layer 3?

I don't know. I have seen problems with some switches and the ethernet shield, but don't know if the layer 3 switching is the exact problem.

ok - well thanks for your help - I appreciate it!