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]