Particle.publish?

I have the following code that runs every 30 seconds. I find that it publishes a few items and then stops…The indicator is flashing a fast aqua (connecting to the cloud). Everything indicates a strong wifi connection…What gives?

    while(!oneMin.interval());
    float int_temp, ext_temp;
    int_temp = ReadIntTemp();
    ext_temp = ReadWaterTemp();
    Serial.printlnf("On Board Temp(C) = %.2f:  External Temp(C) = %.2f", int_temp, ext_temp);
    Particle.publish("OnBdTemp", String::format("%.1f", int_temp));
    Particle.publish("ExternalTemp", String::format("%.1f", ext_temp));

The console shows messages as follows:

What does oneMin.interval() do? Most importantly how long will it keep that while() running?

Blocking the code flow for extended periods is not only bad practice but will also cause the cloud connection to break.
You should avoid these kinds of traps.
If you must have it, you should at least put a Particle.process() call into the loop or use SYSTEM_THREAD(ENABLED) to decouple the cloud process from your blocking code.

A better approach would go along the line of this

const uint32_t DELAY = 30000;
void loop() {
  static uint32_t msDelay = 0;
  if (millis() - msDelay >= DELAY) {
    msDelay = millis();
    // do your stuff every 30 seconds
  }
}

This way loop() can be free running without blocking the cloud process that normally kicks in between iterations of loop().

1 Like

Good to know…I will look into this. I have never done anything with OS on devices so I am not real familiar with what to use.
The .interval is a function from pollingTimer.h library. According to the library description:

// Returns true exactly once each delay interval.
  //
  // min_delay:
  //  If true, wait for full delay before returning true again.
  //  If false, wait for delay from when previous interval expired.
  //
  // This is only relevant if you don't call interval() during the millisecond
  // in which the previous interval expired.

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