Arduino Wifi UDP - shield disconnects randomly

I've been working on an Arduino sketch that receives a hex value in a UDP packet and performs an action based on the value.

Everything works fine for a while, and then at some point the WiFi seems to go zombie, and stops receiving packets entirely. Sometimes it happens within 5 seconds, other times it will work for 5 minutes or so before it begins. I added code to check the WiFi status and reconnect if there's an error. However, even once the Arduino is reconnected successfully, it still will never receive a packet. The only thing that seems to fix the issue is pressing the reset button.

At that point I started adding debug prints to determine what the reason was, and it seems that, for whatever reason, the error status that occurs is "WL_NO_SHIELD". That makes no sense to me, since it is obviously connected and could connect in the first place. Besides that, after a couple of milliseconds, it recognizes the board IS there and reconnects to the wifi network successfully, but can no longer receive any packets.

Here's an example output:

Attempting to connect to SSID: MyNetwork
Connection status: 3
Connected to wifi
SSID: MyNetwork
IP Address: 10.1.10.169
signal strength (RSSI):-70 dBm

Starting connection to server...
Received packet of size 7
From 10.1.10.11, port 48461
Contents:
00ff00
packet is valid

Wifi error: 255
Time: 215616
WiFi seems to have disconnected, restarting...
Attempting to connect to SSID: MyNetwork
Connection status: 3
Connected to wifi
SSID: MyNetwork
IP Address: 10.1.10.169
signal strength (RSSI):-68 dBm

Starting connection to server...

Wifi error: 255
Time: 333398
WiFi seems to have disconnected, restarting...
Attempting to connect to SSID: MyNetwork
Connection status: 3
Connected to wifi
SSID: MyNetwork
IP Address: 10.1.10.169
signal strength (RSSI):-67 dBm

Starting connection to server...

Wifi error: 255
Time: 746135
WiFi seems to have disconnected, restarting...
Attempting to connect to SSID: MyNetwork
Connection status: 3
Connected to wifi
SSID: MyNetwork
IP Address: 10.1.10.169
signal strength (RSSI):-70 dBm

...

I sent a UDP packet to the Arduino every time it reconnected, but none of them (except the ones before the first disconnect) are ever received. Does anyone know why this is happening? Does anyone know a fix? I've already got the latest firmware on the WiFi shield. I've also tried the "heartbeat" code that other people suggested for TCP connections, but that didn't help:

Udp.write("\0");

Edit: I'm using the official Arduino WiFi Shield R3, and Arduino Uno R3

Any news on this topic?

The WiFi shield I have been testing disconnects for no identifiable reason but it is not quite random. The disconnects appear to occur on a 5 minute boundary. Sometimes 5 minutes, some times 45 minutes, sometimes some other multiple of 5 minutes and sometimes, the shield runs fine for hours without disconnection.

I have had some success using the code snippet below. I currently have it implemented on an Uno which is using a WiFi shield to sync with an NTP server once an hour. I last reset the Uno about a month ago and it is still showing the correct time.

#define EPHEMERAL 49152

WiFiUdp* udp = NULL;
uint16_t srcPort;

void setup(void) {
   srcPort = EPHEMERAL + random(1000) + analogRead(A0);
}

void loop(void) {
  //WiFi connection control
    if (WiFi.status() != WL_CONNECTED) {
        if (udp != NULL) {
            udp.flush();
         udp.stop();
            delete udp;
        }
        WiFi.disconnect();

        // disable SD SPI
        pinMode(4,OUTPUT);
        digitalWrite(4,HIGH);

        do {
            WiFi.begin((char*) SSID, (char*) PSK);
        }
        while ( WiFi.status() != WL_CONNECTED );
        udp = new WiFiUdp;
      udp.begin(srcPort);
    }
    //etc...
}

noremacstew: At that point I started adding debug prints to determine what the reason was, and it seems that, for whatever reason, the error status that occurs is "WL_NO_SHIELD". That makes no sense to me, since it is obviously connected and could connect in the first place. Besides that, after a couple of milliseconds, it recognizes the board IS there and reconnects to the wifi network successfully, but can no longer receive any packets.

Wifi error: 255

That "WL_NO_SHIELD" message indicates to me the wifi shield firmware has crashed. I see the same thing when a client overloads the rx buffer when using the tcp server code.

edit: My router shows the wifi shield was disconnected from the network not because the radio failed, but because the shield did not respond to the group key exchange (part of WPA2 encryption).

I have tested the udp section of the code, and the only thing I see that creates an error, is using udp.stop(), then trying to start the udp socket again, but fpoto already knows that. https://github.com/arduino/Arduino/issues/1637

@MattS-UK: If the "delete udp" and "udp = new WiFiUDP" corrects that, maybe you should post a comment there.

I am currently running a test of a udp client sketch. I can disable and enable the router's wifi radio, and the shield reconnects and resumes like nothing happened (without any reconnect code).

Just an update on the test I mentioned in my last post. After two days and over 27,000 packets, it is still running.

I have disabled and enabled the router wifi radio several times and stopped the udp receiver (server) a few times to see if I could get the wifi shield device to fail, but it hasn't. It reconnects and resumes almost immediately when the radio is enabled or the receiver is restarted.

My sketch is the "client". It sends a packet, then listens for a response packet from the udp "server". In other words, it works just like NTP or DNS.

A last update. I forgot the Arduino was running. I just checked it, and it is still functioning. It's count is at 36,000 packets, and that is after it has rolled over an insigned int twice. That is over 150,000 packets.