[solved] ethernetshield connection failed after some succesfull connections

Hi,

I'm looking for ideas on how to troubleshoot the following problem. My sketch contacts a webserver, reads the page, and prints it out on serial.
This will be repeated in the loop with some delay in between.

My problem is that after a certain number of successful page fetches, the connections start to fail consistently.
I use the ethernet library from kegger (found in the forum) because that supports dhcp (a hard requirement for my project).
This code seems not to be maintained anymore (last change 2008).

Should I use another library (suggestions?) or are there possibilities to troubleshoot this issue?

This is my sketch:

#include <SPI.h>
#include <Ethernet.h>
#include "Dhcp.h"
#include <string.h>

byte mac[] = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
byte server[] = {192, 168, 101, 106};
boolean ipAcquired = false;

void setup()
{
Serial.begin(9600);
Serial.println("getting ip...");
int result = Dhcp.beginWithDHCP(mac);
if(result == 1)
{
ipAcquired = true;
printDhcpInfo();
}
else
Serial.println("unable to acquire ip address...");
}

void loop()
{
Serial.print("Remote host ip address: ");
printArray(&Serial, ".", server, 4, 10);

if(ipAcquired)
{
Client client1(server, 55557);
Serial.println("connecting client1...");
if (client1.connect()) {
Serial.println("connected");
client1.println("GET /temp.html HTTP/1.0");
client1.println();
delay(3000);
} else {
Serial.println("connection failed");
}
String l;
int closetag_index;
if (client1.connected())
{
Serial.println("checking if client has data");
while (client1.available())
{
Serial.println("reading");
l = readline(client1);
Serial.print(l);
}
Serial.println("No data anymore from client1");
}
else
{
Serial.println();
Serial.println("disconnecting client1.");
client1.stop();
}
Serial.println("sleeping 30s");
delay(30000);
}
else
spinForever();
}

String readline(Client cl)
{
String str;
while (cl.available()){
char c = cl.read();
if (c != '\n'){
str += c;
} else {
str += c;
return str;
}
}
}

void printDhcpInfo()
{
byte buffer[6];
Serial.println("ip acquired...");

Dhcp.getMacAddress(buffer);
Serial.print("mac address: ");
printArray(&Serial, ":", buffer, 6, 16);

Dhcp.getLocalIp(buffer);
Serial.print("ip address: ");
printArray(&Serial, ".", buffer, 4, 10);

Dhcp.getSubnetMask(buffer);
Serial.print("subnet mask: ");
printArray(&Serial, ".", buffer, 4, 10);

Dhcp.getGatewayIp(buffer);
Serial.print("gateway ip: ");
printArray(&Serial, ".", buffer, 4, 10);

Dhcp.getDhcpServerIp(buffer);
Serial.print("dhcp server ip: ");
printArray(&Serial, ".", buffer, 4, 10);

Dhcp.getDnsServerIp(buffer);
Serial.print("dns server ip: ");
printArray(&Serial, ".", buffer, 4, 10);
}

void printArray(Print output, char delimeter, byte* data, int len, int base)
{
char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

for(int i = 0; i < len; i++)
{
if(i != 0)
output->print(delimeter);

output->print(itoa(data*, buf, base));*

  • }*

  • output->println();*
    }
    void spinForever()
    {

  • for(;;)*

  • ;*
    }
    [/quote]

I would call Dhcp.beginWithDHCP(mac) on every iteration. I assume that's calling Ethernet::begin() and UDP::begin(). I find those two have to be called before every connection.

My problem is that after a certain number of successful page fetches

Is it literally "a certain number", i.e., it occurs after exactly the same number of iterations every time? That could point to a library bug somewhere. I notice there are execution paths where you don't call client.stop(), and maybe the destructor isn't tidying everything up properly at the end of loop(). It's unlikely, but possible.

Which CPU chip are you using? An ATMega168 is extremely iffy for running Ethernet apps, and the memory shortage may not show up until runtime.

Are you certain that the failure is at the Arduino end? Have you tried running, say, a Perl script that does similar queries? Maybe what's happening is that the server has stopped answering.

@DCContrarian: thanks, that indeed solved my problem!

@Ran Talbott: I'm running on an Arduino UNO, using atmel328 if I'm not mistaking. I thought all the execution paths had a client stop, strange I'll investigate that further (I want clean code).
Not sure if it was at a 'certain' number, my impression was that it was rather variable amount of connections, but i didn't check that. The server is definitely working ok, that was verified.

Thanks for your replies!