Ethernet server fails (sometimes)

Hi, i’m using an arduino ethernet board (rev3) to measure temperatures in different rooms in the house. All went well, i’m getting the readings from the DS18S20’s and can print it to the serial.
Last i added ethernet server code to view the temperatures in the browser of my pc and i’m getting “odd” behavior… Sometimes it works, sometimes it doesn’t. My browser would sometimes report “the connection is reinitialized” or “can’t make connection” then after a little while it works perfectly and i can refresh the page a few times until i get no responses anymore.

I have now used a trimmed down version, to only test the ethernet server code and i’m getting the same results:

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

byte mac[] = { 0xb4, 0x75, 0x0e, 0x71, 0x7d, 0x0d };

EthernetServer server(80);

#define bufferMax 128
int bufferSize;
char buffer[bufferMax];

void setup(void)
{
	if(Ethernet.begin(mac) == 0) { 
	if(Serial) Serial.println("Failed to configure Ethernet using DHCP");  
	while(true)
		;
	}
	delay(1000);

	server.begin();
}

void loop(void)
{ 
	EthernetClient client = server.available();
	
	if (client)
	{
		WaitForRequest(client);
		delay(100);
		client.stop();
	}

}

void WaitForRequest(EthernetClient client) // Sets buffer[] and bufferSize
{
  bufferSize = 0;
 
  while (client.connected()) {
    if (client.available()) {
      char c = client.read();
      if (c == '\n')
      {
		client.println("HTTP/1.1 200 OK");
		client.println("Content-Type: text/html");
		client.println("Connnection: close");
		client.println();
		client.println("<!DOCTYPE HTML>");
		client.println("<html>");
		client.println("hello");
		client.println("</html>");
        break;
      } else {
        if (bufferSize < bufferMax)
          buffer[bufferSize++] = c;
        else
          break;
      }
    }
  }
}

Other code would also include printing the sensor readings to the client.
This is just the last code i tested, thought maybe there’s a buffer or memory overflow… The code above gives me the same as well, sometimes it will show me “hello” in the browser, sometimes my browser can’t connect to it, then after a little while without doing anything it would work again for a few times then back to nothing and so on.

I’ve tried several other methods (also without the infinite loop on ethernet.begin or without the delay or a lower delay) and all give me the same results.
I also tried a different port other than the standard 80, because i thought maybe other devices in my LAN were flooding it somehow, but… same result…
Also used an external power adapter instead of relying on the USB power only, because i thought maybe it doesn’t have enough juice.

The arduino never fails to respond to ping requests, so it remains connected to the LAN and i don’t think there’s something wrong with the board itself.

One other thing i can maybe think of is the fact that i’m using an old windows xp laptop to compile and upload the code, only because it’s convenient to place the laptop near the arduino and connect it over USB, could that maybe be the problem?

Maybe this is worth mentioning:
In my router i’m checking the connected LAN computers and saw 2 devices with the same ip address. Only difference are the capitals in the mac address from one. I’m pretty sure there is no other device trying to get the same ip address, so both are actually the arduino imo. maybe that’s just the router software showing what devices were connected in the last few minutes!?

Imgur

I didn’t see this when i checked earlier. yesterday there would only be 1 with that ip address and without a “Name (if any)”.

I’m running out of ideas, anyone else experienced this or has any clue?

SurferTim regularly posts code to print the status of all 4 ports on the Ethernet card. You need to find that code, and see if you are, indeed, failing to close a port correctly.

Without seeing all of your code, all we can do is guess that you are, indeed, running out of memory. Save some of that precious SRAM:
if(Serial) Serial.println(F(“Failed to configure Ethernet using DHCP”));

Why does your code reply the same to ALL requests? Paying to attention to what the client is asking for seems reasonable.

Why are you storing 128 characters of the request? Why are you not doing anything with that saved data?

Here is the code I use to check the socket status.

#include <utility/w5100.h>

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(")"));
  }
}

A socket status list:
0x0 = available.
0x14 = socket waiting for a connection
0x17 = socket connected to a client if a server, or a server if a client.
0x22 = UDP socket.

Here is the code I use to check available SRAM.

int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval);  
}

Use it like this:

Serial.print(F("Free RAM: "));
Serial.println(freeRam());

Call it immediately before where you feel the program is failing.

edit: And here is my server sketches I posted to the playground. The older (second) example is closer to yours.
http://playground.arduino.cc/Code/WebServerST

oh cr*p... i feel like a total dork... :fearful:

on the playground link:

WARNING! Each device on a network must have a unique mac address. If you are using more than one ethernet shield on a network, you must insure all mac addresses are unique. No duplicates!

i knew that, checked the mac addresses anyway and discovered that i had copied a mac address of a belkin wemo switch instead... i connected the arduino (without any ethernet code) to the router, then checked the connected devices in the router to get it's mac address and totally forgot about the switch, i was convinced it was the arduino. :blush: i suppose there was a war going on between the 2 devices and that's why 2 devices with the same ip showed up and it only worked sometimes.

i now did the trouble of disconnecting all wires and checked the back of the arduino board for it's own mac address. (yeh i know, you can pick your own)

your replies were helpful after all, thank you. lesson learned