UDP listening stop after some time

I wrote simple program which listening on one port if data received (Currently ignoring what data come on port) from PC it will blink light. It works properly if device start and I send data continuously from my PC but If I am stop sending data more than 45-50 seconds light not blinking. I don’t understand why? Can anybody check my code and tell me where I am going wrong?

unsigned int localPort = 2201;      
int led = D7;     // We name pin D1 as led

const int PACKET_SIZE = 12;
byte  packetBuffer[PACKET_SIZE]; 

void setup() 
{
    pinMode(led, OUTPUT);
    Udp.begin(localPort);     // start the UDP
}

void loop() 
{
    int packetSize = Udp.parsePacket();
    if(packetSize)
    {
            digitalWrite(led, HIGH);   // Turn ON the LED
            delay(100);               // Wait for 1000mS = 1 second
            digitalWrite(led, LOW);    // Turn OFF the LED
            delay(100);               // Wait for 1 second
            
            Udp.read(packetBuffer,PACKET_SIZE);
            IPAddress remote = Udp.remoteIP();
        }
    }
}

I’m experiencing the same issue.

I used this as a work-around:

    if (int nbytes = myUDP.parsePacket()) {
        myUDP.read(myBuffer,nbytes);
        for(int i=0;i<nbytes;i++) {
            char c = (char)myBuffer[i];
            Serial.print(c>>4,HEX);
            Serial.print(c&0x0f,HEX);
        }
        Serial.println();
        Serial.println(myUDP.remoteIP());
        myUDP.stop();
        delay(500);
        myUDP.begin(localPort);
    }

You can tune to delay(500) value based on your application.

I think it will not work if there is no data received for long time. As you write myUDP.stop() is in if loop. But i will try this solution.

I only call stop after I receive something, not in the loop in general. This worked for me in my application, but it is obviously not a general solution.

Hmm, I wonder if the CC3000 is timing out the UDP listener after 60 seconds of inactivity? Sounds like some community members were talking about that here as well: https://community.spark.io/t/strange-udp-bug/2583/26

Thanks,
David

OK, my bad! I see what you all are seeing now.

This works but is much less than ideal:

void loop() {
    unsigned long nowsec = millis();
    if (nowsec >= (lastsec + delaysec)) {
        lastsec = nowsec;

        if (int nbytes = myUDP.parsePacket()) {
            myUDP.read(myBuffer,nbytes);
            for(int i=0;i<nbytes;i++) {
                char c = (char)myBuffer[i];
                Serial.print(c>>4,HEX);
                Serial.print(c&0x0f,HEX);
            }
            Serial.println();
            Serial.println(myUDP.remoteIP());
        }
        myUDP.stop();
        delay(1);
        myUDP.begin(localPort);
        
    }
}