Core breathing green on WiFi reconnect. Photon breaths cyan with the same code

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
    digitalWrite(tstPin, HIGH);
    Serial.println("connecting WiFi");
    Serial.println("new udp port");
    Serial.println("udp stop");
    delay(10000); //longer delay to allow router to allocate the IP address
    Serial.println("udp begin");
    previousPoll = currentTime;
// now do the udp.parsePacket() / / 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

You’d need to call Spark.connect() for the cloud.

1 Like

Thank you for the quick reply @ScruffR. That worked a treat. I replaced WiFi.connect() with Spark.connect() and everything works beautifully.

1 Like