Thanks, SurferTim. I just ran anther test with heartbeat interval increased to 10 seconds. Over the course of five hours, there were four connection failures - about one failure per 450 heartbeats. This is interesting because my previous test of one second interval in 10 hours yielded around one failure per 433 heartbeats. I agree. Something is fishy.
I have pasted my entire code below. Please note that I have external interrupts 0 and 1 enabled to read the Wiegand26 value from my RFID device. I will start another test with the external interrupts disabled to see if it makes any difference. I would appreciate any insight into the connection failure. Thanks.
#include <SPI.h>
#include <Ethernet.h>
#include <avr/wdt.h>
#define WATCHDOG_ENABLE 1
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 2, 50 }; //assigned arduino LAN IP address
byte server[] = { 192, 168, 2, 47 }; // laptop running apache LAN IP address
byte gateway[] = { 192, 168, 2, 1 }; // laptop running apache LAN IP address
byte subnet[] = { 255, 255, 255, 0 }; // laptop running apache LAN IP address
EthernetClient client;
char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?
volatile unsigned long rfid = 0;
volatile int readCount = 0;
int watchdogCount = 0;
void DATA0(void) {
readCount++;
rfid = rfid << 1;
}
void DATA1(void) {
readCount++;
rfid = rfid << 1;
rfid |= 1;
}
String readPage()
{
//read the page, and capture & return everything between '{' and '}'
stringPos = 0;
memset( &inString, 0, 32 ); //clear inString memory
while(true)
{
if (client.available()) {
char c = client.read();
if (c == '{' ) { //'{' is our begining character
startRead = true; //Ready to start reading the part
}else if(startRead){
if(c != '}'){ //'}' is our ending character
inString[stringPos] = c;
stringPos ++;
}else{
//got what we need here! We can disconnect now
startRead = false;
client.stop();
client.flush();
//Serial.println("disconnecting.");
return inString;
}
}
}
}
}
void httpParse(String command)
{
if(command == "WATCHDOG_RESET")
{
//reset the watchdog timer
wdt_reset();
}
else
{
Serial.println("error");
}
}
void setup()
{
if(WATCHDOG_ENABLE)
{
//enable watchdog timer to 8 seconds
wdt_enable(WDTO_8S);
}
Serial.begin(9600);
Ethernet.begin(mac, ip, gateway, subnet);
Serial.println(Ethernet.localIP());
readCount = 0;
rfid = 0;
attachInterrupt(0, DATA0, RISING);
attachInterrupt(1, DATA1, RISING);
Serial.println("starting arduino client for front entrance");
Serial.println();
}
void loop()
{
//reset watchdog
if(WATCHDOG_ENABLE)
{
if(watchdogCount > 10)
{
Serial.print("*");
if (client.connect(server, 80))
{
Serial.print("+");
String myString = "GET /watchdog.php?ip=";
myString += String(ip[0]) + "." + String(ip[1]) + "." + String(ip[2]) + "." + String(ip[3]) + ".";
myString += " HTTP/1.0";
client.println(myString); //php page invoking my web service
client.println();
//Connected - Read the page
String pageValue = readPage(); //go and read the output
httpParse(pageValue);
watchdogCount = 0;
}
else
{
Serial.println("WATCHDOG connection failed");
}
}
else
{
//reset the watchdog timer
wdt_reset();
}
watchdogCount++;
}
delay(1000);
}