[SOLVED] Particle.subscribe during setup different P1 0.5.? vs 2.3.1

Spent hours tracking this one down, and I don't see a clear reference to it in the docs. Subscribes often don't work in the setup code (before loop is started) ...

I had an old P1 device running fine "in the field" for many years. (FW: 0.5.?) Needed to change the wifi credentials, CLI no longer worked for that, web didn't work either. Did a full device OS update that then allowed wifi to update, but then my firmware stopped working. A morning of debug shows that particle.subscribes (some) don't work during the setup code. This may have been a change from 0.5.? to 2.3.1.

See code for what seems to work and what doesn't (and what changed.)

Maybe this will help someone down the road.

int i = 0;

void handleMySub( const char *event, const char *data ){
    Serial.print( "handleMySub: " );
    Serial.println( data );
}

void handleNameSub( const char *event, const char *data ){
    Serial.print( "handleNameSub: " );
    Serial.println( data );
}

void setup()
{
    Serial.begin(9600);

    Particle.subscribe("particle/device/name", handleNameSub);
    Particle.subscribe("myMessage", handleMySub);

    Serial.println( "Will sit in process loop and see if subs work" );
    for( int i=0; i < 10000; i++ ) {
        Particle.process();
        delay(1);

        if ( i % 2000 == 0 ) {
           Particle.publish("particle/device/name");   // publish and subscribe work, both FW revs
           Particle.publish("myMessage","payload");    // publish works, both FW revs
                                                       // subscribe succeeds 0.5.?
                                                       // subscribe FAILS    2.3.1
        }

    }

    Serial.println( "Exiting Setup" );

}

void loop()
{
   if ( i++ % 5000 == 0 ) {
           Particle.publish("particle/device/name"); // publish and subscribe work, both FW refs
           Particle.publish("myMessage","payload");  // publish and subscribe work, both FW refs
   }
   delay( 1 );
}

Years ago I had a long discussion with the Particle devs about this "issue".

Only "system" event subscriptions like "particle/device/..." will come into effect immediately.
All others only come into effect once setup() has been completed.

The reasoning behind that was never really clear to me.

1 Like

Is there a way to create an "issue" against the documentation. Feels like this needs to be called out there.

The text now reads:

You can call Particle.subscribe from setup() or from loop(). The subscription list can be added to at any time, and more than once. The subscriptions are only updated once loop is running, however. During setup() the subscription will not yet be active. Very old versions of Device OS (prior to 1.0.0) may behave differently.

2 Likes

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