Publish problems again

I’m just recently started to dabble again with photons, and so I may be missing something obvious. This simple bit of code (boiled way down from a larger project that exhibits the problem) refuses to execute a publish either in the setup or the loop immediately after flashing with the WEB-IDE, but will publish after I do a pin reset or repower on.
I’m using OS version 1.4.0. I’m also not getting the hash event message after flashing. Is there some simple error I’m missing or some residual publish issues like those of a couple of days ago that were attributable to the cloud at that time?

boolean justBooted = true;

void setup() {

    Particle.publish("anothertest","some data from setup", PRIVATE);
}

void loop() {

    if (justBooted) {
        justBooted = false;
        Particle.publish("anothertest", "some data from loop", PRIVATE);
    }
}

Although system events should not count against the rate limit some “bug” may have broken that rule.
To test that theory, just slap a delay(5000) at the top of your setup() and see what happens then.

After a lot of trial and error - the following works for me in all my current projects (Photon or ARGON/XENON based)

SYSTEM_THREAD(ENABLED);

SerialLogHandler logHandler(LOG_LEVEL_WARN, // Logging level for non-application messages
                            {
                                {"app", LOG_LEVEL_ALL} // Logging level for application messages
                            });

// device config

#define baudRate1 115200
#define baudRate2 19200

const byte sizeOfCodeVersion PROGMEM = 7;
const char codeVersion[sizeOfCodeVersion] PROGMEM = "ZCP21a"; //
const char deviceModel[sizeOfCodeVersion] PROGMEM = "zSimZ3"; //

//--------------------------------------------------------------------
void setup()
//--------------------------------------------------------------------
{
  const char str_deviceOS[] = "DeviceOS";
  const char str_init[] = "Initialised cloud connection";

  const int sizeOfTempText = 128;
  char tempText[sizeOfTempText];

  Serial.begin(baudRate1);
  delay(500); // wait for ports to initialise
  waitFor(Serial.isConnected, 10000);

  Log.info("Started");

  snprintf(tempText, sizeOfTempText, "%s", System.version().c_str());
  Log.info("%s %s", str_deviceOS, tempText);

  Log.info("....waiting for Cloud");
  if (waitFor(Particle.connected, 30000))
  {
    Particle.publish(str_deviceOS, tempText, PRIVATE);
    Log.info("....waiting for Time");
    waitFor(Time.isValid, 30000);
    Log.info("Started at %s", Time.timeStr().c_str());
  }
  else
  {
    Log.info(".....no Cloud");
    while (1)
    {
      Particle.process();
      delay(2000);
    }
  }
}

YMMV :smile:

Thank you @ScruffR and @shanevanj, but alas I couldn’t get either of your suggestions to produce a better result. And it may be days before I am able to actively work on this some more - I happen to live in one of the areas of northern California that will have power shut off very soon due to fire weather.

Power outage was delayed a few hours so had some time to experiment a little, and found a sort of solution but I don’t remember the cloud being this finicky in the past.

volatile bool justbooted = true;
const char * setupData = "some data from loop";
const char * loopData = "some data from loop";
unsigned long lastTime = 0;

void msgHandler(const char *event, const char *data) {
}

void setup() {
    // These next 3 lines "fixed" the failure to publish issue.
    //Neither the soft delay, nor the publishVitals alone
    //were sufficient. Both required. Weird. Don't remember 
    //the cloud being this finicky in the past
    Particle.publishVitals(0);
    lastTime = millis();
	while (millis()-lastTime < 60000) {Particle.process();}

    Particle.publish("anothertest", setupData, PRIVATE);
}

void loop() {

    if (justbooted) {
        justbooted = false;
        Particle.publish("anothertest", loopData, PRIVATE);
    }

}

I was able to reproduce the issue, but it’s a bit more complicated then that.
There is some inconsistency in the way the device behaves.

void setup() {
    pinMode(D7, OUTPUT);
    Particle.publish("startupTest","setup()", PRIVATE);
}

void loop() {
    static uint32_t ms = 0;
    if (millis() - ms < 1000) return;
    ms = millis();
    digitalWrite(D7, !digitalRead(D7));
    Particle.publish("startupTest", "loop()", PRIVATE);
}

When I flash this code the first time (after a longish period) it works as expected, but when I flash it “immediately” after that again, it won’t.

When it doesn’t work, you also don’t get the automatic spark/device/diagnostics nor the spark/device/app-hash system events.

This might have something to do with one of the recent changes on how the cloud handles session reconnects.

I’ll investigate a bit more.


@bpr, I’ve been working with Eli from Particle and he has found a fix for the cloud side.
So your original code should now work (without any hoops and loops to jump through).

3 Likes

@ScruffR It does indeed work as intended reliably now, whether from cold boot or quick reflash. Tested it multiple times - both the boiled-down test app and the larger project. Thank you very much! ! !

1 Like