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?
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 exampleThe 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..
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.
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.
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..
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.
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.