Photon Sketch hangs when Cloud or Wifi connection is lost

Hello

I am new to Particle and have to say I like the Hardware. I have previously used the Wemos D1 mini for my projects and thought I would give Particle a try. The problem I am having is I had my sketch working perfect with the D1 mini but on the Photon when internet is down or when Wifi is down the sketch hangs. This is a problem because the photon will be controlling an egg Incubator so it’s critical for the sketch to continue to run even if connection is lost. Im hatching eggs worth $400 a dozen and would really suck if temp or Humidity drops too low because of a wifi or cloud disconnect that Im unaware of. The way I had it setup on the D1 is it would check the connection every minute to see if wifi was connected if it was not it would try to connect 15 times and after the 15th sketch would break and continue to run my code. Im new to coding so I am not sure how to accomplish this on the particle devices. I have seen something in the docs about Semi Automatic mode but from what I see you have to push a button for it to connect but that is an issue because if i go to work or away for a couple days I wont be there to push the button. I would like it to check the state of the connection automatically and try to reconnect if Internet connection comes back just like on the D1 mini. Here is the piece code from the D1 mini that checks the connection. I would like to accomplish the exact same thing with the Photon and Boron.

void setup()

{
  // Debug console
  Serial.begin(9600);

  timer.setInterval(60000L, connectWiFi);
  timer.setInterval(65000L, CheckConnection);


void loop()

{
  if (Blynk.connected()) {
    Blynk.run();
  }
  timer.run();
}


void connectWiFi()

{

  BLYNK_LOG("Connecting to %s", ssid);
  if (pass && strlen(pass))
  {
    WiFi.begin(ssid, pass);
  }
  else {
    WiFi.begin(ssid);
  }

  //if (WiFi.status() == WL_CONNECTED) {
  //}
  if (WiFi.status() == WL_CONNECTED) {
    Serial.print("Wifi Connected");
  }

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    tries++;
    if (tries > 15)
      //offline
      break;
  }

}

void CheckConnection()

{ // check every 11s if connected to Blynk server
  if (!Blynk.connected()) {
    Serial.println("Not connected to Blynk server");
    //Blynk.config(auth, IP, 80);
    Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
    Blynk.connect();  // try to connect to server with default timeout
  }

  else
  {
    Serial.println("Connected to Blynk server");
  }

}

Welcome to the community :+1:

This is the normal behaviour in default SYSTEM_MODE(AUTOMATIC) as can be found in the docs.

The simplest way to overcome this is to add SYSTEM_THREAD(ENABLED) at the top of your code.

That "button thing" is just an exemplary use case, you can opt for any logic to trigger the connection best fitting your needs.

So just add SYSTEM_THREAD(ENABLED) above setup and thats it nothing else needed?

Nope, not normally - unless you have some code that relies on the cloud connection to be already up and running, then you need to guard against that.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.