I’ve been trying to improve the reliability of Core UDP communications. The main issue is that if the WiFi signal is temporarily lost or the router is reset then the Core (or Photon) doesn’t always reconnect and respond to UDP packets even though my code is still running. With my busy network and slightly ropey ISP provided router the Core may hang after several hours or may be fine for two weeks or more.
My workaround is to set a timeout and if this is reached, restart the WiFi communications and stop then restart the UDP Server.
This works fine and I can get at my UDP data but there is a strange side effect. After restarting the WiFi the Core breathes green rather than cyan. I understand that this means that I have WiFi access but not Cloud access. In contrast a Photon goes back to breathing cyan as one might expect. Do I need to add another instruction or an extra delay to get back to the cloud?
//in the Loop function ...
currentTime = millis();
if (currentTime - previousPoll > 60000UL){ //timeout triggered if no UDP poll detected for a minute
Serial.println("timeout");
digitalWrite(tstPin, HIGH);
Serial.println("connecting WiFi");
delay(1000);
WiFi.disconnect();
delay(1000);
WiFi.connect();
Serial.println("new udp port");
udp.stop();
Serial.println("udp stop");
delay(10000); //longer delay to allow router to allocate the IP address
udp.begin(localPort);
Serial.println("udp begin");
previousPoll = currentTime;
Serial.println(WiFi.localIP());
Serial.println(udp.remoteIP());
Serial.println(udp.remotePort());
Serial.println(WiFi.ready());
Serial.println("-----");
}
// now do the udp.parsePacket() / udp.read() / udp.beginPacket() / udp.write() / udp.endPacket() stuff and note the time that the poll was acted upon in previousPoll
// end of loop()
// the client polls this server every few seconds