Is there a limit to how many variables I can particle.subscribe to?

EDIT: Whoops! Found the appropriate line in the docs: “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.” Well that sucks. How am I supposed to get more than 4 variables from one Photon to another then?

I’ve got two Photons, one is running out in my shed connected to a weather sensor, the other is running indoors connected to an LCD screen.

Originally, I had the one in the shed outputting 4 variables via Particle.Publish - Temp, humidity, pressure, and absolute humidity - sending all four at once every 20 seconds. And the LCD one in the house happily displayed all of them.

Well today I added an anemometer. I made sure to stagger its particle.publish event 10 seconds apart from the other one, because I’m well aware of the 4-per-second limit, and then it publishes once every minute - wind speed, and wind gust. I know these events are publishing just fine without issue, because I can see them appearing on my Particle Web Console.

What isn’t working, is the ability to display them on the indoor photon with the LCD. The original 4 weather variables were showing up just fine, but not the wind speed, even though I could see it being published in the console. I discovered that by rearranging the particle.subscribe code in the setup to a different order, suddenly I was able to see the Wind speed, wind gust, pressure and humidity, but no temperature or absolute humidity.

It’s as though particle.subscribe can only subscribe to a max of 4 variables, but I can’t see anything to indicate as such in the docs.

there is an easy workaround:

Subscribe like this:

Particle.subscribe("MyName", webhookHandler, MY_DEVICES);

and handle like this:

void webhookHandler(const char* event, const char* data)
  if (strstr(event, "sun_time"))
    gotSunTime(event, data);
  else if(strstr(event, "current_weather"))
    gotCurrentConditions(event, data);
  else if(strstr(event, "forecast_weather"))
    gotForecastWeather(event, data);

and publish like this:

Particle.publish("MyName_current_weather", command, 60, PRIVATE);

Hey thanks, Bulldog! Okay, I think I get it, I make it so that all my Particle.publish events have the same prefix like they all start with MyName_…

Then I make one handler for any particle event that starts with “MyName_…”, and then inside that handler I figure out which event we’re actually looking at and where to send the data.

Does this still work when all 4 events are sent at the exact same time?

1 Like

if rate limits are an issue (i.e. too many publishes at once) you can simply combine your data into a single publish… then parse it at the receiving end.

If I were you I would send all four variables wrapped in an easy-to-parse string:

char publishString[255] = "";  // yes you can send up to 255 bytes
snprintf(publishString, sizeof(publishString), "%.2f~%.1f~%d~%.1f~", temp, humidity, pressure, dewPt);
Particle.publish("MyData", publishString);


now your data looks like this:


easy to parse on the receiving end using strtok()!

I don’t think they’re an issue, I’m staying under the max particle.publish limit of 4 every 1 second. I’m just wondering if I send them all as MyName_ events, and I send all 4 of them at once, if just one particle.subscribe handler, set to pick apart the suffixes after MyName_…, will still see all 4 events, or just the first one, or what…

But thanks for that parsing trick, I might just do that instead.

EDIT: Looks like it does, indeed, see all 4 events, through just one handler, even though they were all sent at the same moment. Kinda makes you wonder why the Particle docs doesn’t just suggest this method in the first place!

Even if you can stay below the rate limit, it’s far from good practice to shoot multiple events when you can also do it in one.

“Kind of” does :wink:

And the fact that you can wrap multiple values of all kinds in one string is such common knowledge that it needn’t be explicitly stated IMHO.