Particle.subscribe() - any restrictions on the number of subscriptions?

Hello, I am fairly new to the Particles so bear with me…

I would like to set up a Core, let’s call it HUB and (at least) one REMOTE where the latter should receive via Publish and subscribe a series of logical states (boolean variables, true and false representing off and on states of some relays). So, on the HUB side, when a particular state (representing a boolean variable) changes, I issue a Publish like…

Particle.publish("System1","ON"); 

And the REMOTE grabs the info by

Particle.subscribe("System1", Sys1Handler); 

The handler function reads the argument passed by the published event and sets the local boolean variable on the REMOTE as true or false accordingly. By this way I would like to mirror the series of states (booleans) on the REMOTE, so that I can display them, e.g. LEDs or a dashboard or something.
I started coding with the first state and made sure the publish/subscribe works, e.g. by toggling an LED on the REMOTE side reflecting the state of an input pin on the HUB side.

This worked fine and then I started to expand my code by “multiplying” (copy, paste, and rename) the subscriptions like …

Particle.subscribe("System2", Sys2Handler); 
Particle.subscribe("System3", Sys3Handler); 
Particle.subscribe("System...", Sys...Handler); 

The problem I face is that as soon as I have three or more Particle.subscribe statements in my setup function, the Core does not work anymore after flashing the code. Initially the Core LED breathes in cyan as if everything is OK but it does not respond to the state changes of the HUB and occasionally signals an SOS by the blinking the red SOS code followed by one red blink.

I reproduced this several times with factory-resetting the core in between (as it did not respond to flashing attempts anymore).

Is there a maximum number of Particle.subscribe statements that I can use in one project? Or do I need to introduce some delay between issuing several Particle.subscribe statements in order not to make multiple calls to the cloud to quickly?

Give the docs a try, it doesn’t actually hurt :wink:

NOTE: A device can register up to 4 event handlers. This means you can call Particle.subscribe() a maximum of 4 times; after that it will return false.

https://docs.particle.io/reference/firmware/core/#particle-subscribe-

However the string passed to Particle.subscribe is a prefix filter, so you can Particle.subscribe(“System”, SysHandler) and SysHandler would get called for events for System1, System2, … with a single registration. The SysHandler can tell which one it was by the first parameter, which is a c string containing the event name.

4 Likes

Thanks, you are right. :grin:

1 Like