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 );
}