ethernet client program stop working

Hi there..

My Arduino adventure goes well but got issue in my last project..

Arduino Mega2560 + Ethernet Shield + some peripherals.
(static internal IP, gateway and DNS configured, no MAC conflict, no IP conflict)
Client connects frequently to php script to read and/or set some data.
Everything works well but from time to time program stop working.
Probably issue is related to internet connection (DSL) when my ISP reset connection.

In loop() my program check every 5 sec data from php script and for some information received call function ~30sec long.

Any suggestions?

How to check outside loop() if program is still working and reset board if time in loop is > 60sec?

Hori

In loop() my program check every 5 sec data from php script and for some information received call function ~30sec long.

If it takes 30 seconds to react to a change, is it even reasonable to check for a change every 5 seconds?

Any suggestions?

You want help with the code you didn't post?

PaulS:
If it takes 30 seconds to react to a change, is it even reasonable to check for a change every 5 seconds?
You want help with the code you didn't post?

Yes, it is.. check every 5 seconds for command to run 30 sec long task - of course when task is running no reason to read from host. Also to inform host that client is alive (status on website) and responds commands.

Code? Does it really matter? Let say it is typical example The question is how to check if loop() takes more than 60 sec and reset board if so - what inside loop() does not matter...

The only one modification is i check for specific command recived and call function. My function/task works well - never hangs

I did some more test today - there is no problem if I un-plug network cable, it starts well when cable is pluged back..

Code? Does it really matter?

It probably matters more to you than the forum.

The question is how to check if loop() takes more than 60 sec

At the start of loop(), record the time, using millis().

At the end of loop(), record the time. Compute the interval.

and reset board if so

You can't, easily. You might be able to use a servo and large capacitor. Charge the capacitor, and use it to keep the servo running long enough to rotate through enough motion to lean on the reset switch for long enough.

Nothing besides pressing the reset switch will fully reset the Arduino.

Have a look at this forum thread:

http://forum.arduino.cc/index.php?topic=277730.0

There are known reasons why all of the W5100 sockets can be blocked (permanently connected).

Surfer Tim has referenced an updated example program that includes a solution for stuck/blocked sockets at:

http://forum.arduino.cc/index.php?topic=277910.0

Basically within your loop() function you need to call another function that checks for, and immediately clears, any blocked/stuck sockets. Otherwise anything that runs Arduino ethernet, especially one exposed to the www world via port forwarding, can get totally blocked to incoming or outgoing connections.

Surfer Tim's example program gives you a procedure called checkSockStatus() that does just that.

Cheers

Catweazle NZ

I used this code to see how fast loop() runs. Slightly different from what you asked, which was seconds (or part thereof) per loop- mine's the inverse, number of loops per second. But pretty much the same kind of thing.

(Mine stops after a second, that's what the while(1){} does.)

//speed test

long startTime;
long runTime;
long counter=0;

void setup()
{
  startTime = millis();
  Serial.begin(9600);
  Serial.print("Start ");
  Serial.println(millis());
}

void loop()
{
  runTime = millis()- startTime;
  counter++;

  if (runTime >= 1000)
  {
    Serial.print("Looped ");
    Serial.print(counter);
    Serial.print(" times in ");
    Serial.print(runTime);
    while(1){
    };
  }

}

PaulS:
At the start of loop(), record the time, using millis().

At the end of loop(), record the time. Compute the interval.

It does not make any sense.. if program hangs inside the loop() never rich end of the loop..

PaulS:
You can't, easily. You might be able to use a servo and large capacitor. Charge the capacitor, and use it to keep the servo running long enough to rotate through enough motion to lean on the reset switch for long enough.

Nothing besides pressing the reset switch will fully reset the Arduino.

It was a joke, I believe...

Other answers do not really help
Still the same issue - if something happened inside the loop() and program hangs, no chance to check time at the end of the loop()

It is client, so I control when to close connection and flush client..

Well.. I think it could be solved by:

  • set timer1 interrupt at 1Hz
  • every 1 sec add 1 to resetCounter and check value, if > 60 reset board
    (no servos, no big capacitors, no trained dogs, no need to press button to fully reset the Arduino.)
  • in loop() having valid respond from the server set resetCounter = 0;

So, if program hangs and/or no answer from server for more than 60sec - reset the board.
The compromise is it will reset board if server is down but.. well.. if server is down it doesn't matter if I reset board..

Will test it today..

CatweasleNZ made some good points. He was the reason for my latest web server modifications. It now includes a stuck socket fix, but that should not affect the client code.

My example client sketch does fine. However, most example client sketches suffer from failures due to broken connections or stalled servers. That will lock up most client code if there is no timeout feature. Take a look at my client code. Maybe that will give you some ideas on how to prevent a lockup.
http://playground.arduino.cc/Code/WebClient
Examine the connectLoop variable and how it prevents the lockup you probably suffer from now.

Hori,

You can reset the board using the Atmega's watchdog timer. Here is a good tutorial on how to set it up:

http://citizen-sensing.org/2013/07/arduino-watchdog/

I am also seeing lockups with my Ethernet but it seems to be happening only when I unplug the serial monitor and leave the system running standalone.... So my search continues.

@CapacitiveSmoke: Maybe you should post your code. It is possible I can help you too.

@SurferTim: Thanks! I opened a new topic so not to hijack Hori's:

http://forum.arduino.cc/index.php?topic=278325.0

I apologize for the code being a bit messy and also having to attach it instead of inlining it since it was too long.