Go Down

Topic: Ethernet shield permanently returning 0.0.0.0 after working for a few hours (Read 4 times) previous topic - next topic

SurferTim

That should work perfect...as long as setup(), readPage(), and httpParse have no errors. But my crystal ball is in the shop for repairs!  ;)

edit: If you did not get the hint, post all the code.

daytrader152

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

SurferTim

This is incorrect.
Code: [Select]
Ethernet.begin(mac, ip, gateway, subnet);
 Serial.println(Ethernet.localIP());


V1.0 uses a different format. If you specify more than just the ip, you must specify a dns server ip. Normally, if you do not plan on resolving any domain names, you can use the gateway for that ip. My routers will provide localnet ips with dns service.

Code: [Select]
Ethernet.begin(mac, ip, gateway, gateway, subnet);
 Serial.println(Ethernet.localIP());


edit: And you are not reading the response from the server. You requested the page. At least be polite enough to read it. You don't have to do anything with it. Let the server close the connection.

Code: [Select]
while(client.connected())
{
  // stay in this loop until the server closes the connection
  while(client.available())
  {
     client.read();
  }
}
// now close your end
client.stop();


Also, if your ethernet shield has a microSD card reader, you need to disable the SD SPI interface if you do not plan on using it. In setup() before Ethernet.begin() call:

Code: [Select]
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);



daytrader152

I tried all your suggestions, but I am still seeing connection failure periodically. I have another Ethernet shield coming next week. I will try that one to see if it makes any difference.

BTW, I noticed that on my setup after uploading, it takes several watchdog resets until the Arduino receives the first response from the server. Once the first response is received, everything runs fine (except for the periodic connection failure, but at least no watchdog reset). Have you seen similar issue in the past? Below is the log. Thanks.

======log=================
192.168.2.48
starting arduino client for front entrance

*192.168.2.48
starting arduino client for front entrance

*192.168.2.48
starting arduino client for front entrance

*192.168.2.48
starting arduino client for front entrance

*192.168.2.48
starting arduino client for front entrance

*192.168.2.48
starting arduino client for front entrance

*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*....

SurferTim

I have not seen that one. For some reason, the connection is failing. It will take about 20 seconds to return from the client.connect() routine if it cannot establish a connection to the server. The watchdog timer only waits 8 seconds, correct?

Maybe it is the ethernet shield. Post your results when you get the new shield.

edit: Check your ethernet shield carefully. There has been one case where the w5100 had pins solder jumpered. Take a look at the pics in this subject.
http://arduino.cc/forum/index.php/topic,90960.0.html

Go Up