# Check cloud or ethernet connection before publish

**URL:** <https://community.particle.io/t/check-cloud-or-ethernet-connection-before-publish/59388>\
**Category:** Cloud\
**Created:** [February 14, 2021, 3:35pm UTC](https://community.particle.io/t/check-cloud-or-ethernet-connection-before-publish/59388 "2021-02-14T15:35:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vacquah](https://avatars.discourse-cdn.com/v4/letter/v/a9adbd/32.png) [@vacquah](https://community.particle.io/u/vacquah)\
**Post date:** [February 14, 2021, 3:35pm UTC](https://community.particle.io/t/check-cloud-or-ethernet-connection-before-publish/59388/1 "2021-02-14T15:35:50Z")

</div>

I am having an issue where my devices loose their connection to the cloud - rapidly blinking cyan. This happens sometimes at the same time on all devices . I suspect this is during a publish event.

I am using the ethernet featherwing with the following:

```auto
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);

```

then in my setup:

```auto
	System.enableFeature(FEATURE_ETHERNET_DETECTION);

	WiFi.off(); // this is an argon device
	Ethernet.on();
	Ethernet.connect();
	Particle.connect();
	waitFor(Particle.connected, 1000); 

	client.connect(MQTT_CLIENT_NAME, "xxx", "xxxx"); // connect to mqtt server

```

then in my loop

```auto
  if(currentMillis - previousMillis > Publishinterval) {
  previousMillis = currentMillis;

    if (Particle.connected()) {
        publishQueue.publish(eventName, payload, PRIVATE, WITH_ACK);
      }

    // send to mqtt broker
    if (client.isConnected()) {
      client.publish(eventName, payload);
      }  

  }

```

I looked up the device connection status on my pfsense router and its showing as offline. Which leads me to believe it may not be a cloud connection issue?

Should I be checking the ethernet connection and/or the particle cloud connection before pushing data to the cloud and mqtt server? here is what I am thinking of doing now in my loop. I’d appreciate some help to clean this up - perhaps add the check for ethernet connection and also merge it all into a nice if else non blocking statement?

```auto
// this keeps the mqtt connection live
  if (!client.isConnected()) {
  client.connect(MQTT_CLIENT_NAME, "xxx", "xxx");
      } else {
      client.loop();
  } 
 // check cloud connection
  if (Particle.connected() == false) {
    Particle.connect(); // will this block loop?
  }

  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > Publishinterval) {
  previousMillis = currentMillis;

    if (Particle.connected()) {
        publishQueue.publish(eventName, payload, PRIVATE, WITH_ACK);
      }

    // send to mqtt broker
    if (client.isConnected()) {
      client.publish(eventName, payload);
      }  

  }

```

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [February 14, 2021, 7:38pm UTC](https://community.particle.io/t/check-cloud-or-ethernet-connection-before-publish/59388/2 "2021-02-14T19:38:21Z")

</div>

If you are losing cloud connectivity (fast blinking cyan) on publish on Ethernet, the most likely cause is that you need to lower the keep-alive using Particle.keepAlive. Try setting it to 30 seconds and see if the problem goes away.

When a Gen 3 device connects to the cloud it uses DTLS over UDP, and your network router/firewall sets up a temporary port mapping to allow return packets from the cloud back to the device. How long this temporary port mapping stays active is site-dependent.

If this is the problem, then it probably won’t help to check if Particle.connected is true, because it will be true until the publish is tried and fails, because the port mapping went away. Likewise, you probably still have Ethernet link (Ethernet.ready), so that won’t help either.

---

<div class="post-metadata">

**Author:** ![vacquah](https://avatars.discourse-cdn.com/v4/letter/v/a9adbd/32.png) [@vacquah](https://community.particle.io/u/vacquah)\
**Post date:** [February 14, 2021, 10:53pm UTC](https://community.particle.io/t/check-cloud-or-ethernet-connection-before-publish/59388/3 "2021-02-14T22:53:51Z")

</div>

hello @rickkas7 I will use the Particle.keepAlive bit and report back.

But wondering why the device looses connectivity entirely, including to the local mqtt server. Isn’t the keepAlive only related to the connection to the cloud?

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [February 15, 2021, 8:55am UTC](https://community.particle.io/t/check-cloud-or-ethernet-connection-before-publish/59388/4 "2021-02-15T08:55:29Z")

</div>

It depends what else you have in your loop. If you have any other Particle.publish() calls in the loop itself (the async one is OK), calls like Cellular.RSSI() or Cellular.command(), those calls will block when the cloud connection is lost, which will then cause the loop to stop running so your MQTT code would not run.

---

<div class="post-metadata">

**Author:** ![vacquah](https://avatars.discourse-cdn.com/v4/letter/v/a9adbd/32.png) [@vacquah](https://community.particle.io/u/vacquah)\
**Post date:** [February 15, 2021, 3:23pm UTC](https://community.particle.io/t/check-cloud-or-ethernet-connection-before-publish/59388/5 "2021-02-15T15:23:11Z")

</div>

@rickkas7 I have nothing like that going on in my loop ( see below ). And some devices just went offline again after setting the keepAlive. I am wondering if the udp timeout states on my pfsense firewall is also contributing to this. Its currently set to:

```auto
udp.first 60s
udp.single 30s
udp.multiple 60s

```

I am going to set it to below, in addition to setting keepAlive to see what happens.

```auto
udp.first 300s
udp.single 150s
udp.multiple 900s

```

my loop

```auto
int counter = 0;
char buf[64];

void loop() {

  MeasurementData sample;

  // SHT31D
  readTemperatureHumidity(sample);
  // SGP30
  setHumidityCompensation(sample);
  readAirQuality(sample);
  readBaseline(sample);
  refreshBaseline(sample);

  char payload[512];
  float tempF = (sample.temperature* 9) /5 + 32;
  int readtime = Time.now(); //Unix Format

  snprintf(payload, sizeof(payload),"{\"readtime\": %ld000,\"deviceID\":\"%s\",\"deviceLocation\":\"%s\",\"deviceName\":\"%s\",\"deviceType\":\"%s\",\"tempC\": %.1f,\"tempF\": %.1f,\"relative_humidity\": %.1f,\"tvoc\":%.1f,\"eco2\":%.1f}",readtime,(const char*)System.deviceID(),deviceLocation,deviceName,deviceType,sample.temperature,tempF,sample.humidity,sample.voc,sample.co2);

  if (millis() - step_timer > 4000) {
  step_timer = millis();
 
    oled.clearDisplay();
    oled.setCursor(0, 0);

    switch(step) { // Run the current "step"
      case 0:
        oled.setCursor(0, 0);
        oled.println(Time.format(Time.now(), "%a %b,%e"));
        oled.setCursor(0, 18);
        oled.println(Time.format(Time.now(), "%l:%M %p"));
      step = 1; // Change "step" to the next step
      break;

      case 1:
        oled.setCursor(0,10);
        snprintf(buf, sizeof(buf), "%.1f ", tempF); // temperature
        oled.print("Temp "); oled.println(buf);
      step = 2; // Change "step" to the next step
      break;

      case 2:
        oled.setCursor(0,10);
        snprintf(buf, sizeof(buf), "%.1f ", sample.humidity); // humidity
        oled.print("RH "); oled.println(buf);
      step = 3; // Reset "step" to the first step to start over
      break;

      case 3:
        oled.setCursor(0,10);
        snprintf(buf, sizeof(buf), "%.1f ", sample.voc); // total volatile organic compounds
        oled.print("tVOC "); oled.println(buf);
      step = 4; // Reset "step" to the first step to start over
      break;

      case 4:
        oled.setCursor(0,10);
        snprintf(buf, sizeof(buf), "%.1f ", sample.co2); // co2
        oled.print("eCO2 "); oled.println(buf);
      step = 0; // Reset "step" to the first step to start over
      break;

      }
    oled.display();
  } 

  // this keeps the mqtt connection live
  if (!client.isConnected()) {
  client.connect(MQTT_CLIENT_NAME, "xxx", "xxxxx");
      } else {
      client.loop();
  } 
 // check cloud connection
  if (Particle.connected() == false) {
    Particle.connect();                   
  }

  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > Publishinterval) {
  previousMillis = currentMillis;

    if (Particle.connected()) {
        publishQueue.publish(eventName, payload, PRIVATE, WITH_ACK);
      }

    // send to mqtt broker
    if (client.isConnected()) {
      client.publish(eventName, payload);
      }  

  }

}

```

---

<div class="post-metadata">

**Author:** ![robc](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@robc](https://community.particle.io/u/robc)\
**Post date:** [February 16, 2021, 4:40pm UTC](https://community.particle.io/t/check-cloud-or-ethernet-connection-before-publish/59388/6 "2021-02-16T16:40:55Z")

</div>

During the day, a wi-fi connection can disconnect / reconnect several times. You need to give the **Particle.connect()** statement time to accomplish its task before moving on in the loop().

> [@vacquah](#):
>
> ```auto
> // check cloud connection
> if (Particle.connected() == false) {
> Particle.connect();                   
> }
> 
> ```

instead, try this:

```auto
    // check cloud connection
    if (Particle.connected() == false) {
      Particle.connect();   
      // After you call Particle.connect(), your loop will not be called again until the device finishes 
      // connecting to the Cloud. Typically, you can expect a delay of approximately one second.
      waitFor(Particle.connected, 60000);
    }

```

---

<div class="post-metadata">

**Author:** ![vacquah](https://avatars.discourse-cdn.com/v4/letter/v/a9adbd/32.png) [@vacquah](https://community.particle.io/u/vacquah)\
**Post date:** [February 23, 2021, 10:07pm UTC](https://community.particle.io/t/check-cloud-or-ethernet-connection-before-publish/59388/7 "2021-02-23T22:07:32Z")

</div>

hello @rickkas7@robc

I implemented the suggested changes above and wanted to give it a couple of days to see it made a difference. I am still experiencing the random device restarts. All the devices occasionally go into a rapid cyan blink and then reconnects sometimes or don’t reconnect at all in some cases. I also now observed the occasional flash of red in between the rapid cyan. It looses connectivity, even on the local lan and doesn’t publish the mqtt payload.

I have done the following:

1. increased the keepalive settings on my pfsense router from defaults to something more generous.

2. changed the code as suggested by @robc

```auto
 // this keeps the mqtt connection live
  if (!client.isConnected()) {
  client.connect(MQTT_CLIENT_NAME, "xx", "xxx");
      } else {
      client.loop();
  } 
 // check cloud connection
  if (Particle.connected() == false) {
    Particle.connect();
    waitFor(Particle.connected, 60000);                   
  }

```

I also dont have any of the things in my loop suggested by @rickkas7 to cause blocking. [Here is the entirety of my code ( github gist )](https://gist.github.com/vacquah/99871f58505619fe0b32aaf0ac11ce06) . Hope I can resolve this.

ps @robc I am using the particle ethernet featherwing, not wifi.

---

<div class="post-metadata">

**Author:** ![robc](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@robc](https://community.particle.io/u/robc)\
**Post date:** [February 24, 2021, 4:03pm UTC](https://community.particle.io/t/check-cloud-or-ethernet-connection-before-publish/59388/8 "2021-02-24T16:03:21Z")

</div>

> [@vacquah](#):
>
> I am using the particle ethernet featherwing, not wifi.

Hey, sorry, I missed that.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/3/2/3291e3c9351789fe8620ae5bcec8d3fec67ff221.png) [@system](https://community.particle.io/u/system)\
**Post date:** [August 26, 2021, 4:03am UTC](https://community.particle.io/t/check-cloud-or-ethernet-connection-before-publish/59388/9 "2021-08-26T04:03:51Z")

</div>

Closed on August 26, 2021, 4:03am UTC

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