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.
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).
@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! ! !