Particle.subscribe not working with SYSTEM_THREAD(ENABLED) on 0.7.0

Hello, have an issue where SYSTEM_THREAD(ENABLED) on Photon 0.7.0 makes my subscribe code not work. Any help appreciated! The self-subscribe is solely so that the publish event is completed without using any delays or “guesswork”.

I also saw some solutions on these discussions, but they are for older firmware versions and they didn’t seem to work: Delay or not delay: that is the questionParticle.subscribe not working when SYSTEM_THREAD(ENABLED);

The following code exits the while loop when the subscribe executes.

When the comment on SYSTEM_THREAD(ENABLED) is removed, I think the Photon is getting stuck in the while loop and it never exits (light blue breathing LED forever). I also tried adding Particle.process() after the delay in the while loop and that had no change in behavior.

bool sleep = false;

void subscribeHandler(const char* event, const char* data) {
    sleep = true;
}

//SYSTEM_THREAD(ENABLED);

void setup() {

    Particle.subscribe("test", subscribeHandler, MY_DEVICES);

    //allow Particle to connect because it is not blocking loop with SYSTEM_THREAD(ENABLED)
    waitFor(Particle.connected, 10000);
}

void loop() {

    Particle.publish("test", PRIVATE);

    //wait for subscription success
    while (!sleep) 
    {
        delay(100); 
    }

    //reset sleep bool
    sleep = false;

    //there is resistor connecting D3 to ground, I don't need wakeup pin but I do want to keep variables in memory without using VBAT
    System.sleep(D3, RISING, 30);
}

Thanks!
Calvin

If your initial connection happens to take longer than 10 seconds your first publish may be exectued before the connection is established and hence no subscription will be triggered and you’ll be trapped in the loop without the chance to get out of it anymore.
And for subsequent runs the problem is even more prominent as after sleep you immediately try to publish which will definetly fail and hence the code will be trapped.

This works tho’

SYSTEM_THREAD(ENABLED);

bool sleep = false;
void subscribeHandler(const char* event, const char* data) {
  sleep = true;
}

void setup() {
  Particle.subscribe("test", subscribeHandler, MY_DEVICES);
}

void loop() {
  if (Particle.connected())
  {
    Particle.publish("test", PRIVATE);
    while (!sleep) Particle.process();
    sleep = false;
    System.sleep(D3, RISING, 30);
  }
}
1 Like

Hey ScruffR, thanks for that info-- makes sense. However I flashed your code, and it doesn’t work either way (with system_thread enabled and without)! I am wondering if this is an issue with my particular hardware… console is showing events registering. Could this be a server side issue?

Thanks

What behaviour do you expect and what do you see?

Do you see the app-hash event in the console when you flash the new code?
One explanation for the failure to work for you may be that your flash hasn’t actually stuck to the device.
Try flashing from Safe Mode and also add some changes to the code (e.g. change the event name) to actually see whether this is the now or the old code running.
Also double check whether you are actually targeting 0.7.0

Flashing from safe mode onto 0.7.0 (and changing event name) I confirmed the new code was flashed (got app-hash on console). New event name was firing as well.

Still didn’t work, even when I hit reset button multiple times. Each time the event would read on console but did not go to sleep. Finally I cut power for about a minute and after powering back up it worked. So it looks like there’s something that’s not updating and it needs a power reset.

thanks for your help!

1 Like