Hi all,
I had some troubles with a hanging WiFi-Connection in my Arduino Mega with a WiFi shield (more about this later, I am still investigating), and I wanted to get a more stable system by activating the Watchdog:
#include <avr/wdt.h>
//...
Serial.print("Enabling WDT ... ");
wdt_enable(WDTO_8S); // wdt_reset() has to be called at least every 8 seconds, or the Arduino will reboot
Serial.println("Done");
//...
void loop(){
wdt_reset();
// Do main stuff
}
But it turned out that the WiFi Library GitHub - arduino-libraries/WiFi101: Wifi library for the Arduino WiFi 101 Shield does not work with the watchdog
There are lots of timeouts in the Library like this:
// Wait for connection or timeout:
unsigned long start = millis();
while (!IS_CONNECTED && millis() - start < 20000) {
m2m_wifi_handle_events(NULL);
}
which of course causes the watchdog to reboot the Arduino if the action takes longer than 8 seconds (and I had that in places like WiFiClient::connect or WiFiClient::write, mainly when the WLAN connection is not so good, or the WLAN has been turned off).
I reduced all these timeouts to max. 6 seconds, and this worked fine for me. But probably there is a reason for the high timeouts - is there?
An alternative would be to add a wdt_reset(); to all these loops.
What do you think?
And another question: I found in some posts that you should not do a busy wait, but put a delay in the code (e.g. when trying to open a WiFi connection in a loop).
The above code in the WiFi Library does a busy waiting - at least as far as I have seen there is no delay() in m2m_wifi_handle_events() and below.
Would it be better to put a short delay in the while-loop above?
Thanks for any comments,
Klaus